mikasa
mikasa

Reputation: 900

docker - execute a file from code that gets updated

I have an application in which the user submits the code which the docker then executes. Currently, I have an image that executes python code. My dockerfle is as follows:

FROM python:latest

WORKDIR /app

COPY /repository/test.py /app

CMD ["python", "test.py"]

It executes the code fine but the problem comes after that: If a user updates his code and the command is again run to execute the updated file the image does not run the updated file. It runs the old one. I'm assuming that's because when the image gets created from the dockerfile the file is copied for the first and the last time and this file will be executed again and again everytime its container is called.

How I can tackle this issue? Basically, I want to execute code from the docker that will be updated continuously and I need docker to cater for that.

Upvotes: 0

Views: 73

Answers (1)

smashed-potatoes
smashed-potatoes

Reputation: 2222

The problem is that the docker file explains how to build the image, and in this case you are copying the test file as part of the build process. If there are no changes to the docker file, docker isn't going to rebuild your image so the previously built image with the original test.py will always be used.

Instead of copying the file to the docker image (requiring a new image to be built with every change), you could mount a volume where the latest file will always exist

e.g.:

FROM python:latest
CMD ["python", "/mount/test.py"]

Then run

docker run -v /repository:/mount

(The -v option mounts the local /repository directory to /mount in the container)

Upvotes: 2

Related Questions