Let’s start with a simple example.
Example 1: Postgres
docker-compose.yml:
1 | version: '3.8' |
Note that:
- Make sure to set
name, otherwise, Docker will use folder name as container name. postgresis the service name, displayed under ‘project_name’ in Docker Desktop. A container can have multiple services.- ports:
: , that means your postgres runs on localhost:5458. - volumes is used to persist data.
persist_datais an identifier for the volume. It’s like an alias.nameis the actual name of the Docker volume, which will be displayed in Docker Desktop.
On Volume name
If you do not specify a name, Docker will use [project_name]_[service_name]_data as the volume name. For example, dushexuezhang_postgres_data. So, I recommend setting a name for your volume.
If you reuse the same name as another container, Docker will reuse the same volume, essentially you get same DB data.
Docker commands
To see all containers, run docker ps.
To see all images, run docker images.
To see all volumes, run docker volume ls.
To remove orphaned volumes, run docker volume prune.