Docker-compose is a tool that helps us define and run multiple services together. We declare the services and their attributes in a configuration file and the docker-compose tool reads it and issues the command to docker CLI in order to start and run the services.
It helps us abstract all the creation of different components, building or pulling images etc… To run multiple services for a project.
Once a compose-file is declared, we can run everything all at once with a single run command, we can also manage them via the docker-compose interface.
Here’s an example docker-compose file.
networks:
app_network:
driver: bridge
services:
nodesvc:
image: nodesvc
container_name: nodesvc_container
build:
context: ./dfilepath
dockerfile: Dockerfile-name
environment:
NODE_ENV: development
env_file:
./env/.env_local
ports:
- "3000:8080"
networks:
- app_network
postgres:
image: postgres
container_name: postgres_container
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
PGDATA: /data/postgres
volumes:
- ./postgres:/data/postgres
ports:
- "5432:5432"
networks:
- app_network
restart: unless-stopped
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin_container
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin@gmail.com
PGADMIN_DEFAULT_PASSWORD: pgadmin
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- ./pgadmin:/root/.pgadmin
- ./pgadminhome:/home/
ports:
- "8080:80"
networks:
- app_network
restart: unless-stopped
Here, we are defining 3 services - nodesvc, postgres and pgadmin.
also, a network called app_network.
- We define service name, it can be used to resolve DNS among services in the app_network network.
- image will specify the image name that needs to be used to start up a container.
- The container will start with the name as specified in container_name.
- build helps to redefine the build path or where the Dockerfile is defined. context is the path to dockerfile and dockerfile is the filename of the Dockerfile
- environment specifies the environment variables available within the container.
- env_file is the path to an .env file with environment variables. which will be loaded as environment variables within the container.
- ports is a mapping to base os and container port, external:internal mapping allows to access the service from base OS’s port outside docker.
- networks specify a list of networks to which the service is connected. Every service in the network can see each other and use DNS to resolve their IPs.
- volumes is a mapping of persistent storage. A path from our base OS can be mapped to a path inside the container.
This is a basic overview and in advanced usage, there are more meaning to these keywords. Anyway, this is a good example to start with.
Once we execute docker-compose up
command from the directory with this compose file, all of the services will start. Images that are not present in the cache will be pulled or built and everything required to set up the network and container will be done by the docker-compose.
Usually, we run with docker-compose up -d
to detach the containers from the terminal.
docker-compose down
will stop all the services, tear down the containers and networks.
docker-compose ps
will show the container status of the services specified inside the compose file.