Docker Volume

Leo Lo
2 min readJan 13, 2021

With Docker Volume, we do not need to stop the docker container, rebuild our image, and then restart the container when we have some changes in our code. It can somewhat cleverly show our changes immediately inside the running container. It improves our speed of development and user experience in using docker service.

Before understanding docker volume, we should know how Docker file system works.

A Docker image made up multiple read-only layer. When we launch a container from an image, Docker adds a read-write layer to top of that of read-only layer .

When we build a image , the Docker makes a copy of the file from reference directory to the read-only layers. Then when we start a container, docker makes a copy of the file from the read-only layers up into the top read-write layer. So when we change the file in our reference directory, it does not affect our built image until we rebuild image. Rebuilding image sometimes waste our valuable time in development. So Docker volume can help us solve this issue.

FROM node:alplineWORKDIR '/app'COPY package.json .
RUN npm install
COPY . .CMD ["npm", "run", "restart"]

This is a sample docker file, Dockerfile.dev. Instead of executing the original command docker run -p 3000:3000 [image_id] , we add the following argument to our docker run command.

docker run -p [host_port]:[container_port] -v…

--

--