容器早已创建好,如何得知它的启动参数(数据挂载到哪)
#假设通过如下命令启动了一个容器 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 #如何通过容器名得知启动参数 [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, 启动容器 [root@master ~]# docker run -d --name nginx2 nginx a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8 #2, 获取容器id [root@master ~]# docker inspect nginx2 | grep -i id [root@master ~]# docker inspect nginx2 | grep -i id "Id": "a9c9f31cdccf13c3385f3de33443325d2e14d69458e6d679e54c8cf9e5ff24c8", "Pid": 2069, "ExecIDs": null, "ContainerIDFile": "", #查看容器临时文件--每次创建时随机生成的文件(容器层) # /var/lib/docker/containers/容器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",
后天修改容器端口
需要确保:数据卷映射完全一致(新旧的容器,在数据卷上要一致)
[root1@c7-docker ~]# docker inspect mysql_port |egrep -i 'volum|Mount' -A 2 "Volumes": { "/var/lib/mysql": {} }, -- "Volumes": { "/var/lib/mysql": {} },
保存容器为镜像,再启动新容器是,添加端口映射
############## 1, 启动一个容器 ############## [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, 提交当前容器环境--保存为新的镜像 ############## [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, 停止旧容器, 启动新容器 ############## [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)
要先停止docker服务,否则不生效
通过容器层文件,找到配置文件hostconfig.json,修改容器映射的端口
[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"}]}......
[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" },
I, 子容器:拷贝镜像的文件
II, 子容器:添加新文件
III, 子容器:修改由镜像拷贝的文件
# 进入镜容器文件存储目录, 修改配置文件 [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; #在此处修改配置文件,把user nginx; worker_process 1; 这两行的分号去掉,看容器里面的配置是否更改 [root@master d37ff828e6308...0c0599b]# head merged/etc/nginx/nginx.conf -n 3 user nginx worker_processes 1 #登陆容器,查看修改的配置: 验证是否生效 [root@master d37ff828e6308...0c0599b]# docker exec -it nginx2 bash root@a9c9f31cdccf:/# head /etc/nginx/nginx.conf -n 3 user nginx worker_processes 1
#备份数据到新的存放路径 service docker stop mkdir /docker.bak mv /var/lib/docker/* /docker.bak #创建软连接 mkdir /home/docker-data mv /docker.bak/* /home/docker-data/ && rmdir /docker.bak ln -s /home/docker-data /var/lib/docker
vi /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd --graph /new-path/docker #reload配置文件 systemctl daemon-reload #重启docker systemctl restart docker.service
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。