- English
- English
Appearance
Appearance
The filesystem and data inside a container disappear when the container is removed. An app that needs persistent data, such as a database, or files from your laptop, such as source code, should use a volume or a bind mount.
Docker has two types of volume:
pgdata.Example of an anonymous volume:
docker run -d \
-v /var/lib/postgresql/data \
postgres:16In this example, Docker creates the volume without you giving it a name. Docker still manages the volume, but it is harder to recognize and reuse because its name is not clear.
Example of a named volume:
docker run -d \
-v pgdata:/var/lib/postgresql/data \
postgres:16pgdata is a name you choose. You can use any clear name that fits your needs, such as project-db-data or postgres16-data.
Both anonymous and named volumes are stored on your laptop or server, but Docker manages their storage location. You do not need to choose or change that location directly.
This lesson uses named volumes more often because their names are easy to remember, reuse, and manage through Docker CLI.
A path such as /var/lib/postgresql/data is a path inside the container, not a path on your laptop. When a volume is mounted, Docker connects the volume to that path:
Named volume mount format:
-v <volume-name>:<path-inside-container>Example:
docker run -d --name db \
-v pgdata:/var/lib/postgresql/data \
postgres:16In this example:
pgdata is the name of the named volume managed by Docker./var/lib/postgresql/data is PostgreSQL's data location inside the container.pgdata.db container, the pgdata volume remains.To view volume information, including the location used by Docker:
docker volume inspect pgdataUse this information when debugging. Do not edit files directly in Docker's storage location. Manage volumes through Docker CLI.
Both can provide data or files to a container. The main difference is how their storage is managed and what they are used for.
Bind mount
./html.Named volume
Use a bind mount when you need to access and change files directly from your laptop. Use a named volume when Docker should manage the data.
Run Nginx with a page from an html folder on your laptop:
mkdir -p html
echo '<h1>From the laptop</h1>' > html/index.html
docker run -d --name web -p 8080:80 \
-v "$(pwd)/html:/usr/share/nginx/html:ro" \
nginx:1.27-alpine-v laptop:container connects a laptop folder to a folder inside the container. :ro means read-only inside the container. This is useful for static files.
Change html/index.html, then refresh the browser. You do not need to rebuild the image.
On Windows PowerShell, the path format is different. Use an absolute path or Docker Desktop, which supports bind mounts from project folders.
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data \
postgres:16To prove that a named volume keeps data after its container is removed, remove the db container:
docker rm -f dbThen list the volumes:
docker volume lsThe data is still in the pgdata volume. A new container with -v pgdata:/var/lib/postgresql/data can see the same data.
TIP
docker rm -f db only stops and removes the container named db. It does not remove the pgdata named volume, so the data in the volume remains.
To remove the volume and its data, run docker volume rm pgdata separately. Do not run that command on a work machine unless you are sure the data can be deleted.