Docker Development - Mistakes 1
Hello dear coders
How had you been? I hope you all doing great.
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 git
COPY . /go/src
WORKDIR /go/src
RUN 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/config
RUN 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 didnt 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 necessory 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…
Written with StackEdit.
Comments
Post a Comment