Docker(十七)-修改Docker容器啟動配置引數

吕金林發表於2024-04-24

有時候,我們建立容器時忘了新增引數 --restart=always ,當 Docker 重啟時,容器未能自動啟動,

現在要新增該引數怎麼辦呢,方法有二:

1、Docker 命令修改

docker container update --restart=always 容器名字

2、直接改配置檔案

首先停止容器,不然無法修改配置檔案

配置檔案路徑為:/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://i.cnblogs.com/posts/edit

相關文章