Modify Docker container automatic start / not automatic start, mount path, storage location

Sometimes, we forgot to add parameters --restart=always when creating the container  . When Docker restarts, the container fails to start automatically.

Now what if we want to add this parameter, there are two methods:

1. Docker command modification

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

我实际运行时,可以支持同时配置多个容器id,比如(其中container关键字可以忽略不写)

docker update 87cd61ad7f7c f488b0479f24 2109903220ce 1fb346ea1a46 --restart=no

2. Change the configuration file directly

First stop the container, otherwise you cannot modify the configuration file

The configuration file path is:/var/lib/docker/containers/容器ID

Find a file in the directory  hostconfig.json , find the keyword in the file RestartPolicy

Configuration before modification:"RestartPolicy":{"Name":"no","MaximumRetryCount":0}

Modified configuration:"RestartPolicy":{"Name":"always","MaximumRetryCount":0}

Finally, start the container.

 

Modify the mounting path of the docker container


 

  • Stop all docker containers
    sudo docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
  • Stop the docker service
    sudo service docker stop
  • Modify mysql path
    cd ~
    sudo cp -r mysql/ /home/server/
  • Back up container configuration files
    cd /var/lib/docker/containers/de9c6501cdd3
    cp hostconfig.json hostconfig.json.bak
    cp config.v2.json config.v2.json.bak
  • Modify the configuration path before the colon of 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"],
  • Modify the source configuration path of config

    Copy code

    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"
    
                  }
    
           },

    Copy code

  • Start docker service
     sudo service docker start
  • Start all docker containers
    sudo docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)

     

Modify the default storage location of docker


 

The storage location of all images and related information of docker is: / var / lib / docker

  • View the default docker storage path
    docker info |grep 'Docker Root Dir'
    WARNING: No swap limit support
    Docker Root Dir: /var/lib/docker
  • Stop all docker containers
    sudo docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
  • Stop the docker service
    sudo service docker stop
    cd /var/lib
  • Package docker directory
    sudo tar -czvf /usr/docker.tar.gz docker/
    cd /usr/
    sudo tar -xzvf docker.tar.gz
  • Modify the default storage location of docker
    sudo vim /etc/docker/daemon.json
    
    {
        "graph": "/home/server/docker"
    }
  • Start docker service
    sudo service docker start
  • Start all docker containers
    sudo docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
  • View the modified docker storage path
    docker info |grep 'Docker Root Dir'
    WARNING: No swap limit support
    Docker Root Dir: /usr/docker
Published 69 original articles · Like 72 · Visit 240,000+

Guess you like

Origin blog.csdn.net/londa/article/details/97611947