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.yml
docker-compose up db
- start only servicedb
docker-compose up --build
- rebuild all images and run containersdocker-compose -f docker-compose.prod.yml up
- start all services fromdocker-compose.prod.yml
docker-compose build db
- rebuilddb
servicedocker-compose down
docker-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/volumes
docker-compose logs db | less
- show logs ofdb
containerdocker-compose ps
- show running app's containersdocker-compose exec fpm bash
- connect to running fpm container and startbash
console in itdocker-compose exec db mysql -u root -ppassword
- connect to running db container and start mysql consoledocker-compose run --rm php-cli sh
- run php-cli container, startsh
console.--rm
- Docker also removes the anonymous volumes associated with the container when the container is removed.docker-compose rm db
- removedb
containerdocker-compose rm -fv
- remove all containers with volumesdocker-compose config
- showdocker-compose.yml
content after the substitution step has been performed
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:123@277.27.16.233:3118"
Environment="HTTPS_PROXY=http://kenny:123@277.27.16.233: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
We can create .env
file and define there env variables.
COMPOSE_PROJECT_NAME=myapp
- Sets the project name. This value is prepended along with the service name to the container on start up. For example, if your project name ismyapp
and it includes two servicesdb
andweb
, then Compose starts containers namedmyapp_db_1
andmyapp_web_1
respectively.