沙滩星空的博客沙滩星空的博客

设置Docker和指定容器开机启动

Docker正常以socket守护进程(daemon)形式运行在系统后台。
Docker daemon socket at

unix:///var/run/docker.sock

设置容器开机启动分为三个步骤:

1.设置Docker开机启动:

systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

2.启动指定容器:

docker start <your_container_name or containerid>

3.设置指定容器开机启动:

docker update --restart=always <name>

禁止开机启动Docker

systemctl disable docker
Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.

不想容器开启启动:

docker update --restart=no <name>

例:设置开机启动MySql PHP NGINX
我的运行环境是Vagrant的CentOS7虚拟机。
默认登录用户不是root系统管理员,无法直接运行docker.所以加了个sudo命令。
后面su root,输入密码,切换到系统管理员角色后,就不需要sudo了。

[vagrant@localhost ~]$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied
[vagrant@localhost ~]$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[vagrant@localhost ~]$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
39f8d2b40665        nginx               "nginx -g 'daemon of…"   37 hours ago        Exited (0) 10 hours ago                       cm_nginx
f99b6692e5b9        php:7.2-fpm         "docker-php-entrypoi…"   37 hours ago        Exited (0) 10 hours ago                       cm_phpfpm72
6630c2f46607        mysql:5.7           "docker-entrypoint.s…"   41 hours ago        Exited (0) 10 hours ago                       cm_mysql57
[vagrant@localhost ~]$ su
Password:
[root@localhost vagrant]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# systemctl enable docker
[root@localhost ~]# docker start cm_mysql57
cm_mysql57
[root@localhost ~]# docker start cm_phpfpm72
cm_phpfpm72
[root@localhost ~]# docker start cm_nginx
cm_nginx
[root@localhost ~]# docker update --restart=always cm_mysql57
cm_mysql57
[root@localhost ~]# docker update --restart=always cm_phpfpm72
cm_phpfpm72
[root@localhost ~]# docker update --restart=always cm_nginx
cm_nginx
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
39f8d2b40665        nginx               "nginx -g 'daemon of…"   37 hours ago        Up About a minute   0.0.0.0:80->80/tcp                  cm_nginx
f99b6692e5b9        php:7.2-fpm         "docker-php-entrypoi…"   37 hours ago        Up About a minute   0.0.0.0:9000->9000/tcp              cm_phpfpm72
6630c2f46607        mysql:5.7           "docker-entrypoint.s…"   42 hours ago        Up About a minute   0.0.0.0:3306->3306/tcp, 33060/tcp   cm_mysql57


Windows下重启Centos7虚拟机

vagrant reload

重启后,发现本地PHP网页仍不能访问。但每个容器确实已启动成功。
可能是php-fpm开机启动有问题。重启下php-fpm容器。发现PHP网页恢复正常访问。
或者重启docker亦可:

systemctl restart docker

对于多容器依赖式启动,比如 MYSQL -> PHP-FPM -> NGINX. 还是使用docker-compose靠谱

[vagrant@localhost ~]$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
39f8d2b40665        nginx               "nginx -g 'daemon of…"   38 hours ago        Up 40 seconds       0.0.0.0:80->80/tcp                  cm_nginx
f99b6692e5b9        php:7.2-fpm         "docker-php-entrypoi…"   38 hours ago        Up 41 seconds       0.0.0.0:9000->9000/tcp              cm_phpfpm72
6630c2f46607        mysql:5.7           "docker-entrypoint.s…"   42 hours ago        Up 42 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   cm_mysql57
[vagrant@localhost ~]$ sudo docker restart cm_phpfpm72
cm_phpfpm72

如何开机启动docker容器?如何进入docker容器? https://newsn.net/say/docker-exec.html
未经允许不得转载:沙滩星空的博客 » 设置Docker和指定容器开机启动

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址