How do I use docker and docker-compose

Goal

Build Bind service with Docker and auto startup it using docker-compose on Ubuntu

Environment

  • Ubuntu 14.04.5 LTS Server
  • docker-compose 1.11.2
  • docker 17.03.0-ce

About docker-compose

Create docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mkdir -p ~/bind
cat >> ~/bind/docker-compose.yml << EOF
pichuang-dns:
image: sameersbn/bind:latest
dns: 127.0.0.1
environment:
- ROOT_PASSWORD=pichuang
ports:
- 10000:10000
- 53:53/udp
volumes:
- /srv/docker/dns:/data
EOF

Test

1
2
docker-compose -f ~/bind/docker-compose.yml up -d
docker-compose -f ~/bind/docker-compose.yml ps

About Startup cript

Create Daemon

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
cat >> /etc/init.d/bind << EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: bind
# Required-Start: $docker
# Required-Stop: $docker
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Docker Services
### END INIT INFO
set -e
PROJECT_NAME=bind
YAMLFILE=/home/pichuang/docker-compose.yml
DOCKER_COMPOSE_BIN=$(whereis docker-compose|awk '{print $2}')
OPTS="-f $YAMLFILE -p $PROJECT_NAME"
UPOPTS="-d"
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting Docker Compose" "bind" || true
$DOCKER_COMPOSE_BIN $OPTS up $UPOPTS
;;
stop)
log_daemon_msg "Stopping Docker Compose" "bind" || true
$DOCKER_COMPOSE_BIN $OPTS stop
;;
reload)
log_daemon_msg "Reloading Docker Compose" "bind" || true
$DOCKER_COMPOSE_BIN $OPTS up $UPOPTS
;;
restart)
$DOCKER_COMPOSE_BIN $OPTS stop
$DOCKER_COMPOSE_BIN $OPTS up $UPOPTS
;;
status)
$DOCKER_COMPOSE_BIN $OPTS ps
;;
*)
log_action_msg "Usage: /etc/init.d/bind {start|stop|restart|reload|status}" || true
exit 1
;;
esac
exit 0
EOF
chmod +x /etc/init.d/bind

Update rcx.d

1
2
cd /etc/init.d/
update-rc.d -f bind defaults 80 20

Test

1
2
3
4
5
6
7
reboot
...
service bind status
service bind stop
service bind start
service bind reload
service bind restart