Welcome to our Docker Compose Cheat Sheet !
overview |
Docker-Compose is used to define and run multi-container Docker applications. You use a YAML file to configure your application’s services. Then, with a single command, you can create and start all the services defined in the yml file. |
docker-compose up is used to start docker compose defined services. Depending on the contents of the .yml file it may build the images, or it may start services from existing images on the docker host. |
docker-compose up | |
docker-compose up | uses docker-compose.yml to start services |
docker-compose -f <filename.yml> -f <filenamelocal.yml> up | uses a yml file in a custom location/name to start services |
-d, –detach | runs the services in detached/background mode |
–build | forces image build from dockerfile before starting services |
–no-build | bypasses the image build |
–force-recreate | force recreation of containers when starting services |
–no-color | outputs without colour |
–scale | lets you set the number of instances of the service |
stopping and restarting | |
docker-compose start | starts existing service/containers |
docker-compose stop | stops running services/containers but doesn’t delete them |
docker-compose pause | pauses running containers |
docker-compose unpause | unpauses running containers |
docker-compose restart | restarts all stopped and running services. |
docker-compose kill | forces running containers to stop |
docker-compose down | stops containers and removes containers, networks and images |
docker-compose down –volumes | stops containers and removes containers, networks, images and volumes |
remove stopped containers | |
docker-compose rm | removes stopped containers |
docker-compose rm -f | forces removal of containers |
docker-compose rm -s | stops the containers before removing |
example docker-compose.yml file |
version: ‘3.3’ services: db: image: mysql:5.7 volumes: – db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: – db image: wordpress:latest volumes: – web_data:/var/www/html ports: – “8000:80” restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: db_data: {} web_data: {} |
more docker-compose cmds | |
docker-compose ps | shows list of containers for a service |
docker-compose logs | displays logs for a service |
docker-compose pull | pulls the images specified in the docker-compose.yml file but doesnt start any services |
docker-compose build | builds images, but doesn’t start services |
docker-compose run | runs a command against a service |
docker-compose version | outputs the version of docker compose |
docker-compose config | View the docker-compose.yml file used to start the services |
installing docker compose |
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose $ sudo chmod +x /usr/local/bin/docker-compose $ docker-compose |
If you found this useful, please share!