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 -f docker-compose.prod.yml up- start all services fromdocker-compose.prod.ymldocker-compose build db- rebuilddbservicedocker-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 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 config- showdocker-compose.ymlcontent 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:[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
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 ismyappand it includes two servicesdbandweb, then Compose starts containers namedmyapp_db_1andmyapp_web_1respectively.