如何獲取容器的啟動命令
容器早已創(chuàng)建好,如何得知它的啟動參數(shù)(數(shù)據(jù)掛載到哪)
#假設(shè)通過如下命令啟動了一個容器
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
#如何通過容器名得知啟動參數(shù)
[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, 容器層:文件存放路徑
#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": "",
#查看容器臨時文件--每次創(chuàng)建時隨機生成的文件(容器層)
# /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",
后天修改容器端口
a, 先提交舊容器為新的鏡像,再啟動新容器
需要確保:數(shù)據(jù)卷映射完全一致(新舊的容器,在數(shù)據(jù)卷上要一致)
[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, 提交當前容器環(huán)境--保存為新的鏡像 ##############
[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)
b, 修改容器配置文件置文件:hostconfig.json
要先停止docker服務(wù),否則不生效
通過容器層文件,找到配置文件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"}]}......
2, 鏡像層:文件存放路徑
[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, 容器層,鏡像層:關(guān)聯(lián)
I, 子容器:拷貝鏡像的文件
II, 子容器:添加新文件
III, 子容器:修改由鏡像拷貝的文件
b, 在鏡像層(文件存儲目錄), 修改配置文件
# 進入鏡容器文件存儲目錄, 修改配置文件
[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
3, 修改默認存儲路徑
a, 重建/var/lib/docker目錄
#備份數(shù)據(jù)到新的存放路徑
service docker stop
mkdir /docker.bak
mv /var/lib/docker/* /docker.bak
#創(chuàng)建軟連接
mkdir /home/docker-data
mv /docker.bak/* /home/docker-data/ && rmdir /docker.bak
ln -s /home/docker-data /var/lib/docker
b, 修改配置
vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --graph /new-path/docker
#reload配置文件
systemctl daemon-reload
#重啟docker
systemctl restart docker.service
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。