Show Menu
Cheatography

Docker Compose Cheat Sheet by

Docker Compose

Basics

Dock­er-­Com­pose: is a tool for defining and running multi-­con­tainer Docker applic­ations. With Compose, you use a YAML file to configure your applic­ation’s services. Then, with a single command, you create and start all the services from your config­ura­tion.
Need to good to yml file directory to succes­sfully run the docker­-co­mpose commands.
docker­-co­mpose start wordpr­ess_db :it will only start 1 service
but docker­-co­mpose start : will start all the services
similar is the case with other commands.

docker­-co­mpose start

Starts an existing service container.

docker­-co­mpose stop

-t, --timeout
specify a shutdown timeout in second­s.(­def­ault: 10)
Stops running containers without removing them. They can be started again with docke­r-c­ompose start.

docker­-co­mpose pause

Pauses running containers of a service. They can be unpaused with docker­-co­mpose unpause

docker­-co­mpose unpause

Unpauses paused containers of a service.

docker­-co­mpose restart

Restarts all stopped and running services.

docker­-co­mpose ps

-q, --quiet
Only display IDs
Shows list of containers for a service.

docker­-co­mpose logs

-f, --follow
Follow log output.
Displays log output from services.

docker­-co­mpose top

View the processes running within each service container.

docker­-co­mpose pull

Pulls an image associated with a service defined in a docker­-co­mpo­se.yml file, but does not start containers based on those images.

docker­-co­mpose rm

Removes stopped service contai­ners.By default, anonymous volumes attached to containers are not removed. You can override this with -v. To list all volumes, use docker volume ls.
-f, --force – Don’t ask to confirm the removal
-s, --stop – Stop the contai­ners, if required, before removing
-v – Remove any anonymous volumes attached to containers

docker­-co­mpo­se.yml

version: "3.7"
services:
  wordpress_db:
    container_name: "wordpress_db"
    image: "mysql:5.7"
    volumes:
      - ~/dockers/wordpress/.data/wordpress_db:/var/lib/mysql
    environment:
      MYSQL_USER: gaurav
      MYSQL_PASSWORD: victory
      MYSQL_DATABASE: db
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    networks:
      - wordpress_network
    ports:
      - 3307:3306
  wordpress_web:
    container_name: "wordpress_web"
    image: "wordpress"
    volumes:
      - ~/dockers/wordpress/.data/wordpress_web:/var/www/html
    environment:
      WORDPRESS_DB_HOST: wordpress_db
      WORDPRESS_DB_USER: gaurav
      WORDPRESS_DB_PASSWORD: victory
      WORDPRESS_DB_NAME: db
    networks:
      - wordpress_network
    ports:
      - 8080:80
    depends_on:
      - wordpress_db
networks:
  wordpress_network:

Depend­encies

 
# makes the db service available as the hostname database
# (implies depend­s_on)
links:
- db:dat­abase
- redis
# make sure db is alive before starting
depend­s_on:
- db

Network

# creates a custom network called frontend
networks:
  frontend:
 

docker compose up

docker­-co­mpose up
use docker­-co­mpo­se.yml
docker­-co­mpose -f <fi­len­ame.ym­l> -f <fi­len­ame­loc­al.y­ml> up
use custom yml files
-d, --detach
background detached mode
--build
forcefully Build images before starting contai­ners.
--no-build
skips the image build process
--forc­e-r­ecreate
Recreate containers even if their config­uration and image haven’t changed.
--no-color
Produce monochrome output.
--scale SERVIC­E=NUM
Scale SERVICE to NUM instances. Overrides the scale setting in the Compose file if present.
docker­-co­mpose up is used to start a project. It tries to automate a series of operations including building a mirror, (re)cr­eating a service, starting a service, and associ­ating a servic­e-r­elated container.
It also builds the images if the images do not exist and starts the contai­ners:

docker­-co­mpose down

Stops containers and removes contai­ners, networks, volumes, and images
By default, the only things removed are:
- Containers for services defined in the Compose file
- Networks defined in the networks section of the Compose file
- The default network, if one is used
created by up.N­etworks and volumes defined as external are never removed.

use -v to remove volumes also along with other things

docker­-co­mpose build

only builds the images, does not start the contai­ners:

docker­-co­mpose run

Runs a one-time command against a service. For example, the following command starts the web service and runs bash as its command :
docker­-co­mpose run wordpr­ess_db bash

docker­-co­mpose version

Prints the version of docker­-co­mpose.

docker­-co­mpose push

Pushes images for services to their respective regist­ry/­rep­ository

docker­-co­mpose config

Validate and view the Compose file.

docker­-co­mpose kill

Forces running containers to stop by sending a SIGKILL signal.

docker­-co­mpose bundle

A Dockerfile can be built into an image, and containers can be created from that image. Similarly, a docker­-co­mpo­se.yml can be built into a distri­buted applic­ation bundle

Building

web:
  # build from Dockerfile
  build: .
  # build from custom Dockerfile
  build:
    context: ./dir
    dockerfile: Dockerfile.dev
  # build from image
  image: ubuntu
  image: ubuntu:14.04
  image: tutum/influxdb
  image: example-registry:4000/postgresql
  image: a4bc65fd

Ports

ports:
    - "3000"
    - "8000:80" # guest:host
  # expose ports to linked services (not to host)
  expose: ["3000"]

Commands

# command to execute
  command: bundle exec thin -p 3000
  command: [bundle, exec, thin, -p, 3000]
  # override the entrypoint
  entrypoint: /app/start.sh
  entrypoint: [php, -d, vendor/bin/phpunit]

Enviro­nment variables

# environment vars
  environment:
    RACK_ENV: development
  environment:
    - RACK_ENV=development
  # environment vars from file
  env_file: .env
  env_file: [.env, .development.env]

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Ubuntu Compose Key Combinations Cheat Sheet
          docker-compose Cheat Sheet

          More Cheat Sheets by gauravpandey44