-
Notifications
You must be signed in to change notification settings - Fork 0
/
Docker.sh
320 lines (259 loc) · 9.71 KB
/
Docker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Docker - platform for containerization
# version: 19.03
# Image - read-only template with instructions for creating a docker container.
# Container - runnable instance of an image.
# After installation need to add user to docker group
sudo usermod -aG docker $(whoami)
################################################################################
docker
info # show details about installed docker, images, containers
inspect <name|id> # low-level information in json format
build -t <name>:<tag> <src_path> # build new image with version <tag>
run <image> [<cmd arg...>] # run new container
# <cmd args...> - to override CMD or add ENTRYPOINT arguments
# <image> can be <usename>/<repository>:<tag> to pull from repo and run
-p <foreign>:<inner> # map ports
--network bridge|host|overlay|macvlan|none # connect to network
--name <name> # specify name for new container
-d # detached mode (run in background)
-i # interactive
-t # allocate pseudo tty
-a stdin|stdout|stder # attach to stdin, stdout and/or stderr
--rm # automatically remove when exits
-v|--volume <volume|host_path>:<mount_path> # attach storage
--mount <options> # attach storage (more explicit syntax)
# options: comma-separated key=value pairs
type=bind|volume|tmpfs
source=<host_path>
destination=<mount_path>
readonly=true
bind-propagation=rprivate|private|rslave|slave|rshared|shared
# whether or not sub-mounts can be propagated to replicas
# r - recursive
ps # list running containers
logs <container> # fetch logs of a container
-f|--follow # follow log output
--tail <n> # show only <n> last lines
-t|--timestamps # show timestamps
--since <time> # show logs since <time> (e.g. 2013-01-02T13:23:37)
# or relative (e.g. 42m for 42 minutes)
--details # show extra deails provided to logs (env vars)
start <container> # run stopper container
-i # interactive
-a # attach stdout and stderr and forward signals
exec <container> <cmd arg...> # execute in running container
-d # detached mode
-w <path> # set working directory
-i # interactive
-t # allocate pseudo tty
-e <key>=<val> # set environment variable
attach <container> # connect to container's process
# press ctrl+p, ctrl+q to detach
stop <container> # gracefully stop running container
kill <container> # force stop
image ls # list images
-a # all (default hides intermediate images)
image rm <image> # remove image
image prune # remove all dangling images
-f # without prompt
container ls # list containers
-a # all (default hides stopped)
container rm <container> # remove container
-v # with associated volumes
container prune # remove all stopped containers
-f
volume create <volume> # create storage
# volumes stored in /var/lib/docker/volumes/<volume-name>
volume ls # list volumes
volume rm <volume> # remove volume
# copy files from host to container
cp <file_path> <container>:/<path>
# copy file into dir or rewrite file
cp <dir_path> <container>:/<dir_path>
# if exists: create subdirectory and copy contents into it
# if does not exist: create dir and copy contents into it
cp <dir_path>/. <container>:/<dir_path>
# copy dir contents into dir
################################################################################
# Dockerfile - config defining the steps needed to create the image and run it.
# Each instruction in a Dockerfile creates a layer in the image. When you change
# the Dockerfile and rebuild the image, only those layers which have changed
# are rebuilt.
FROM <parent_image>:<version> # specify parent layer
USER <user>[:<group>] # set user to use when running the image and
# for all following RUN, CMD, ENTRYPOINT
RUN <shellstr> # run command (shell form)
RUN [<cmdarg>...] # exec form
ARG <name>=<default> # variable that can be passed at build time
# using `--build-arg <name>=<value>` and can be used inside
# Dockerfile as `$name`
ENV <name> <value> # environment variable
EXPOSE <port>... # inform that container listens port(s) at runtime
WORKDIR /<path> # set working directory
COPY <path> /<path> # copy files into container
# The directory itself is not copied, just its contents.
ENTRYPOINT [<cmd> <args>...] # allows to configure a container that will
# run as an executable
CMD <shellstr> # default command or additional arguments for ENRTYPOINT
CMD [<cmd> <arg>...] # exec form
################################################################################
# Dockerfile example:
FROM debian:stretch
RUN apt-get update -y
RUN apt-get install -y locales
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
locale-gen
RUN locale-gen en_US.UTF-8
ENV LC_ALL en_US.UTF-8
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common
RUN curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | apt-key add -
RUN apt-key fingerprint 0EBFCD88
RUN add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
$(lsb_release -cs) \
stable"
RUN apt-get update -y
RUN apt-get install -y docker-ce
WORKDIR /app
COPY ./requirements.txt ./
RUN pip3 install -r ./requirements.txt
COPY ./* ./
CMD pytest -sv ./
################################################################################
# docker-compose
# version: 1.24
# Compose is a tool for defining and running multi-container Docker applications.
# Use a YAML file to configure your application’s services, then, with a single
# command, create and start all the services.
# Features:
# Multiple isolated environments on a single host
# Preserve volume data when containers are created
# Only recreate containers that have changed
# Variables and moving a composition between environments
docker-compose
-f|--file <path> # alternative compose file
# default: ./docker-compose.yml
--project-directory <path> # alternative working directory
# default: path to the compose file
-p|--project-name <name> # directory name by default
--log-level DEBUG|INFO|WARNING|ERROR|CRITICAL
build # build or rebuild services
--compress # gzip compress the build context
--force-rm # remove intermediate containers
-m|--memory <n> # set memory limit
--build-arg <key>=<val> # build time variables
run <service> [<cmd args...>] # start service and execute command
-d|--detach # detached mode
--name <name> # assign name to the container
--no-deps # don't start linked services
-e <key>=<val> # set environment variable
-w|--workdir # set working directory
-v|--volume <volume> # bind mount a volume
--rm # remove container after run
up [<service>...] # build, (re)create and start services
-d # detached mode
--no-deps # don't start linked services
--build # build images before starting containers
--scale <service>=<n> # scale <service> to <n> instances
down # stop and remove containers
-v|--volumes
ps # list running containers
--services
logs [<service>...]
-f|--follow # follow log output
--tail <n> # show <n> last lines
-t|--timestamps # show timestamps
top [<service>...] # display the running processes
exec <service> <cmd args...> # execute command in running service
-d|--detach # detached mode
--index <n> # index of the container if there are multiple instances
-e|--env <key>=<val> # set environment variable
-w # workdir for this command
pause [<service>...]
unpause [<service>...]
start [<service>...] # start existing container(s)
stop [<service>...]
kill [<service>...] # send a signal to the service(s) (default: SIGKILL)
-s <signal>
restart [<service>...]
################################################################################
# docker-compose.yml example:
version: "3.7"
services:
webapp: # service name
build: ./ # path to Dockerfile
container_name: web
ports: # expose ports <host>:<container>
- 8000:8000
volumes:
- ./:/app/ # mount project directory to /app/ inside container
environment:
- APP_ENV=dev
command: python manage.py runserver # override the default CMD
depends_on: # specify service's dependencies
- redis
dns:
- 8.8.8.8
env_file:
- .env
restart: on-failure # auto restart policy
# available: no, always, on-failure, unless-stopped
networks: # networks to join (referencing entries of top-level networks)
- frontend
stdin_open: true # to make an interactive shell available
tty: true
db:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args: # build time arguments
key: val
entrypoint: ./start_db.sh # override the default ENTRYPOINT
network_mode: bridge
redis:
image: redis:alpine
networks:
frontend:
driver: bridge
name: frontend
my_volume:
driver: local
driver_opts:
type: none
device: ./path/to/host/dir
o: bind
################################################################################
# Dump and restore Postgres DB
# Manually
$ docker exec -ti <container> pg_dumpall -c -U <pg-user> <file>.sql
$ cat <file>.sql | docker exec -i <container> psql -U <pg-user>
# Backup automatically within a given time interval
services:
pgbackups:
container_name: Backup
image: prodrigestivill/postgres-backup-local
restart: always
volumes:
- ./backup:/backups
links:
- db:db
depends_on:
- db
environment:
- POSTGRES_HOST=db
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_EXTRA_OPTS=-Z9 --schema=public --blobs
- SCHEDULE=@every 0h30m00s
- BACKUP_KEEP_DAYS=7
- BACKUP_KEEP_WEEKS=4
- BACKUP_KEEP_MONTHS=6
- HEALTHCHECK_PORT=81