009820
009820

Reputation: 47

How to run golang web app on a Docker container

I have a web app that uses go language as it's back end. When I run my website I just do go build; ./projectName then it will run on local server port 8000. How do I run this web app on a container? I can run sample images like nginx on a container, but how do I create my own images for my projects. I created a Dockerfile inside my project folder with the following codes:

FROM nginx:latest

WORKDIR static/html/

COPY . /usr/src/app

Then made an image using the Dockerfile, but when I run it on a container and go to localhost:myPort/static/html/page.html it says 404 page not found. My other question is, does docker can only run static pages on a container? cause my site can receive and send data. Thanks

this is my docker file (./todo is my project name and folder name)

this is my terminal ( as you can see the the container exits emmediately)

Upvotes: 0

Views: 3999

Answers (3)

Truong Dang
Truong Dang

Reputation: 3417

Here is what i did for my GOlang web app use Gin-gonic framework - my Dockerfile:

FROM golang:latest
# Author
MAINTAINER dangminhtruong
# Create working folder
RUN mkdir /app
COPY . /app
RUN apt -y update && apt -y install git
RUN go get github.com/go-sql-driver/mysql
RUN go get github.com/gosimple/slug
RUN go get github.com/gin-gonic/gin
RUN go get gopkg.in/russross/blackfriday.v2
RUN go get github.com/gin-gonic/contrib/sessions
WORKDIR /app 

Then build docker image

docker build -t web-app:latest .

Finally, start my web-app

docker run -it -p 80:8080 -d web-app:latest go run main.go //My webapp start at 8080 port

Hope this helpfull

Upvotes: 1

Alex Pliutau
Alex Pliutau

Reputation: 21957

  1. You don't need Nginx to run a server in Go
  2. It's better to build a binary in Dockerfile

Here is how your Dockerfile may look like:

FROM golang:latest 
RUN mkdir /app 
ADD . /app/ 
WORKDIR /app 
RUN go build -o main .
EXPOSE 8000
CMD ["/app/main"]

Upvotes: 0

Prashanth
Prashanth

Reputation: 196

I guess you are not exposing the Docker Port outside the container. That's why you are not able to see any output rather than just being specific to GO Program. Try adding the below lines to your docker compose File

EXPOSE 80(whichever port you want it to be)
EXPOSE 443
EXPOSE 3306

This will make the container be accessed from outside

Upvotes: 1

Related Questions