Volumes and environment
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:/pathmounts a managed volume.-v /host/path:/pathmounts a host folder (bind mount), handy in development.-e KEY=valuesets an environment variable.--rmremoves 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 detailsClean up
docker system df # disk used by Docker
docker system prune # remove stopped containers, unused networks and dangling imagesCarefuldocker 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.