Docker, copy files in production and use volume in development
Asked Answered
T

1

8

I'm new to using docker for development but wanted to try it in my latest project and have ran into a couple of questions.

I have a scenario where I want to link the current project directory as a volume to a running docker container in development mode, so that file changes can be done locally without restarting the container each time. To do this, I have the following comand:

docker run --name app_instance -p 3100:80 -v $(pwd):/app appimage

In contrast, in production I want to copy files from the current project directory. E.G in the docker file have ADD . /app (With a .dockerignore file to ignore certain folders). Also, I would like to mount a volume for persistent storage. For this scenario, I have the following command :

docker run --name app_instance -p 80:80 -v ./filestore:/app/filestore appimage

My problem is that with only one dockerfile, for the development command a volume will be mounted at /app and also files copied with ADD . /app. I haven't tested what happens in this scenario, but I am assuming it is incorrect to have both for the same destination.

My question is, what is the best practice to handle such a situation?

Solutions I have thought of:

  • Mount project folder to different path than /app during development and ignore the /app directory created in the container by the dockerfile
  • Have two docker files, one that copies the current project and one that does not.
Tenebrous answered 16/7, 2018 at 9:17 Comment(0)
M
5

My problem is that with only one dockerfile, for the development command a volume will be mounted at /app and also files copied with ADD . /app. I haven't tested what happens in this scenario, but I am assuming it is incorrect to have both for the same destination.

For this scenario, it will do as follows:

a) Add your code in host server to app folder in container when docker build.

b) Mount your local app to the folder in the container when docker run, here will always your latest develop code.

But it will override the contents which you added in dockerfile, so this could meet your requirements. You should try it, no need for any complex solution.

Mudslinger answered 16/7, 2018 at 9:25 Comment(4)
So -v $(pwd):/app overrides ADD . /app? If so that's perfect! Didn't think it would :DTenebrous
Yes, ADD only do when you do docker build, -v will override the folder in container when docker run. You could try it.Mudslinger
It sounds logical, however didn't know if it was implemented that way or implemented to throw error or something. I will test it out. Is this quite common to do then, or is there other ways that people solve this issue?Tenebrous
For your scenario, it is the simplest way. Anyway, if more complex situation, people may transmit --build-arg to dockerfile when do docker build. Then dockerfile could do different things according to different envs(Your scenrio: control if add files to images)Mudslinger

© 2022 - 2024 — McMap. All rights reserved.