0

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)

009820
  • 37
  • 2
  • 9
  • 1
    The simplest way to run a golang web app in a container would be to start from the official golang image found here: [hub.docker.com/_/golang/](https://hub.docker.com/_/golang/) and follow the instructions of the "Start a Go instance in your app" section. – tgogos Nov 28 '18 at 09:51
  • You could try accessing the port of the localhost from within the Docker Container. [as pointed out here](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – Prashanth Nov 28 '18 at 11:09

3 Answers3

1

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

Prashanth
  • 186
  • 3
  • 13
1

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

Truong Dang
  • 3,119
  • 1
  • 15
  • 21
  • is your web app running on it's own or inside the container? cause my container exited immediately after running it, but the web app is up cause of the command //go build; projectName. So I guess my web app is running on it's own and not inside the container – 009820 Dec 03 '18 at 08:11
  • My web app run inside container. You container stop immediately my your set entry point for it. Can you show your dockerfile now ? – Truong Dang Dec 03 '18 at 08:20
  • `docker ps -a` then copy your docker container name id which exited. Then check container logs details `docker logs --details your_container_name` may helpfull – Truong Dang Dec 03 '18 at 09:46
0
  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"]
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
  • what is CMD command, should I do CMD["main.go"] ? and I'm confused with the WORKDIR, what directory you should choose? thanks – 009820 Dec 10 '18 at 07:50
  • CMD tells what will be executed when you start container. You don't need to run main.go, you need to build it first (RUN go build -o main .), and then /app/main will be your executable path. WORKDIR just means what is the pwd of your container. – Alex Pliutau Dec 10 '18 at 10:11
  • How do you make docker file ignore a package, cause mine is always trying to get a package that is actually just a folder that I imported to my golang code, If I use RUN go get github.com/folderName I will still get the error because it's not on github but just a folder. Thanks – 009820 Dec 11 '18 at 03:29
  • "cannot find package github.com/rn1hd/todo/models". But on my code i just imported that folder cause it contains some of my .go files, thanks – 009820 Dec 11 '18 at 03:45