Docker compose is a tool that is used for defining and running multi-container Docker apps in an easy way.
It provides docker-compose.yml configuration file that can be used to bring up an app and the suite of services it depends on
with just one command.
docker compose up- start all services fromdocker-compose.ymldocker compose up db- start only servicedbdocker compose up --build- rebuild all images and run containersdocker compose restart db- restartdbservicedocker compose kill db- killdbservicedocker compose -f docker-compose.prod.yml up- start all services fromdocker-compose.prod.ymldocker compose -f docker-compose.yml -f docker-compose.staging.yml up -d- run Docker Compose and override config filedocker compose build- build images for all servicesdocker compose build db- rebuilddbservicedocker compose build php --build-arg PROXY_SERVER=10.200.10.200 --build-arg PROXY_PORT=8080 --build-arg PROXY_USERNAME=myuser --build-arg PROXY_PASSWORD=mypass- build using proxy settingsdocker compose downdocker compose down --remove-orphans- remove containers for services not defined in the Compose file.docker compose down -v- shutdown containers, remove volumes. Docker stores volumes at/var/lib/docker/volumesdocker compose logs db | less- show logs ofdbcontainerdocker compose ps- show running app's containersdocker compose exec fpm bash- connect to running fpm container and startbashconsole in itdocker compose exec db mysql -u root -ppassword- connect to running db container and start mysql consoledocker compose events- listen for events that happen on the Compose applicationdocker compose run --rm php-cli sh- run php-cli container, startshconsole.--rm- Docker also removes the anonymous volumes associated with the container when the container is removed.docker compose rm db- removedbcontainerdocker compose rm -fv- remove all containers with volumesdocker compose top- it will runtopcommand in the containers listed in thedocker-compose.yamldocker compose config- showdocker-compose.ymlcontent after the substitution step has been performeddocker compose images- show images used by containers
If you see an error:
docker compose up
Pulling db (mysql:)...
ERROR: Get https://registry-1.docker.io/v2/: Proxy Authentication Required
you should configure your Docker's proxy settings (see stackoverflow).
- Create file
/etc/systemd/system/docker.service.d/http-proxy.conf:
[Service]
Environment="HTTP_PROXY=http://kenny:[email protected]:3118"
Environment="HTTPS_PROXY=http://kenny:[email protected]:3118"
Environment="NO_PROXY=localhost,127.0.0.1,localaddress,.localdomain.com"
- Reload
docker:
sudo service docker restart
- Check if it works:
docker pull busybox
Example of docker-compose.yml with env variables:
services:
database:
image: postgres:${POSTGRES_VERSION:-15}-alpine
environment:
POSTGRES_DB: ${POSTGRES_DB:-app}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-123}
POSTGRES_USER: ${POSTGRES_USER:-app}
volumes:
- database_data:/var/lib/postgresql/data:rwWe can pass environment variables to docker-compose.yml files in these ways:
- By exporting the variable to the terminal before running docker compose:
POSTGRES_PASSWORD=123 docker compose config - By putting the variables inside
.envfile:POSTGRES_PASSWORD=123
The env_file option only passes those extra variables to the containers and not the docker-compose.yml file.
If we use build and image, the image will be built and will then be tagged based on the name given using image:
services:
some-service:
build: .
image: some-service:1.5services:
some-service:
# ...
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/ping"]
interval: 20s
timeout: 10s
retries: 3
start_period: 5sEvery 20 seconds after an initial period of 5 seconds, a ping to some-service will be issued using
curl with a timeout of 10 seconds. In case of failure, there should be 3 retries before marking the
instance unhealthy.
We use curl with -f option: fail fast with no output on HTTP errors.
Run docker ps to see if container's status is healty or unhealty.
Links: