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 ### Other options
137
138 ```yaml
139   # make this service extend another
140   extends:
141     file: common.yml  # optional
142     service: webapp
143 ```
144
145 ```yaml
146   volumes:
147     - /var/lib/mysql
148     - ./_data:/var/lib/mysql
149 ```
150
151 ```yaml
152   # automatically restart container
153   restart: unless-stopped
154   # always, on-failure, no (default)
155 ```
156
157 ## Advanced features
158 {: .-three-column}
159
160 ### Labels
161
162 ```yaml
163 services:
164   web:
165     labels:
166       com.example.description: "Accounting web app"
167 ```
168
169 ### DNS servers
170
171 ```yaml
172 services:
173   web:
174     dns: 8.8.8.8
175     dns:
176       - 8.8.8.8
177       - 8.8.4.4
178 ```
179
180 ### Devices
181
182 ```yaml
183 services:
184   web:
185     devices:
186     - "/dev/ttyUSB0:/dev/ttyUSB0"
187 ```
188
189 ### External links
190
191 ```yaml
192 services:
193   web:
194     external_links:
195       - redis_1
196       - project_db_1:mysql
197 ```
198
199 ### Hosts
200
201 ```yaml
202 services:
203   web:
204     extra_hosts:
205       - "somehost:192.168.1.100"
206 ```
207
208 ### Network
209
210 ```yaml
211 # creates a custom network called `frontend`
212 networks:
213   frontend:
214 ```
215
216 ### External network
217
218 ```yaml
219 # join a pre-existing network
220 networks:
221   default:
222     external:
223       name: frontend
224 ```
225
226 ### Volume
227
228 ```yaml
229 # mount host paths or named volumes, specified as sub-options to a service
230   db:
231     image: postgres:latest
232     volumes:
233       - "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
234       - "dbdata:/var/lib/postgresql/data"
235
236 volumes:
237   dbdata:
238 ```
239
240 ### User
241
242 ```yaml
243 # specifying user
244 user: root
245 ```
246
247 ```yaml
248 # specifying both user and group with ids
249 user: 0:0
250 ```