Search code examples
dockerdockerfileproduction

Docker, copy files in production and use volume in development


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.

Solution

  • 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.