└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Docker-Compose cheat sheet 2 | A cheet sheat to create you docker-compose.yml 3 | 4 | #cheatsheet #docker #docker-compose 5 | 6 | `version: '3.8'` 7 | 8 | ## Build the image 9 | 10 | ```yaml 11 | web: 12 | # build from Dockerfile 13 | build: . 14 | # alternative to use a folder with more than one Dockerfile: 15 | build: 16 | context: ./dir # path to Dockerfile 17 | dockerfile: Dockerfile.dev #name of the file 18 | target: dev # specify the target in a multi-stage build 19 | # build from image 20 | image: ubuntu 21 | image: ubuntu:14.04 22 | ``` 23 | ## Init 24 | Run the service as PID 1. Set this option to true to enable this feature for the service. 25 | ```yaml 26 | services: 27 | web: 28 | image: alpine:latest 29 | init: true 30 | ``` 31 | 32 | ## Expose port 33 | 34 | ```yaml 35 | ports: 36 | - "3000" 37 | - "8000:80" # guest:host 38 | # expose ports to linked services (not to host) 39 | expose: ["3000"] 40 | ``` 41 | 42 | ### Commands and Entrypoint 43 | 44 | ```yaml 45 | # command to execute 46 | command: bundle exec thin -p 3000 47 | command: [bundle, exec, thin, -p, 3000] 48 | 49 | # override the entrypoint 50 | entrypoint: /app/start.sh 51 | entrypoint: [php, -d, vendor/bin/phpunit] 52 | ``` 53 | 54 | ### Environment variables 55 | 56 | ```yaml 57 | # environment vars 58 | environment: 59 | RACK_ENV: development 60 | environment: 61 | - RACK_ENV=development 62 | 63 | # environment vars from file 64 | env_file: .env 65 | env_file: [.env, .development.env] 66 | ``` 67 | 68 | ## Health check 69 | 70 | ```yaml 71 | # declare service healthy when `test` command succeed 72 | healthcheck: 73 | test: ["CMD", "curl", "-f", "http://localhost"] 74 | interval: 1m30s 75 | timeout: 10s 76 | retries: 3 77 | start_period: 40s 78 | ``` 79 | 80 | ### Dependencies 81 | 82 | ```yaml 83 | # make sure `db` is alive before starting 84 | depends_on: 85 | - db 86 | 87 | 88 | # make sure `db` is healty before starting 89 | # and db-init completed without failure 90 | depends_on: 91 | db: 92 | condition: service_healthy 93 | db-init: 94 | condition: service_completed_successfully 95 | 96 | ``` 97 | 98 | ## Volumes 99 | 100 | ```yaml 101 | volumes: 102 | - /var/lib/mysql # anonimous volume 103 | - ./host_path:/docker_container_path # bind mount 104 | ``` 105 | ```yaml 106 | volumes: 107 | - type: bind 108 | source: ./host_path 109 | target: /docker_container_path 110 | - type: volume 111 | # no source. Override the previous volumes for a specific path 112 | target: /node_modules 113 | ``` 114 | 115 | ## Restart 116 | 117 | ```yaml 118 | # automatically restart container 119 | restart: unless-stopped 120 | # always, on-failure, no (default) 121 | ``` 122 | 123 | ## User 124 | 125 | ```yaml 126 | # specifying user 127 | user: root 128 | ``` 129 | 130 | ```yaml 131 | # specifying both user and group with ids 132 | user: 0:0 133 | ``` 134 | ## Commands 135 | ``` shell 136 | # Starts existing containers. 137 | docker-compose start 138 | 139 | # Stops running containers. 140 | docker-compose stop 141 | 142 | # Pauses running containers. 143 | docker-compose pause 144 | 145 | # Unpauses paused. 146 | docker-compose unpause 147 | 148 | # Lists containers. 149 | docker-compose ps 150 | 151 | # Builds, (re)creates, starts, and attaches to containers for a service. 152 | docker-compose up 153 | 154 | # Stops and removes containers. 155 | docker-compose down 156 | ``` 157 | 158 | 159 | Based on https://devhints.io/docker-compose 160 | --------------------------------------------------------------------------------