docker Modify Configuration Information Action for a Container Not Started
- 2021-09-25 00:04:31
- OfStack
The dockerfile or docker-compose orchestration service was not used when I started using docker in the previous 1. Directly use the container started by docker run. As a result, 1 accidentally made an error in the configuration file or stopped it, which will lead to the service can no longer be started. Don't worry at this time. Use the following methods to modify the configuration file and modify the port information and other configuration information of the startup container, so that the files and data in the container still exist.
1. Modify the configuration information in the container
Here, take nginx startup as an example. For example, docke run creates a container of nginx. As a result, docker exec ** bash modifies the configuration file of internal nginx, resulting in the nginx container can no longer be started. What should I do in this case
# First enter the following directory
cd /var/lib/docker/overlay2
# Find configuration files
find ./ -name nginx.conf
./a45cd97113877fb480f66e0d982a594c7b18f2035c16e1f7eb687eef15552272/diff/etc/nginx/nginx.conf
./feff64c1f27a695e531c4654afa3b06e1ca84cc38d81cec76dc5ef52f6821c44/diff/etc/nginx/nginx.conf
./feff64c1f27a695e531c4654afa3b06e1ca84cc38d81cec76dc5ef52f6821c44/merged/etc/nginx/nginx.conf
# And then cd To nginx.conf The directory where the
cd ./feff64c1f27a695e531c4654afa3b06e1ca84cc38d81cec76dc5ef52f6821c44/diff/etc/nginx
# Like mine nginx The configuration file of is in the conf.d Under the folder
cd conf.d
default.conf
# Observe whether the configuration information inside is your own nginx Configuration information of, if yes, then modify it, if not, just look at it find Look for other directories in the same way.
vim *.conf
# After modifying the configuration file, start directly such as mine nginx The container name is apg_nginx
docker start apg_nginx
2. Modify the configuration information of docker run
What if my container is shut down and other services want to use the port number mapped by my container or my container wants to change the information of run configuration at that time?
# Enter the following directory
cd /var/lib/docker/containers
096ed809576e948ada99be65faa181b7f926dd0e655c4c373169305a1954c115 d570bc34c3bb5955ec4c336ad3eeb6105ed49e33e87b8dfd35da3c907d074fdc
662d928aada58645e84fba34f1a1c54696061b767e3e262ccf6562db0498e733 e7237690488f15e40a6462df9cdcfeda83a6f7a74517662935c0a149dd36e057
92bb44f63186c3d80ab8557f84cb1ce907aedab714bb0109827dbfed5641daa8 f2849f199fd78b1636aaedf0bd890c12e7c6d8c6ad5849fff2670920e5e37c7d
d27bdc6286dd3314a1116115cab3a33233b9f4fba45ae4c88a6756d5c04a9aa9
# View the of your own container hash Value, hash Value is the configuration directory for the container
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
096ed809576e nginx "nginx -g 'daemon of … " 3 months ago Up 33 minutes 0.0.0.0:8081->80/tcp apg_nginx
# Enter 096 Directory at the beginning
cd 096ed809576e*
drwx------ 4 root root 4096 Jun 22 21:41 ./
drwx------ 9 root root 4096 Mar 8 18:07 ../
-rw-r----- 1 root root 2559664 Jun 22 21:47 096ed809576e948ada99be65faa181b7f926dd0e655c4c373169305a1954c115-json.log
drwx------ 2 root root 4096 Mar 8 18:07 checkpoints/
-rw------- 1 root root 3408 Jun 22 21:41 config.v2.json
-rw-r--r-- 1 root root 1519 Jun 22 21:41 hostconfig.json
-rw-r--r-- 1 root root 13 Jun 22 21:41 hostname
-rw-r--r-- 1 root root 174 Jun 22 21:41 hosts
drwx------ 3 root root 4096 Mar 8 18:07 mounts/
-rw-r--r-- 1 root root 259 Jun 22 21:41 resolv.conf
-rw-r--r-- 1 root root 71 Jun 22 21:41 resolv.conf.hash
#hostconfig.json Is to store run File of configuration information at startup
vim hostconfig.json
# Find it inside HostPort Value is the port number mapped to the host machine, which can be modified by itself and restarted after modification docker Service, it will not take effect if it is not restarted
systemctl docker restart
Through the configuration modification under the above two situations, the container started by docker run can be modified here and started normally. It is best to use docker-compose to start the container.
Additional knowledge: How does docker view the container startup log
sudo docker logs-f-t--tail Row Number Container Name
Command format:
$ docker logs [OPTIONS] CONTAINER
Options:
--details Show more information
-f, --follow Trace real-time log
--since string Display from a certain timestamp After the log, or relative time, such as 40m (I. E. 40 Minutes)
--tail string How many lines of log are displayed from the end of the log, The default is all
-t, --timestamps Display timestamp
--until string Display from a certain timestamp Previous log, or relative time, such as 40m (I. E. 40 Minutes)
Check the log for the last 30 minutes:
$ docker logs --since 30m CONTAINER_ID
View the log after a certain time:
$ docker logs -t --since="2019-08-02T13:23:37" CONTAINER_ID
View the log of a certain time period:
$ docker logs -t --since="2019-08-02T13:23:37" --until "2019-08-03T12:23:37" CONTAINER_ID