3 容器管理
最简单运行容器:
docker run 镜像名docker run centos:v1(这个是镜像名称)docker run -dit --name=c1 centos:v1[root@docker ~]# docker tag hub.c.163.com/public/centos:6.7-tools centos:v1[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql latest b8fd9553f1f0 4 weeks ago 445 MBhub.c.163.com/library/mysql latest 9e64176cd8a2 2 years ago 407 MBcentos v1 b2ab0ed558bb 2 years ago 602 MBhub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 2 years ago 602 MB
查看正在运行的容器:
docker ps
查看所有容器:
docker ps -a
运行容器:
docker run -t centos:v1
提供一个模拟终端,但不能交互,这样容器里面终端不关闭,容器也不会关闭docker run -t -i centos:v1
允许交互
可以这样写:```
docker run -it centos:v1
我们在运行一个容器的时候,如果没有指定容器运行什么程序,则默认运行镜像最上面一层的CMD所指的东西。我们可以用指定的命令,替换掉容器里默认的程序:如:
[root@docker ~]# docker run centos:v1 /bin/bash
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4bbd7c1810d1 centos:v1 "/bin/bash" About a minute ago Exited (0) About a minute ago sleepy_spence
5be8fb481041 centos:v1 "/usr/bin/supervisord" 17 minutes ago Up 17 minutes 22/tcp c1
如果直接执行docker run centos:v1,是执行CMD "/usr/bin/supervisord"命令,但是我们通过修改指令,变成运行/bin/bash。 由于/bin/bash生命周期非常短,执行完成则关闭了。可以指定不同的shell:
[root@docker ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@docker ~]# docker run -t -i centos:v1 /bin/sh
sh-4.1# exit
exit
[root@docker ~]# docker run -t -i centos:v1 /bin/bash
[root@53f96ebba145 /]#
验证容器里面的程序结束,容器也跟着结束:老段在验证过程是用了个CMD "/bin/bash"的镜像,容器一运行,就很快结束。docker run centos:v1 sleep 10可以观察到10秒后,容器也跟着结束创建容器同时定义名称docker run --name=c1 centos:v1停掉/开启/进入j交互容器
docker run -it --name=c1 centos:v1 /bin/bash
docker rm c1
docker start c1
docker attach c1
只要退出容器,容器就会关闭,怎么做到退出也仍然保留容器呢?docker run -it --name=c1 --restart=always centos:v1[root@docker ~]# docker run -it --name=c3 --restart=always centos:v1 /bin/bash频繁测试时候,会产生大量的不运行的容器,需要逐个删非常麻烦,我们可以加--rmdocker run -it --name=c1 --rm centos:v1docker run -it --name=c3 --restart=always centos:v1 /bin/bash运行退出后,docker ps -a 下也会自动删除在交互环境下,改容器的hostname,可以加-h 名字[root@docker ~]# docker run -it --name=c3 --rm -h test centos:v1 /bin/bash[root@test /]#容器放在后台运行:-ddocker run --it -d --name=c1 centos:v1练习:开发环境中,需要python3的环境,但是每次都需要部署安装python3环境就非常麻烦,咱们可以直接运行容器实现:1 首先找python3的镜像: docker search pyhon3或者在网易云/阿里云上找镜像。2 下载 docker pull docker.io/sgoblin/python3.43 查看CMD:
[root@docker ~]# docker history docker.io/sgoblin/python3.4
IMAGE CREATED CREATED BY SIZE COMMENT
f01226b9a9cf 23 months ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/bash"] 0 B
[root@docker ~]#
4运行容器:docker run -it --name=p1 --restart=always docker.io/sgoblin/python3.4我们需运行mysql
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest b8fd9553f1f0 4 weeks ago 445 MB
docker.io/sgoblin/python3.4 latest f01226b9a9cf 23 months ago 489 MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 2 years ago 407 MB
centos v1 b2ab0ed558bb 2 years ago 602 MB
hub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 2 years ago 602 MB
[root@docker ~]#
[root@docker ~]# docker run -d --name=db --restart=always mysql
03a9ed98a6c87d07e072af59c6e6a758f200bc7db312142a76781da1c4a56f1a
[root@docker ~]#
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03a9ed98a6c8 mysql "docker-entrypoint..." 8 seconds ago Restarting (1) 1 second ago db
65a7fc20e52a docker.io/sgoblin/python3.4 "/bin/bash" 14 minutes ago Up 13 minutes p1
[root@docker ~]#
[root@docker ~]#
[root@docker ~]# docker log
login logout logs
[root@docker ~]# docker logs db 查看容器里面运行的内容使用logs
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
....
[root@docker ~]#
[root@docker ~]# docker logs -f db
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
.....
[root@docker ~]#
可以看到我们没有定义数据库相关的变量,就会有报错提示,需要定义数据库、账号、密码等docker run -dit --restart=always -e 变量1=值1 -呃变量2=值2 镜像docker run -dit --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=woaini mysql可以下载客户端验证:yum install mariadb -y查看容器mysql运行时候的IP:docker exec db ip adocker inspect db (db是容器名字)docker run -d --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=woaini mysqldocker exec -it db /bin/bashmysql -uroot -predhatALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';使用客户端验证docker运行 mysql时候出现的报错:[root@docker ~]# mysql -uroot -predhat -h272.17.0.2ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory问题原因:可以看到MySQL8.0.12版本默认的认证方式是caching_sha2_password,连接不上的原因在于下载的mariadb客户端版本比较低,不支持该格式的密码。解决办法:[root@docker ~]# docker run -d --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=xxx mysql9f947de785f8514d0688abbf30715022c6ffcdb8e3071029a818b51db1399a4b[root@docker ~]# docker exec -it db /bin/bashroot@9f947de785f8:/# mysql -uroot -predhatmysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 8Server version: 8.0.17 MySQL Community Server - GPLCopyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> select version();+-----------+| version() |+-----------+| 8.0.17 |+-----------+1 row in set (0.00 sec)mysql> show variables like 'default_authentication_plugin';+-------------------------------+-----------------------+| Variable_name | Value |+-------------------------------+-----------------------+| default_authentication_plugin | caching_sha2_password |+-------------------------------+-----------------------+1 row in set (0.01 sec)mysql> select host,user,plugin from mysql.user;+-----------+------------------+-----------------------+| host | user | plugin |+-----------+------------------+-----------------------+| % | root | caching_sha2_password || localhost | mysql.infoschema | caching_sha2_password || localhost | mysql.session | caching_sha2_password || localhost | mysql.sys | caching_sha2_password || localhost | root | caching_sha2_password |+-----------+------------------+-----------------------+5 rows in set (0.00 sec)mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';Query OK, 0 rows affected (0.01 sec)flush privileges;mysql> select host,user,plugin from mysql.user;+-----------+------------------+-----------------------+| host | user | plugin |+-----------+------------------+-----------------------+| % | root | caching_sha2_password || localhost | mysql.infoschema | caching_sha2_password || localhost | mysql.session | caching_sha2_password || localhost | mysql.sys | caching_sha2_password || localhost | root | mysql_native_password |+-----------+------------------+-----------------------+5 rows in set (0.00 sec)mysql>练习:安装nginx安装mysql安装tomcat安装redis安装apache安装nginx:[root@docker ~]# docker pull hub.c.163.com/library/nginx:latestTrying to pull repository hub.c.163.com/library/nginx ...latest: Pulling from hub.c.163.com/library/nginx5de4b4d551f8: Pull completed4b36a5e9443: Pull complete0af1f0713557: Pull completeDigest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6Status: Downloaded newer image for hub.c.163.com/library/nginx:latest[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql latest b8fd9553f1f0 5 weeks ago 445 MBdocker.io/sgoblin/python3.4 latest f01226b9a9cf 23 months ago 489 MBhub.c.163.com/library/nginx latest 46102226f2fd 2 years ago 109 MBhub.c.163.com/library/mysql latest 9e64176cd8a2 2 years ago 407 MBcentos v1 b2ab0ed558bb 2 years ago 602 MBhub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 2 years ago 602 MB[root@docker ~]#[root@docker ~]#[root@docker ~]# docker history hub.c.163.com/library/nginxIMAGE CREATED CREATED BY SIZE COMMENT46102226f2fd 2 years ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daem... 0 B 2 years ago /bin/sh -c #(nop) STOPSIGNAL [SIGQUIT] 0 B 2 years ago /bin/sh -c #(nop) EXPOSE 80/tcp 0 B 2 years ago /bin/sh -c ln -sf /dev/stdout /var/log/ngi... 22 B 2 years ago /bin/sh -c apt-get update && apt-get inst... 52.2 MB 2 years ago /bin/sh -c #(nop) ENV NJS_VERSION=1.13.0.... 0 B 2 years ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.13.... 0 B 2 years ago /bin/sh -c #(nop) MAINTAINER NGINX Docker... 0 B 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B 2 years ago /bin/sh -c #(nop) ADD file:253a2a9f49900b3... 57.1 MB [root@docker ~]#[root@docker ~]# docker run -d --name=web --restart=always -p 80:80 hub.c.163.com/library/nginxd283f5c11513a068e871603067e8147a6e18c8aebb8c0c4b87c86846b8e7a304/usr/bin/docker-current: Error response from daemon: driver failed programming external connectivity on endpoint web (1ab60e90073de05080aa53be78cff38a60b5c04fef6d51fbd8b82039695cbbfc): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 80 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: No chain/target/match by that name.(exit status 1)).[root@docker ~]#出现报错,查询百度:实际原因:docker服务启动时定义的自定义链DOCKER由于某种原因被清掉重启docker服务及可重新生成自定义链DOCKER解决方法:守护进程重启 sudo systemctl daemon-reload重启docker服务 systemctl restart docker[root@docker ~]# docker run -d --name=web --restart=always -p 80:80 hub.c.163.com/library/nginxbfc14f7146607b351d5a1b4c9484cd0aecec7afdebd6526a3eddd9fac17fbb0a[root@docker ~]#[root@docker ~]# docker inspect web在图形界面的浏览器输入IP地址,即可看到登陆nginx的网页安装tomcatdocker pull hub.c.163.com/library/tomcat:latestdocker imagesdocker history hub.c.163.com/library/tomcatdocker run -d --name=tm --restart=always hub.c.163.com/library/tomcat