Skip to content

Docker - Data and Volumes


TypesOfData.excalidraw.svg


ContainerImageData.excalidraw.svg


Data can be stored inside containers as files, just as you would normally do on your local development machine.

However, once a container is removed, the data will be lost.


  • Volumes are folders on your host machine hard drive which are mounted (“made available”, mapped) into containers.

  • Volumes persist if a container shuts down. If a container (re-)starts and mounts a volume, any data inside of that volume is available in the container.
  • A container can write data into a volume and read data from it.

FROM node:14
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 80
VOLUME [ "/app/feedback" ]
CMD ["node", "server.js"]

The VOLUME command specifies a path inside the container that will be treated as a volume. The mapping to the host’s file system is defined by the run command when starting a container.


Terminal window
# Anonymous Volume
docker run -v /app/data ...
# Named Volume
docker run -v data:/app/data ...
# Bind Mount
docker run -v /path/to/code:/app/code

VolumesBindmounts.excalidraw.svg


  • Containers can read + write data. Volumes can help with data storage, Bind Mounts can help with direct container interaction.
  • Containers can read + write data, but written data is lost if the container is removed.
  • Volumes are folders on the host machine, managed by Docker, which are mounted into the container.
  • Named Volumes survive container removal and can therefore be used to store persistent data.
  • Anonymous Volumes are attached to a container - they can be used to save data inside the container.
  • Bind Mounts are folders on the host machine which are specified by the user and mounted into containers - like Named Volumes.