Docker 将已经部署的容器设置为自动启动

场景需求:假如服务器断电恢复Docker服务虽然设置了开机自启,但是容器是不会自启动的。

解决方案: 1. 手动重启断电前运行的容器;

                  2. 将容器设置成和Docker同样的开机自启.

手动重启实现过程(以ngixn为例):

  • 拉取镜像nginx镜像:
    • [root@localhost ~]# docker pull nginx
      Using default tag: latest
      latest: Pulling from library/nginx
      a2abf6c4d29d: Already exists 
      a9edb18cadd1: Pull complete 
      589b7251471a: Pull complete 
      186b1aaa4aa6: Pull complete 
      b4df32aa5a72: Pull complete 
      a0bcbecc962e: Pull complete 
      Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
      Status: Downloaded newer image for nginx:latest
      docker.io/library/nginx:latest
  • 查看拉取的nginx镜像:
    • [root@localhost ~]# docker images
      REPOSITORY TAG IMAGE ID CREATED SIZE
      nginx latest 605c77e624dd 2 years ago 141MB
  • 运行nginx容器
    • [root@localhost ~]# docker run -d -p 8080:80 --name nginx nginx:latest 
      e933bdfd2f46d16a311512b025e73873b8a9bdc6ebc53eb734c8f4799b0779b4
      
      [root@localhost ~]# docker ps
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
      e933bdfd2f46 nginx:latest "/docker-entrypoint.…" 8 seconds ago Up 7 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx
  • 重启Docker后观察运行的nginx容器已经消失了:
    • [root@localhost ~]# systemctl restart docker.service
      
      [root@localhost ~]# docker ps
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  • 手动重启nginx容器:
    • [root@localhost ~]# docker ps -a
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
      e933bdfd2f46 nginx:latest "/docker-entrypoint.…" 2 minutes ago Exited (0) 55 seconds ago nginx
      
      [root@localhost ~]# docker restart nginx 
      nginx
      
      [root@localhost ~]# docker ps
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
      e933bdfd2f46 nginx:latest "/docker-entrypoint.…" 2 minutes ago Up 3 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx

将容器设置成自启动实现过程(以ngixn为例):

  • 查看当前容器的重启策略:
    • [root@localhost ~]# docker inspect -f "{{ .HostConfig.RestartPolicy.Name }}" nginx
      no
    • no 默认值,不重启
  • 使用
    docker update

    修改重启策略:

    • [root@localhost ~]# docker update --restart always nginx
      nginx
  • 再次查看重启策略:
    • [root@localhost ~]# docker inspect -f "{{ .HostConfig.RestartPolicy.Name }}" nginx
      always
  • 命令的参数解释:
    • docker update

      : 这是 Docker 命令,用于更新一个或多个容器的配置。

    • --restart always

      : 这是重启策略选项,表示无论容器以何种方式停止(包括正常停止和异常停止),都将自动重启该容器。常见的重启策略包括:

      • no

        : 不自动重启容器(默认值)。

      • on-failure

        : 当容器以非零退出代码停止时自动重启。

      • always

        : 无论容器以何种方式停止都自动重启。

      • unless-stopped

        : 除非容器被手动停止,否则总是自动重启。

    • nginx

      : 这是容器的名称或ID。这个命令将会更新名为

      nginx

      的容器的重启策略。

  • 重启Docker,然后在观察容器已经重新自启动:
  • [root@localhost ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    e933bdfd2f46 nginx:latest "/docker-entrypoint.…" 27 minutes ago Up 5 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx
    
    [root@localhost ~]# systemctl restart docker.service
    
    [root@localhost ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    e933bdfd2f46 nginx:latest "/docker-entrypoint.…" 27 minutes ago Up 1 second 0.0.0.0:8080->80/tcp, :::8080->80/tcp

nginx创建容器的时候,加参数使容器自启动:

    • [root@localhost ~]# docker run -d --name nginx --restart always nginx
      48961c5ce8acede9f8ea69feee8e3c876929f99d741b7c5862f26454cccc509c
  • 重启Docker
    • [root@localhost ~]# systemctl restart docker.service
  • 再次查看,nginx容器依然运行,不会受Docker重启的影响:
    • [root@localhost ~]# docker ps
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
      48961c5ce8ac nginx "/docker-entrypoint.…" 31 seconds ago Up 11 seconds 80/tcp nginx

 


云野 » Docker 将已经部署的容器设置为自动启动

发表回复