Docker, Where's My Data?

02-26-2020

Docker is a fantastic tool, one of favorite in recent years, but it can have some unexpected behavior sometimes.

If you ever mount volumes to your Docker containers to provide access to shared files between your container and host, you might have run into this issue. When you mount a volume into your Docker container, Docker will override any existing files, data, etc. in that directory.

Let’s look at an example:

Dockerfile

1
2
3
4
5
...

COPY myapp/ /app

...

Docker Run

1
docker run -v $(pwd)/myotherapp:/app node

Both our Dockerfile and our Docker run command are both trying to put files into the /app directory, one of them has to win. Docker will default to the docker run commands parameters, overriding anything defined elsewhere.