如何修改Docker容器为“自启动/不自动启动”、“挂载路径”、“存储位置”呢?

书欣 Docker 发布时间:2023-01-12 11:15:42 阅读数:20248 1
置 下文笔者讲述docker容器修改为“自启动/不自动启动”、“挂载路径”、“存储位置”的方法分享,如下所示
实现思路:
    实现自启动,添加参数 --restart=always,即可实现Docker重启时,容器自启动
例:
 
//使用docker命令修改docker容器,容器自启动
  docker container update --restart=always 容器名字

或一次配置多个容器
docker update 容器id1 容器id2 容器id3 容器id4 --restart=no

或直接改配置文件
 1.停止容器(否则无法修改配置文件)

 2.配置文件路径为:/var/lib/docker/containers/容器ID

 3.在该目录下找到一个文件 hostconfig.json ,找到该文件中关键字 RestartPolicy
   修改前配置:"RestartPolicy":{"Name":"no","MaximumRetryCount":0}
   修改后配置:"RestartPolicy":{"Name":"always","MaximumRetryCount":0}

 4.最后启动容器

修改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/92er178912a0
    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.Java265.com/docker/202301/5345.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者