-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregistry.sh
executable file
·335 lines (280 loc) · 6.23 KB
/
registry.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/bash
REGISTRY_PORT="8080"
####################
# System variables #
####################
REDIS_IMAGE_NAME="redis"
REDIS_CONTAINER_NAME="registry-redis"
DOCKER_IMAGE_NAME="registry-docker"
DOCKER_CONTAINER_NAME="registry-docker"
DOCKER_PATH_TO_DOCKERFILE="registry-docker"
DOCKER_DATA_DIR="data"
DOCKER_CONF_DIR="registry-docker/conf"
DOCKER_DB_DIR="db"
NGINX_IMAGE_NAME="registry-nginx"
NGINX_CONTAINER_NAME="registry-nginx"
NGINX_PATH_TO_DOCKERFILE="registry-nginx"
PWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
###########
# Helpers #
###########
echo_delimiter()
{
echo "--------------------------------------"
echo
}
help_commands()
{
cat << EOF
Please, specify command:
help -- view full help message
build -- build registry images form Dockerfiles
run [PORT] -- run registry listening on PORT (8080 by default)
stop -- stop registry
attach -- attach to registry
logs -- see registry logs
rm -- remove registry containers
rmi -- remove registry images
EOF
}
help_message()
{
cat << EOF
#########
# Usage #
#########
Use this script to build and deploy secure and persistent
private Docker registry:
$0 build -- build docker containers for Redis, Docker and Nginx
$0 run [PORT] -- run registry on PORT (8080 by default)
After starting registry, you should assign symbolic name to
ip address of machine, which runs it, for example, by editing
/etc/hosts of client machines, or setup appropriate DNS records.
This name should match with symbolic name, stored in ssl certificate.
Default value is: private_registry.
After that, you should be able to work with your registry:
search, push and pull images. For example:
docker search private_registry:8080/some_container
docker pull private_registry:8080/another_container
Docker images, served by registry, stored in data/ directory.
Docker system database, used for indexing, stored in db/ directory.
################
# Certificates #
################
To use your own ssl keys, replace ssl/registry-docker.crt,
ssl/registry-docker.key with your signed by CA keys.
After that you should add certificate authority to system certificates
ON ALL MACHINES, which will use your private repo.
To do this, copy ssl/CA.crt or your own certificate to
/etc/ssl/certs/registry-docker.crt, then run sudo update-ca-certificates
and restart docker.
EOF
}
###################
# Redis utilities #
###################
redis_pull()
{
echo "Pulling Redis from docker.io...";
docker pull redis;
echo_delimiter;
}
redis_run()
{
echo "Starting Redis container...";
docker run -d --name ${REDIS_CONTAINER_NAME} redis;
echo_delimiter;
}
redis_stop()
{
echo "Stopping Redis container...";
docker stop ${REDIS_CONTAINER_NAME};
echo_delimiter;
}
redis_rm()
{
echo "Removing Redis container...";
docker rm ${REDIS_CONTAINER_NAME};
echo_delimiter;
}
redis_rmi()
{
echo "Removing Redis image...";
docker rmi ${REDIS_IMAGE_NAME};
echo_delimiter;
}
####################
# Docker utilities #
####################
docker_build()
{
echo "Building Docker registry image...";
docker build -t="${DOCKER_IMAGE_NAME}" ${PWD}/${DOCKER_PATH_TO_DOCKERFILE};
echo_delimiter;
}
docker_run()
{
echo "Starting Docker registry container...";
docker run \
-d \
--name ${DOCKER_CONTAINER_NAME} \
--link ${REDIS_CONTAINER_NAME}:redis \
-v ${PWD}/${DOCKER_DATA_DIR}:/registry/data \
-v ${PWD}/${DOCKER_CONF_DIR}:/registry/conf \
-v ${PWD}/${DOCKER_DB_DIR}:/registry/sqlitedb \
${DOCKER_IMAGE_NAME};
echo_delimiter;
}
docker_stop()
{
echo "Stopping Docker registry container...";
docker stop ${DOCKER_CONTAINER_NAME};
echo_delimiter;
}
docker_logs()
{
echo "Logging to Docker registry container..."
docker logs -f ${DOCKER_CONTAINER_NAME};
}
docker_attach()
{
echo "Attaching to Docker registry container..."
docker attach ${DOCKER_CONTAINER_NAME};
}
docker_rm()
{
echo "Removing Docker registry container...";
docker rm ${DOCKER_CONTAINER_NAME};
echo_delimiter;
}
docker_rmi()
{
echo "Removing Docker registry image...";
docker rmi ${DOCKER_IMAGE_NAME};
echo_delimiter;
}
###################
# Nginx utilities #
###################
nginx_build()
{
echo "Building Nginx image...";
docker build -t="${NGINX_IMAGE_NAME}" ${PWD}/${NGINX_PATH_TO_DOCKERFILE};
echo_delimiter;
}
nginx_run()
{
echo "Starting nginx container listening on port ${REGISTRY_PORT}...";
docker run \
-d \
--name ${NGINX_CONTAINER_NAME} \
--link ${DOCKER_CONTAINER_NAME}:registry-docker \
-v ${PWD}/ssl:/etc/nginx/ssl \
-p ${REGISTRY_PORT}:8080 \
${NGINX_IMAGE_NAME};
echo_delimiter;
}
nginx_stop()
{
echo "Stopping nginx container...";
docker stop ${NGINX_CONTAINER_NAME};
echo_delimiter;
}
nginx_rm()
{
echo "Removing nginx container...";
docker rm ${NGINX_CONTAINER_NAME};
echo_delimiter;
}
nginx_rmi()
{
echo "Removing nginx image...";
docker rmi ${NGINX_IMAGE_NAME};
echo_delimiter;
}
####################
# Common functions #
####################
all_run()
{
echo "Starting containers..."
redis_run;
docker_run;
nginx_run;
}
all_stop()
{
echo "Stopping containers..."
nginx_stop;
docker_stop;
redis_stop;
}
all_rm()
{
echo "Removing containers..."
nginx_rm;
docker_rm;
redis_rm;
}
all_rmi()
{
echo "Removing images..."
nginx_rmi;
docker_rmi;
redis_rmi;
}
###############
# Script body #
###############
if [[ "$1" == "help" ]]
then
help_message;
exit 0;
fi
if [[ "$1" == "build" ]]
then
echo "Building containers..."
redis_pull;
docker_build;
nginx_build;
exit 0;
fi
if [[ "$1" == "run" ]]
then
if [[ "$2" -gt "0" ]] && [[ "$2" -lt "65535" ]]
then
let REGISTRY_PORT="$2"
fi
all_run;
exit 0;
fi
if [[ "$1" == "stop" ]]
then
all_stop;
exit 0;
fi
if [[ "$1" == "logs" ]]
then
docker_logs;
exit 0;
fi
if [[ "$1" == "attach" ]]
then
docker_attach;
exit 0;
fi
if [[ "$1" == "rm" ]]
then
all_rm;
exit 0;
fi
if [[ "$1" == "rmi" ]]
then
all_rmi;
exit 0;
fi
###################
# Unknown command #
###################
help_commands;
exit 1;