Chris T
Chris T

Reputation: 483

Configuring a dockerfile to 'read' from a directory?

I am building a webapp (a simple flask site) that uses docker. I want my development code to not reside within docker, but be executed by the development environment (using python3) I have defined in my dockerfile. I know that I can use the COPY . . syntax in a dockerfile to copy my source code into the image for execution, but that violates my aim of separating the container from my source. Is there a way to have a docker container read and execute the code that it is in the directory I run the docker container run command from?

Right now my container uses the copy company to copy all the source code into the container. It then uses the CMD command to automatically run the flask app:

CMD [ "python", "flask_app/server.py" ]

(I'm storing all my flask code in a directory called flask_app). I'm assuming this works because all this has been copied into the container (according to the specifications given in the dockerfile) and is being executed when I run the container. I would like for the container to instead access and execute flask_app/server.py without copying this information into itself -- is this possible? If so, how?

Upvotes: 1

Views: 39

Answers (1)

Josh Karpel
Josh Karpel

Reputation: 2145

Instead of using COPY to move the code into the container, you'll use a "bind mount" (https://docs.docker.com/storage/bind-mounts/).

When you run the container, you'll do it with a command like this:

docker run --mount type=bind,source=<path_outside_container>,target=<path_inside_container> <image_tag>

For portability, I recommending putting this line in a script intended to be run from the repository root, and having the <path_outside_container> be "$(pwd)", so that it will work on other people's computers. You'll need to adjust <path_inside_container> and your CMD depending on where you want the code to live inside the container.

(Obviously you can also put whatever other options you'd like on the command, like --it --rm or -p <whatever>.)

Upvotes: 1

Related Questions