Docker Community Forums

Share and learn in the Docker community.

How to build a deploy a Golang project with Docker?


(Avivcohen) #1

I have built a Go project, which is a web application. It has packages and imports from different github repositories (such as Gorilla mux).

However, there aren’t really any docs on how to use Docker with a Go project that has dependencies and packages(folders).

I am new to Docker, but I am also totally lost. I’m hoping to deploy my project to the server using Docker, but I really have no clue in how to do that.

I tried creating a Dockerfile and following the Golang Docker Documentation, however the building proccess gives me the errors below.

What am I missing, or most precisely: What do I really don’t know in how to deploy a Go project with Docker?


(Archimedes Trajano) #2

General idea is to build an executable and package it together with whatever else it needs.

FROM alpine:3.7
COPY your files /
RUN chmod 0700 /yourexecutable
CMD [ '/yourexecutable' ]

(Avivcohen) #3


(Avivcohen) #4

Thank you for your response.

However, what happens when dependencies and packagea are involved? The docker cannot create the image.


(Raj Chaudhuri) #5

From your output, it looks like your application is called rm1, and is installed directly under the src folder under your GOPATH. Is this correct?

If that is the case, you should set that as your working directory in your Dockerfile. The Dockerfile from the golang documentation should be modified like this:

FROM golang:1.8

WORKDIR /go/src/rm1
COPY . .

RUN go get -d -v ./...
RUN go install -v ./...

CMD ["rm1"]

This should make it work. There are several other things you can do to make it better, but we would need to know more about your application before making further recommendations.


(Archimedes Trajano) #6

I think what you’re trying to do is build your application inside a container. I don’t normally recommend that for your “final image” because that means you have your development tools.

As you have stated in your OP

I presume that means you have a compiled executable. So what you put in your container is just the executable and whatever it needs (which usually does not require the go compiler.


(Avivcohen) #7

Ok, I am really sorry for been a noob with Docker (and maybe a little with Go).
So basically what I have done so far, is try to create a container with a project folder that contains go files and packages.

I have followed your commands, but doing so still provides me with the same errors from the Image above in the first comment.

@trajano I guess one of the main things I forgot is that I was trying to create an container with a folder, and not from an executable binary file(And also, what do I need to do in this case)?


(Archimedes Trajano) #8

I think you’re following @rajchaudhuri advice for the Dockerfile not mine. His approach uses the container to build your Go project.

My approach uses the container to run the executable built from your Go project outside Docker. If you want to do @rajchaudhuri approach you have to make sure you add any development dependencies that you need inside the container. I did a similar approach here

in which case I used centos as the base (because I wanted centos glusterfs) and installed go on the container then uninstalled it after I do the build.


(Avivcohen) #9

Thank you for the reply. I don’t have much of an idea on what you have done there. Is there a way for me to learn that?

And precisely in my case, what do I have to do (assuming I am going by your approach-which is more efficient?)?


(Archimedes Trajano) #10

I see that you’re on Windows but generally containers are using Linux (I haven’t tried the windows containers yet). So you need to work around that.

There’s an environment variable GOOS=linux which will allow you to cross compile Linux but I am not sure if the executable it builds works in Linux. But let’s assume it does not.

I would then have a Centos/Ubuntu VM running in VMWare. In that VM I would install Go and docker. Do my programming there and run the app outside the container. Once I get it working outside the container I just “dockerize” it using the Dockerfile I wrote on my first reply.

Others may have a different approach but that’s what I have done when I built the docker-volume-plugins.


(Raj Chaudhuri) #11

From your errors screenshot, it seems the go toolset is trying to import packages with the path rm1/controllers (using the command go get -d -v ./...) and failing. This is a go problem, not a docker problem.

I am assuming that you know about the go language conventions about package paths and names. If not, a good introductions is available here (https://golang.org/doc/code.html).

Does the go application directly on your Windows machine? Or are you getting the same problem there?

@trajano has given a good suggestion, in that your final docker image should contain just your executable and support files. You can, in fact, use one docker container to build your application, and then copy only the build outputs to another one, using docker multi-stage builds. But we can get to that, once we manage to successfully build and run the application.


(Avivcohen) #12

So at the momey I gave up a little. I have very little knowledge in Docker, and I thought Docker might be a very fast solution to deploy my first project.

But It just got messier. I am struggling to understand and I feel that I am dragging you all and still not managing to follow the instructions.

I really appreciate all the efforts and your help. I think I should force to understand Docker, but I do want to deploy my project soon too.


(Archimedes Trajano) #13

Understood. the whole container stuff is a bit of marketting, but it is nothing really new.

Java EE and IIS had the concepts earlier. Except with LxCs like Docker it goes almost down to the OS, where the other ones provide containers at a higher level (but provide higher level constructs such as databases, queues, clustering etc).

This comic sort of gives you a high level idea


(Avivcohen) #14

That’s actually a great comic. Thanks :slight_smile: