Docker file storage path, modify port mapping operation mode

Docker file storage path, modify port mapping operation mode

How to get the container startup command

The container has already been created, how to know its startup parameters (where the data is mounted)

#Suppose a container is started by the following command docker run -d --name mysql_cdh \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
-v mysql-data:/var/lib/mysql \
--restart=always \
mysql:5.5 --character-set-server=utf8
#How to get the startup parameters through the container name [root@jenkins ~]# docker inspect mysql_cdh
[
 {
  "Id": "3aad772538b5e86705d3358362517a08d53f951aa6522e2881321135f05c8872",
  "Created": "2019-09-25T01:43:37.720505875Z",
  "Path": "docker-entrypoint.sh",
  "Args": [
   "--character-set-server=utf8"
  ],
  ...
  "Name": "/mysql_cdh",
  
	 	"HostConfig": {
	   "Binds": [
	    "mysql-data:/var/lib/mysql"
	   ],
	   ...
	   "NetworkMode": "default",
	   "PortBindings": {
	    "3306/tcp": [
	     {
	      "HostIp": "",
	      "HostPort": "3306"
	     }
	    ]
	   },
	   "RestartPolicy": {
	    "Name": "always",
	    "MaximumRetryCount": 0
	   },
	
 	"Mounts": [
	   {
	    "Type": "volume",
	    "Name": "mysql-data",
	    "Source": "/var/lib/docker/volumes/mysql-data/_data",
	    "Destination": "/var/lib/mysql",
	    "Driver": "local",
	    "Mode": "z",
	    "RW": true,
	    "Propagation": ""
	   }
	  ],
	
		"Config": {
	   "Hostname": "3aad772538b5",
	    ...
	   "ExposedPorts": {
	    "3306/tcp": {}
	   },
				...
	   "Env": [
	    "MYSQL_ROOT_PASSWORD=123456",
	    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/mysql/bin:/usr/local/mysql/scripts",
	    "GOSU_VERSION=1.7",
	    "MYSQL_MAJOR=5.5",
	    "MYSQL_VERSION=5.5.62"
	   ],
	   "Cmd": [
	    "--character-set-server=utf8"
	   ],

1. Container layer: file storage path

#1, Start the container [root@master ~]# docker run -d --name nginx2 nginx
a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8
#2, Get the container id
[root@master ~]# docker inspect nginx2 | grep -i id 
[root@master ~]# docker inspect nginx2 | grep -i id 
  "Id": "a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8",
   "Pid": 2069,
  "ExecIDs": null,
   "ContainerIDFile": "",
			
#View container temporary files--randomly generated files each time they are created (container layer)
# /var/lib/docker/containers/Container id
[root@master ~]# docker inspect nginx2 | grep -i path
 "Path": "nginx",
 "ResolvConfPath": "/var/lib/docker/containers/a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8/resolv.conf",
 "HostnamePath": "/var/lib/docker/containers/a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8/hostname",
 "HostsPath": "/var/lib/docker/containers/a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8/hosts",
 "LogPath": "/var/lib/docker/containers/a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8/a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8-json.log",

Modify the container port the day after tomorrow

a. Submit the old container as a new image first, then start the new container

You need to ensure that: the data volume mapping is completely consistent (the new and old containers must be consistent on the data volume)

[root1@c7-docker ~]# docker inspect mysql_port |egrep -i 'volum|Mount' -A 2
   "Volumes": {
    "/var/lib/mysql": {}
   },
--
   "Volumes": {
    "/var/lib/mysql": {}
   },

Save the container as an image, and then start a new container and add port mapping

############### 1. Start a container############## 
[root1@c7-docker ~]# docker run --name t1 -d -e MYSQL_ROOT_PASSWORD=123456 -v mysql-test-dir:/var/lib/mysql/ -p 3308:3306 mysql:5.5
53f2a3c53d4c6769897ee484c3d2ff4e3f82ea1e390d4950b2162c08af05d7cc
[root1@c7-docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53f2a3c53d4c mysql:5.5 "docker-entrypoint.s..." 3 seconds ago Up 3 seconds 0.0.0.0:3308->3306/tcp t1
[root1@c7-docker ~]# mysql -uroot -p123456 -P3308 --protocol tcp
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62 MySQL Community Server (GPL)
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
|mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MySQL [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> use test;
Database changed
MySQL [test]> create table t1(id int);
Query OK, 0 rows affected (0.00 sec)
MySQL [test]> insert into t1 values(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
MySQL [test]> select * from t1;
+------+
|id|
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
 
############## 2. Submit the current container environment--save as a new image############## 
[root1@c7-docker ~]# docker commit -m mysql_test_chag-port_img t1 mysql_port
sha256:9f90be3c200c6a8b535478375c4caf86d589d231afae1293d28026f9248f8ae3
[root1@c7-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql_port latest 9f90be3c200c 3 seconds ago 205MB
mysql 5.5 d404d78aa797 18 months ago 205MB
############## 3. Stop the old container and start the new container############### 
[root1@c7-docker ~]# docker stop t1
t1
[root1@c7-docker ~]# docker run --name t2 -d -v mysql-test-dir:/var/lib/mysql/ -p 3309:3306 mysql_port
fd7cefe55db8bb2687ab044ff1473f0c935ac5ce867df81b3b13f570e9026868
[root1@c7-docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd7cefe55db8 mysql_port "docker-entrypoint.s..." 3 seconds ago Up 2 seconds 0.0.0.0:3309->3306/tcp t2
[root1@c7-docker ~]# mysql -uroot -p123456 -P3309 --protocol tcp
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.62 MySQL Community Server (GPL)
MySQL [(none)]> show databases;
MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [test]> select * from t1;
+------+
|id|
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)

b. Modify the container configuration file: hostconfig.json

You must stop the docker service first, otherwise it will not take effect

Find the configuration file hostconfig.json through the container layer file and modify the port mapped by the container

[root@docker a9c9f31cd...ff24c8]# pwd
/var/lib/docker/containers/d93185e3a0....2e544ccfa
[root@docker a9c9f31cd...ff24c8]# cat hostconfig.json 
{"Binds":["/dockersuperset:/home/superset"],"ContainerIDFile":"",
"LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default",
"PortBindings":{"8088/tcp":[{"HostIp":"","HostPort":"8099"}]}......

2. Image layer: file storage path

[root@master ~]# docker image inspect nginx | ​​tail -n 22
  "GraphDriver": {
   "Data": {
    "LowerDir": "/var/lib/docker/overlay2/bf20cf788cc053f00ff1467525d50e19bd1cf07a2167f72511bdfcb28918a472/diff:/var/lib/docker/overlay2/317d80bb7ae58ed288be9ebd84aeb5b4b3a1c06f3211f5d1f32d89b629d1876e/diff",
    "MergedDir": "/var/lib/docker/overlay2/7782d0eb292fdc8bbd73bf9bae2d65468e8aba0bcd6baed55ac348618b80ae16/merged",
    "UpperDir": "/var/lib/docker/overlay2/7782d0eb292fdc8bbd73bf9bae2d65468e8aba0bcd6baed55ac348618b80ae16/diff",
    "WorkDir": "/var/lib/docker/overlay2/7782d0eb292fdc8bbd73bf9bae2d65468e8aba0bcd6baed55ac348618b80ae16/work"
   },
   "Name": "overlay2"
  },

a, container layer, image layer: association

I, sub-container: copy the image file

II, Subcontainer: Adding new files

III. Subcontainer: Modifying files copied from the image

b. In the image layer (file storage directory), modify the configuration file

# Enter the mirror container file storage directory and modify the configuration file [root@master d37ff828e6308...0c0599b]# ls diff/run/
nginx.pid
[root@master d37ff828e6308...0c0599b]# ls diff /var/cache/nginx/
client_temp fastcgi_temp proxy_temp scgi_temp uwsgi_temp
[root@master d37ff828e6308...0c0599b]# ls merged/
bin/ dev/ etc/ lib/ media/ opt/ root/ sbin/ sys/ usr/  
boot/ .dockerenv home/ lib64/ mnt/ proc/ run/ srv/ tmp/ var/  
[root@master d37ff828e6308...0c0599b]# ls merged/etc/nginx/
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
[root@master d37ff828e6308...0c0599b]# head merged/etc/nginx/nginx.conf 
user nginx;
worker_processes 1;
#Modify the configuration file here, remove the semicolons of the two lines user nginx; worker_process 1; to see if the configuration in the container has changed [root@master d37ff828e6308...0c0599b]# head merged/etc/nginx/nginx.conf -n 3
user nginx
worker_processes 1
#Log in to the container and view the modified configuration: Verify whether it is effective [root@master d37ff828e6308...0c0599b]# docker exec -it nginx2 bash
root@a9c9f31cdccf:/# head /etc/nginx/nginx.conf -n 3
user nginx
worker_processes 1

3. Modify the default storage path

a, rebuild the /var/lib/docker directory

#Backup data to a new storage pathservice docker stop
mkdir /docker.bak
mv /var/lib/docker/* /docker.bak
#Create a soft link mkdir /home/docker-data
mv /docker.bak/* /home/docker-data/ && rmdir /docker.bak
ln -s /home/docker-data /var/lib/docker

b. Modify the configuration

vi /usr/lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd --graph /new-path/docker 
#reload configuration file systemctl daemon-reload 
#Restart Docker 
systemctl restart docker.service

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Docker port mapping and external inaccessibility issues
  • How to achieve network access with Docker port mapping
  • Docker enables multiple port mapping commands
  • Docker volumes file mapping method
  • Docker primary network port mapping configuration
  • How to modify the port mapping of a running Docker container
  • Docker sets up port mapping, but cannot access the solution

<<:  Example code for implementing 3D text hover effect using CSS3

>>:  Tutorial on using Webpack in JavaScript

Recommend

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

Three ways to forward linux ssh port

ssh is one of the two command line tools I use mo...

Graphical steps of zabbix monitoring vmware exsi host

1. Enter the virtualization vcenter, log in with ...

How complicated is the priority of CSS styles?

Last night, I was looking at an interview question...

How to run postgreSQL with docker

1. Install Docker. Reference URL: Docker Getting ...

Web Design Principles of Hyperlinks

<br />Related articles: 9 practical tips for...

Realize super cool water light effect based on canvas

This article example shares with you the specific...

The principles and defects of MySQL full-text indexing

MySQL full-text index is a special index that gen...

Summary of MySQL data migration

Table of contents Preface: 1. About data migratio...

The whole process record of introducing Vant framework into WeChat applet

Preface Sometimes I feel that the native UI of We...

HTML hyperlink style (four different states) setting example

Copy code The code is as follows: <style type=...

JavaScript imitates Jingdong carousel effect

This article shares the specific code for JavaScr...

Summary of CSS usage tips

Recently, I started upgrading my blog. In the proc...