OSDN Git Service

Regular updates
[twpd/master.git] / docker-compose.md
1 ---
2 title: docker-compose
3 category: Devops
4 layout: 2017/sheet
5 prism_languages: [yaml]
6 weight: -1
7 updated: 2020-01-01
8 ---
9
10 ### Basic example
11
12 ```yaml
13 # docker-compose.yml
14 version: '2'
15
16 services:
17   web:
18     build:
19     # build from Dockerfile
20       context: ./Path
21       dockerfile: Dockerfile
22     ports:
23      - "5000:5000"
24     volumes:
25      - .:/code
26   redis:
27     image: redis
28 ```
29
30 ### Commands
31
32 ```sh
33 docker-compose start
34 docker-compose stop
35 ```
36
37 ```sh
38 docker-compose pause
39 docker-compose unpause
40 ```
41
42 ```sh
43 docker-compose ps
44 docker-compose up
45 docker-compose down
46 ```
47
48 ## Reference
49 {: .-three-column}
50
51 ### Building
52
53 ```yaml
54 web:
55   # build from Dockerfile
56   build: .
57   args:     # Add build arguments
58     APP_HOME: app
59 ```
60
61 ```yaml
62   # build from custom Dockerfile
63   build:
64     context: ./dir
65     dockerfile: Dockerfile.dev
66 ```
67
68 ```yaml
69   # build from image
70   image: ubuntu
71   image: ubuntu:14.04
72   image: tutum/influxdb
73   image: example-registry:4000/postgresql
74   image: a4bc65fd
75 ```
76
77 ### Ports
78
79 ```yaml
80   ports:
81     - "3000"
82     - "8000:80"  # host:container
83 ```
84
85 ```yaml
86   # expose ports to linked services (not to host)
87   expose: ["3000"]
88 ```
89
90 ### Commands
91
92 ```yaml
93   # command to execute
94   command: bundle exec thin -p 3000
95   command: [bundle, exec, thin, -p, 3000]
96 ```
97
98 ```yaml
99   # override the entrypoint
100   entrypoint: /app/start.sh
101   entrypoint: [php, -d, vendor/bin/phpunit]
102 ```
103
104 ### Environment variables
105
106 ```yaml
107   # environment vars
108   environment:
109     RACK_ENV: development
110   environment:
111     - RACK_ENV=development
112 ```
113
114 ```yaml
115   # environment vars from file
116   env_file: .env
117   env_file: [.env, .development.env]
118 ```
119
120 ### Dependencies
121
122 ```yaml
123   # makes the `db` service available as the hostname `database`
124   # (implies depends_on)
125   links:
126     - db:database
127     - redis
128 ```
129
130 ```yaml
131   # make sure `db` is alive before starting
132   depends_on:
133     - db
134 ```
135
136 ```yaml
137   # make sure `db` is healty before starting
138   # and db-init completed without failure
139   depends_on:
140     db:
141       condition: service_healthy
142     db-init:
143       condition: service_completed_successfully
144 ```
145
146 ### Other options
147
148 ```yaml
149   # make this service extend another
150   extends:
151     file: common.yml  # optional
152     service: webapp
153 ```
154
155 ```yaml
156   volumes:
157     - /var/lib/mysql
158     - ./_data:/var/lib/mysql
159 ```
160
161 ```yaml
162   # automatically restart container
163   restart: unless-stopped
164   # always, on-failure, no (default)
165 ```
166
167 ## Advanced features
168 {: .-three-column}
169
170 ### Labels
171
172 ```yaml
173 services:
174   web:
175     labels:
176       com.example.description: "Accounting web app"
177 ```
178
179 ### DNS servers
180
181 ```yaml
182 services:
183   web:
184     dns: 8.8.8.8
185     dns:
186       - 8.8.8.8
187       - 8.8.4.4
188 ```
189
190 ### Devices
191
192 ```yaml
193 services:
194   web:
195     devices:
196     - "/dev/ttyUSB0:/dev/ttyUSB0"
197 ```
198
199 ### External links
200
201 ```yaml
202 services:
203   web:
204     external_links:
205       - redis_1
206       - project_db_1:mysql
207 ```
208
209 ### Healthcheck
210
211 ```yaml
212     # declare service healthy when `test` command succeed
213     healthcheck:
214       test: ["CMD", "curl", "-f", "http://localhost"]
215       interval: 1m30s
216       timeout: 10s
217       retries: 3
218       start_period: 40s
219 ```
220
221 ### Hosts
222
223 ```yaml
224 services:
225   web:
226     extra_hosts:
227       - "somehost:192.168.1.100"
228 ```
229
230 ### Network
231
232 ```yaml
233 # creates a custom network called `frontend`
234 networks:
235   frontend:
236 ```
237
238 ### External network
239
240 ```yaml
241 # join a pre-existing network
242 networks:
243   default:
244     external:
245       name: frontend
246 ```
247
248 ### Volume
249
250 ```yaml
251 # mount host paths or named volumes, specified as sub-options to a service
252   db:
253     image: postgres:latest
254     volumes:
255       - "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
256       - "dbdata:/var/lib/postgresql/data"
257
258 volumes:
259   dbdata:
260 ```
261
262 ### User
263
264 ```yaml
265 # specifying user
266 user: root
267 ```
268
269 ```yaml
270 # specifying both user and group with ids
271 user: 0:0
272 ```