Modify Docker container startup configuration parameters

Posted by s3rg1o on Tue, 12 Nov 2019 20:51:36 +0100


Sometimes, we forget to add the parameter -- restart=always when creating the container. When the Docker is restarted, the container fails to start automatically,

Now what about adding this parameter? There are two methods:

1. Docker command modification

docker container update --restart=always container name

2. Directly change the configuration file

Stop the container first, otherwise the configuration file cannot be modified

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

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

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

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

Finally, start the container.

Modify the mount path of docker container

  • Stop all docker containers

    sudo docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
  • Stop docker service

    sudo service docker stop
  • Modify mysql path

    cd ~
    sudo cp -r mysql/ /home/server/
  • Backup container profile

    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 configuration path of the Source of config

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"

              }

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

 

All images and related information of docker are stored in / 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 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

Topics: Linux Docker MySQL sudo JSON