Docker 容器不会以 systemctl 启动
Docker Container won't start with systemctl
我刚开始查看 coreos
我已经尝试设置 redis 和 mysql docker 实例,但收效甚微。
我最初希望开箱即用,但事实并非如此。
因此,基于 coreos 网站上的这个 documentation on systemd,我决定尝试这个来启动我的 docker instace
cd /etc/systemd/system
sudo systemctl enable redis.service
$ sudo systemctl start redis.service
这没有用。
我使用 docker 事件来跟踪它的初始化
docker event &
我不确定我可能错过了什么..
这是我的云配置文件
#cloud-config
hostname: user1
# include one or more SSH public keys
ssh_authorized_keys:
- ssh-rsa....
users:
- name: user2
passwd: temp123
groups:
- sudo
- docker
ssh-authorized-keys:
- ssh-rsa....
coreos:
etcd2:
#generate a new token for each unique cluster from https://discovery.etcd.io/new?size=#{number_instances}
discovery: https://discovery.etcd.io/fdadfadjskd546887878kfksdjfds
# multi-region and multi-cloud deployments need to use 1.1.1.1
advertise-client-urls: http://1.1.1.1:2379
initial-advertise-peer-urls: http://2.2.2.2:2380
# listen on both the official ports and the legacy ports
# legacy ports can be omitted if your application doesn't depend on them
listen-client-urls: http://0.0.0.0:2379,http://0.0.0.0:4001
listen-peer-urls: http://2.2.2.2:2380,http://2.2.2.2:7001
fleet:
public-ip: 1.1.1.1
flannel:
interface: 3.3.3.51/23
units:
- name: systemd-networkd
command: stop
- name: 00-static.network
runtime: true
content: "[Match]\n\
Name=ens19\n\
[Network]\n\
Address=3.3.3.3/23\n\
Gateway=3.3.3.255\n\
DNS=8.8.8.8\n\
DNS=8.8.4.4 \n"
- name: systemd-networkd
command: start
- name: etcd2.service
command: start
- name: fleet.service
command: start
- name: redis.service
command: start
enable: true
content: "[Unit]\n\
Description=Redis Server Docker Container\n\
After=docker.service\n\
Requires=docker.service\n\
[Service]\n\
TimeoutStartSec=0 \n\
EnvironmentFile=/etc/environment\n\
ExecStartPre=-/usr/bin/docker kill %p\n\
ExecStartPre=-/usr/bin/docker rm %p\n\
ExecStartPre=/usr/bin/docker pull redis:latest \n\
ExecStart=/usr/bin/docker run --name=redis --detach=true --publish=6379:6379 redis \n\
ExecStop=/usr/bin/docker stop redis \n\
[Install] \n\
WantedBy=multi-user.target \n"
- name: mysql.service
command: start
enable: true
content: "[Unit]\n\
Description=MySQL Server Docker Container\n\
After=docker.service\n\
Requires=docker.service\n\
[Service]\n\
TimeoutStartSec=0 \n\
EnvironmentFile=/etc/environment\n\
ExecStartPre=-/usr/bin/docker kill %p\n\
ExecStartPre=-/usr/bin/docker rm %p\n\
ExecStartPre=/usr/bin/docker pull mysql:latest \n\
ExecStart=/usr/bin/docker run --name=mysql --env MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} --env MYSQL_USER=${MYSQL_USER} --env MYSQL_PASSWORD=${MYSQL_PASSWORD} --env MYSQL_DATABASE=${MYSQL_DATABASE} --detach --publish 3306:3306 --volume=/path/to/dumps/:/dumps/ mysql\n\
ExecStop=/usr/bin/docker stop mysql\n\
[Install] \n\
WantedBy=multi-user.target \n"
write_files:
- path: /etc/environment
permissions: 0644
content: "\nMYSQL_USER='user1'\n\
MYSQL_DATABASE='db1'\n\
MYSQL_CONTAINER_NAME='mysql'\n\
MYSQL_ROOT_PASSWORD=$(cat /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | sed 1q) \n"
您是处于分离模式的 运行 redis 容器,因此 docker 实用程序启动容器然后退出。从 systemd 的角度来看,这看起来像是受控进程退出,因此 systemd 执行 ExecStop
脚本,在您的情况下,该脚本停止了 redis 容器。
您需要保留进程 运行,这样 systemd 就不会尝试停止或重启您的容器。实现此目的的一种方法是删除 --detach
标志。您还可以使用 KillMode=none
这样 systemd 就不会将 SIGTERM 发送到 docker 实用程序,而是只会执行 ExecStop
。
[Unit]
Requires=docker.service
After=docker.service
[Service]
TimeoutStartSec=0
KillMode=none
Restart=always
RestartSec=5s
ExecStartPre=-/usr/bin/docker kill %p
ExecStartPre=-/usr/bin/docker rm -v %p
ExecStart=/usr/bin/docker --name=redis --publish=6379:6379 redis
ExecStop=/usr/bin/docker stop %p
ExecStopPost=-/usr/bin/docker stop %p
我刚开始查看 coreos
我已经尝试设置 redis 和 mysql docker 实例,但收效甚微。
我最初希望开箱即用,但事实并非如此。
因此,基于 coreos 网站上的这个 documentation on systemd,我决定尝试这个来启动我的 docker instace
cd /etc/systemd/system
sudo systemctl enable redis.service
$ sudo systemctl start redis.service
这没有用。
我使用 docker 事件来跟踪它的初始化
docker event &
我不确定我可能错过了什么..
这是我的云配置文件
#cloud-config
hostname: user1
# include one or more SSH public keys
ssh_authorized_keys:
- ssh-rsa....
users:
- name: user2
passwd: temp123
groups:
- sudo
- docker
ssh-authorized-keys:
- ssh-rsa....
coreos:
etcd2:
#generate a new token for each unique cluster from https://discovery.etcd.io/new?size=#{number_instances}
discovery: https://discovery.etcd.io/fdadfadjskd546887878kfksdjfds
# multi-region and multi-cloud deployments need to use 1.1.1.1
advertise-client-urls: http://1.1.1.1:2379
initial-advertise-peer-urls: http://2.2.2.2:2380
# listen on both the official ports and the legacy ports
# legacy ports can be omitted if your application doesn't depend on them
listen-client-urls: http://0.0.0.0:2379,http://0.0.0.0:4001
listen-peer-urls: http://2.2.2.2:2380,http://2.2.2.2:7001
fleet:
public-ip: 1.1.1.1
flannel:
interface: 3.3.3.51/23
units:
- name: systemd-networkd
command: stop
- name: 00-static.network
runtime: true
content: "[Match]\n\
Name=ens19\n\
[Network]\n\
Address=3.3.3.3/23\n\
Gateway=3.3.3.255\n\
DNS=8.8.8.8\n\
DNS=8.8.4.4 \n"
- name: systemd-networkd
command: start
- name: etcd2.service
command: start
- name: fleet.service
command: start
- name: redis.service
command: start
enable: true
content: "[Unit]\n\
Description=Redis Server Docker Container\n\
After=docker.service\n\
Requires=docker.service\n\
[Service]\n\
TimeoutStartSec=0 \n\
EnvironmentFile=/etc/environment\n\
ExecStartPre=-/usr/bin/docker kill %p\n\
ExecStartPre=-/usr/bin/docker rm %p\n\
ExecStartPre=/usr/bin/docker pull redis:latest \n\
ExecStart=/usr/bin/docker run --name=redis --detach=true --publish=6379:6379 redis \n\
ExecStop=/usr/bin/docker stop redis \n\
[Install] \n\
WantedBy=multi-user.target \n"
- name: mysql.service
command: start
enable: true
content: "[Unit]\n\
Description=MySQL Server Docker Container\n\
After=docker.service\n\
Requires=docker.service\n\
[Service]\n\
TimeoutStartSec=0 \n\
EnvironmentFile=/etc/environment\n\
ExecStartPre=-/usr/bin/docker kill %p\n\
ExecStartPre=-/usr/bin/docker rm %p\n\
ExecStartPre=/usr/bin/docker pull mysql:latest \n\
ExecStart=/usr/bin/docker run --name=mysql --env MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} --env MYSQL_USER=${MYSQL_USER} --env MYSQL_PASSWORD=${MYSQL_PASSWORD} --env MYSQL_DATABASE=${MYSQL_DATABASE} --detach --publish 3306:3306 --volume=/path/to/dumps/:/dumps/ mysql\n\
ExecStop=/usr/bin/docker stop mysql\n\
[Install] \n\
WantedBy=multi-user.target \n"
write_files:
- path: /etc/environment
permissions: 0644
content: "\nMYSQL_USER='user1'\n\
MYSQL_DATABASE='db1'\n\
MYSQL_CONTAINER_NAME='mysql'\n\
MYSQL_ROOT_PASSWORD=$(cat /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | sed 1q) \n"
您是处于分离模式的 运行 redis 容器,因此 docker 实用程序启动容器然后退出。从 systemd 的角度来看,这看起来像是受控进程退出,因此 systemd 执行 ExecStop
脚本,在您的情况下,该脚本停止了 redis 容器。
您需要保留进程 运行,这样 systemd 就不会尝试停止或重启您的容器。实现此目的的一种方法是删除 --detach
标志。您还可以使用 KillMode=none
这样 systemd 就不会将 SIGTERM 发送到 docker 实用程序,而是只会执行 ExecStop
。
[Unit]
Requires=docker.service
After=docker.service
[Service]
TimeoutStartSec=0
KillMode=none
Restart=always
RestartSec=5s
ExecStartPre=-/usr/bin/docker kill %p
ExecStartPre=-/usr/bin/docker rm -v %p
ExecStart=/usr/bin/docker --name=redis --publish=6379:6379 redis
ExecStop=/usr/bin/docker stop %p
ExecStopPost=-/usr/bin/docker stop %p