Old version of code showing up in docker container

Viewed 3390

I think I'm doing something silly but can't figure out what. I have a docker file for a simple python flask app (like below). When I build the container and then run it, I see that an old copy of my code is being run in the flask app. My git repo doesn't have any branches so it's unlikely to have been that. I also have made sure to delete the previous docker images by running the command docker image rm -f DOCKERTAG. Also have done docker prune for cleanup. But still after docker image is built and run, I see an older version of code. As I type this, I'm wondering if the github token would be tied to old version but that'd make no sense. Please help. For clarity, these are my build and run commands. I can also confirm that the issue is with git clone since the app runs with latest code when RUN git clone.... code is replaced with COPY ./ in the dockerfile.

docker image build -t XYZ .

docker container run -p 5000:5000 XYZ

FROM python:3.6
RUN apt-get update
RUN apt-get install -y git
RUN git clone https://GITHUBTOKEN@github.com/GITHUBACCOUNT/MYREPO
WORKDIR /translator-be
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app/app.py"]
2 Answers

Docker will always try to use the build cache to speed up the image build.
The problem in your case is that nothing has changed from docker's point of view, so it doesn't clone the repository again, but uses the build cache that contains the old version.

You can use --no-cache in this case:

docker build --no-cache -t XYZ .

If your build takes some time, this is obviously not a great solution, because you will completely loose the useful caching mechanism of docker.
I would therefore propose a different build setup, e.g. checking out the current state of the repository on a build server (e.g. Jenkins) and then building the container in the directory of the project:

docker build -t XYZ .

The docker build command caches the results of each RUN command. Since the command is the same, and nothing else has changed, it reuses the result of the last time it ran it.

You can either use --no-cache on the build command, or make sure that something changes at the point where you want the build command to redo all the RUN commands (for example COPY a changing file or an ARG with a new value).

Related