docker参数--restart=always的作用

docker参数--restart=always的作用

 

创建容器时没有添加参数  --restart=always ,导致的后果是:当 Docker 重启时,容器未能自动启动。

现在要添加该参数怎么办呢,方法有二:

1、Docker 命令修改

docker container update --restart=always 容器名字
复制代码
操作实例如下:
[root@localhost mnt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
46cdfc60b7a6        nginx               "nginx -g 'daemon ..."   About a minute ago   Up 42 seconds       80/tcp              n3
79d55a734c26        nginx               "nginx -g 'daemon ..."   About a minute ago   Up 42 seconds       80/tcp              n2
f7b2206c019d        nginx               "nginx -g 'daemon ..."   About a minute ago   Up 46 seconds       80/tcp              n1
[root@localhost mnt]# docker container update --restart=always n1
n1
[root@localhost mnt]# systemctl restart docker 
[root@localhost mnt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
46cdfc60b7a6        nginx               "nginx -g 'daemon ..."   2 minutes ago       Exited (0) 5 seconds ago                       n3
79d55a734c26        nginx               "nginx -g 'daemon ..."   2 minutes ago       Exited (0) 5 seconds ago                       n2
f7b2206c019d        nginx               "nginx -g 'daemon ..."   2 minutes ago       Up 2 seconds               80/tcp              n1
复制代码

2、直接改配置文件(经测试后无效,修改配置文件后,启动容器后,该参数有自动变成了no,修改不生效)

复制代码
首先停止容器,不然无法修改配置文件
配置文件路径为:/var/lib/docker/containers/容器ID
在该目录下找到一个文件 hostconfig.json ,找到该文件中关键字 RestartPolicy
修改前配置:"RestartPolicy":{"Name":"no","MaximumRetryCount":0}
修改后配置:"RestartPolicy":{"Name":"always","MaximumRetryCount":0}
最后启动容器。
复制代码

 

 

修改docker容器的挂载路径


 

  • 停止所有docker容器
    sudo docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
  • 停止docker服务
    sudo service docker stop
  • 修改mysql路径
    cd ~
    sudo cp -r mysql/ /home/server/
  • 备份容器配置文件
    cd /var/lib/docker/containers/de9c6501cdd3
    cp hostconfig.json hostconfig.json.bak
    cp config.v2.json config.v2.json.bak
  • 修改hostconfig的冒号前的配置路径
    vi hostconfig.json
    
    "Binds": ["/home/server/mysql/conf/my.cnf:/etc/mysql/my.cnf", "/home/server/mysql/logs:/logs", "/home/server/mysql/data:/mysql_data"],
  • 修改config的Source的配置路径
    复制代码
    复制代码
    vi config.v2.json
    
           "MountPoints": {
    
                  "/etc/mysql/my.cnf": {
    
                         "Source": "/home/server/mysql/conf/my.cnf",
    
                         "Destination": "/etc/mysql/my.cnf",
    
                         "RW": true,
    
                         "Name": "",
    
                         "Driver": "",
    
                         "Relabel": "",
    
                         "Propagation": "rprivate",
    
                         "Named": false,
    
                         "ID": ""
    
                  },
    
                  "/logs": {
    
                         "Source": "/home/server/mysql/logs",
    
                         "Destination": "/logs",
    
                         "RW": true,
    
                         "Name": "",
    
                         "Driver": "",
    
                         "Relabel": "",
    
                         "Propagation": "rprivate",
    
                         "Named": false,
    
                         "ID": ""
    
                  },
    
                  "/mysql_data": {
    
                         "Source": "/home/server/mysql/data",
    
                         "Destination": "/mysql_data",
    
                         "RW": true,
    
                         "Name": "",
    
                         "Driver": "",
    
                         "Relabel": "",
    
                         "Propagation": "rprivate",
    
                         "Named": false,
    
                         "ID": ""
    
                  },
    
                  "/var/lib/mysql": {
    
                         "Source": "",
    
                         "Destination": "/var/lib/mysql",
    
                         "RW": true,
    
                         "Name": "85d91bff7012b57606af819480ce267449084e81ab386737c80ace9fe75f6621",
    
                         "Driver": "local",
    
                         "Relabel": "",
    
                         "Propagation": "",
    
                         "Named": false,
    
                         "ID": "897cd0152dd152166cb2715044ca4a3915a1b66280e0eb096eb74c2d737d7f77"
    
                  }
    
           },
    复制代码
    复制代码
  • 启动docker服务
     sudo service docker start
  • 启动所有docker容器
    sudo docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)

     

修改docker默认的存储位置


 

docker 的所有images及相关信息存储位置为:/var/lib/docker

  • 查看默认的docker存储路径
    docker info |grep 'Docker Root Dir'
    WARNING: No swap limit support Docker Root Dir: /var/lib/docker
  • 停止所有docker容器
    sudo docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
  • 停止docker服务
    sudo service docker stop
    cd /var/lib
  • 打包docker目录
    sudo tar -czvf /usr/docker.tar.gz docker/
    cd /usr/
    sudo tar -xzvf docker.tar.gz
  • 修改docker默认的存储位置
    sudo vim /etc/docker/daemon.json
    
    {
        "graph": "/home/server/docker"
    }
  • 启动docker服务
    sudo service docker start
  • 启动所有docker容器
    sudo docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
  • 查看修改后docker存储路径
    复制代码
    docker info |grep 'Docker Root Dir'
    WARNING: No swap limit support Docker Root Dir: /usr/docker





    本文转自:https://www.cnblogs.com/zhuochong/p/10070516.html !!!!!!
posted on 2021-06-08 10:37  时间暂停  阅读(310)  评论(0编辑  收藏  举报