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 ## Advanced features
152 {: .-three-column}
153
154 ### Labels
155
156 ```yaml
157 services:
158   web:
159     labels:
160       com.example.description: "Accounting web app"
161 ```
162
163 ### DNS servers
164
165 ```yaml
166 services:
167   web:
168     dns: 8.8.8.8
169     dns:
170       - 8.8.8.8
171       - 8.8.4.4
172 ```
173
174 ### Devices
175
176 ```yaml
177 services:
178   web:
179     devices:
180     - "/dev/ttyUSB0:/dev/ttyUSB0"
181 ```
182
183 ### External links
184
185 ```yaml
186 services:
187   web:
188     external_links:
189       - redis_1
190       - project_db_1:mysql
191 ```
192
193 ### Hosts
194
195 ```yaml
196 services:
197   web:
198     extra_hosts:
199       - "somehost:192.168.1.100"
200 ```
201
202 ### Network
203
204 ```yaml
205 # creates a custom network called `frontend`
206 networks:
207   frontend:
208 ```
209
210 ### External network
211
212 ```yaml
213 # join a pre-existing network
214 networks:
215   default:
216     external:
217       name: frontend
218 ```
219
220 ### Volume
221
222 ```yaml
223 # Mount host paths or named volumes, specified as sub-options to a service
224   db:
225     image: postgres:latest
226     volumes:
227       - "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
228       - "dbdata:/var/lib/postgresql/data"
229
230 volumes:
231   dbdata:
232 ```