Docker Mistakes — 1
Hello Readers
Today I am going to talk about my long (yet very small in total) development using docker experiences.
First lets see a simple docker file from my recent hobby project.
FROM golang:alpine
RUN apk update
RUN apk add gitCOPY . /go/src
WORKDIR /go/srcRUN set -e \
go get github.com/opentracing/opentracing-go \
&& go get github.com/uber/jaeger-client-go \
&& go get github.com/uber/jaeger-client-go/configRUN go build -o main .
RUN chmod 755 main
CMD [ “./main” ]
This dockerfile based on golang:alpine and just add git to docker-image.
Then copy current working directory to /go/src
Set working directory and installing opentracing packages.
Finally just build and running main go module.
What is the so called docker mistake here?
Here I didn't notice that, but when every time I run docker-compose up — build after changing the code opentracing and jaeger packages were downloading to docker-image.
I was thinking about it, almost post a question to Stackoverflow asking why my docker always download images
Suddenly I understood my mistake. Its the order in my dockerfile.
What I do is first coping current directory to docker-image and then install necessary packages to image.
What is happening here is that, when there is a change in code docker consider its a fresh new step and all the following commands are executed again as it can not use cached containers.
What I have learned here :
* Always put package installations at the top of the dockerfile
* Coping local changes to docker image should be done at the later stage.
I hope this would help someone…
Happy coding….