Modifying Docker Container Configuration Parameters

Modifying Docker Container Configuration Parameters

In this article, we will explore two methods to modify Docker container configuration parameters, including modifying the container’s mount path and changing the default storage location.

Method 1: Modifying Docker Container Configuration Parameters

Sometimes, when creating a container, we forget to add parameters such as --restart=always. As a result, when Docker restarts, the container does not start automatically. To resolve this issue, we can use one of the following methods:

  1. Docker Command Modifies Container: We can use the docker container update command to modify the container’s configuration parameters. For example, to add the --restart=always parameter, we can use the following command:

docker container update --restart=always <container_name>

   Replace `<container_name>` with the actual name of your container.

2. **Change Configuration File Directly**: We can also modify the configuration file directly. To do this, we need to stop the container first, as modifying the configuration file while the container is running may cause issues. The configuration file path is `/var/lib/docker/containers/<container_id>`. Find the file named `hostconfig.json` and locate the `RestartPolicy` keyword. Modify the configuration as follows:
   Before modification:
   ```json
"RestartPolicy": {
    "Name": "no",
    "MaximumRetryCount": 0
}

After modification:

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

Finally, start the container.

Modifying Container Mount Path

We can modify the container’s mount path by modifying the hostconfig.json file. To do this, we need to stop all Docker containers and stop the Docker service. We can then modify the hostconfig.json file to change the mount path. For example, to change the mount path of the MySQL database, we can use the following commands:

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

We can then modify the hostconfig.json file to change the mount path. For example, to change the mount path of the MySQL database, we can use the following command:

cd /var/lib/docker/containers/de9c6501cdd3
cp hostconfig.json hostconfig.json.bak
cp config.v2.json config.v2.json.bak

We can then modify the hostconfig.json file to change the mount path. For example, to change the mount path of the MySQL database, we can use the following command:

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

We can then modify the config.v2.json file to change the mount path. For example, to change the mount path of the MySQL database, we can use the following command:

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

Finally, start the Docker service and start all Docker containers.

Modifying Docker Default Storage Location

We can modify the default storage location of Docker by modifying the daemon.json file. To do this, we need to stop all Docker containers and stop the Docker service. We can then modify the daemon.json file to change the default storage location. For example, to change the default storage location to /home/server/docker, we can use the following commands:

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

We can then modify the daemon.json file to change the default storage location. For example, to change the default storage location to /home/server/docker, we can use the following command:

cd /var/lib
tar -czvf /usr/docker.tar.gz docker
cd /usr
tar -xzvf docker.tar.gz

We can then modify the daemon.json file to change the default storage location. For example, to change the default storage location to /home/server/docker, we can use the following command:

sudo vim /etc/docker/daemon.json
{
 "Graph": "/home/server/docker"
}

Finally, start the Docker service and start all Docker containers.

Conclusion

In this article, we have explored two methods to modify Docker container configuration parameters, including modifying the container’s mount path and changing the default storage location. We have also provided step-by-step instructions on how to modify the hostconfig.json and daemon.json files to change the mount path and default storage location, respectively. By following these instructions, you can modify your Docker container configuration parameters to suit your needs.