appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Volumes and environment

Lesson 3 of 48 minBeginner

Keep data after a container is removed and configure containers.

A container filesystem disappears when the container is removed. To keep data, use a volume or a bind mount.

docker volume create dbdata
docker run -d --name db -v dbdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=change-me postgres:16
# bind mount: a host folder inside the container
docker run --rm -v "$PWD":/app -w /app node:22 node index.js
  • -v name:/path mounts a managed volume.
  • -v /host/path:/path mounts a host folder (bind mount), handy in development.
  • -e KEY=value sets an environment variable.
  • --rm removes the container when it exits.

Inspect and debug

docker exec -it web sh      # open a shell inside a running container
docker logs -f web          # follow the logs
docker inspect web          # full JSON details

Clean up

docker system df            # disk used by Docker
docker system prune         # remove stopped containers, unused networks and dangling images

Carefuldocker system prune -a --volumes also deletes unused volumes, which can destroy data you wanted.

Test yourself

Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.

Hintdocker exec -it name shell.
Hintlogs with -f.
3. How do you keep database files after removing the container?
4. What does -e POSTGRES_PASSWORD=change-me do?

Key terms