3容器管理

最简单运行容器:

10余年建站经验, 成都做网站、网站设计客户的见证与正确选择。创新互联建站提供完善的营销型网页建站明细报价表。后期开发更加便捷高效,我们致力于追求更美、更快、更规范。

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 images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
MySQL                         latest              b8fd9553f1f0        4 weeks ago         445 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

查看正在运行的容器:

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

频繁测试时候,会产生大量的不运行的容器,需要逐个删非常麻烦,我们可以加--rm

docker run -it --name=c1 --rm centos:v1

docker 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 /]#

容器放在后台运行:-d

docker run --it -d --name=c1 centos:v1

练习:开发环境中,需要python3的环境,但是每次都需要部署安装python3环境就非常麻烦,咱们可以直接运行容器实现:

1 首先找python3的镜像:
    docker search pyhon3
或者在网易云/阿里云上找镜像。
2 下载
 docker pull docker.io/sgoblin/python3.4
3 查看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
23 months ago /bin/sh -c #(nop) CMD [] 0 B
23 months ago /bin/sh -c pip3 install -U virtualenv 3.86 MB
23 months ago /bin/sh -c ln -s /usr/bin/python3.4 /usr/b... 10.9 MB
23 months ago /bin/sh -c #(nop) ADD 79312a2a112b91c4ece7... 1.6 MB
23 months ago /bin/sh -c sed -i 's/archive.ubuntu.com/mi... 341 MB
23 months ago /bin/sh -c #(nop) MAINTAINER Ryan M. 3 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
3 years ago /bin/sh -c sed -i 's/^#\s(deb.universe... 1.88 kB
3 years ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/po... 701 B
3 years ago /bin/sh -c #(nop) ADD file:3f4708cf445dc1b... 131 MB
[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 a
docker inspect db  (db是容器名字)

docker run -d --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=woaini mysql
docker exec -it db /bin/bash
mysql -uroot -predhat
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

使用客户端验证docker运行 mysql时候出现的报错:
[root@docker ~]# mysql -uroot -predhat -h272.17.0.2
ERROR 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 mysql
9f947de785f8514d0688abbf30715022c6ffcdb8e3071029a818b51db1399a4b
[root@docker ~]# docker exec -it db /bin/bash
root@9f947de785f8:/# mysql -uroot -predhat
mysql: [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 8
Server version: 8.0.17 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
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:latest
Trying to pull repository hub.c.163.com/library/nginx ...
latest: Pulling from hub.c.163.com/library/nginx
5de4b4d551f8: Pull complete
d4b36a5e9443: Pull complete
0af1f0713557: Pull complete
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6
Status: Downloaded newer image for hub.c.163.com/library/nginx:latest
[root@docker ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
mysql                         latest              b8fd9553f1f0        5 weeks ago         445 MB
docker.io/sgoblin/python3.4   latest              f01226b9a9cf        23 months ago       489 MB
hub.c.163.com/library/nginx   latest              46102226f2fd        2 years ago         109 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 ~]#
[root@docker ~]# docker history hub.c.163.com/library/nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
46102226f2fd        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/nginx
d283f5c11513a068e871603067e8147a6e18c8aebb8c0c4b87c86846b8e7a304
/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/nginx
bfc14f7146607b351d5a1b4c9484cd0aecec7afdebd6526a3eddd9fac17fbb0a
[root@docker ~]#
[root@docker ~]# docker inspect web

在图形界面的浏览器输入IP地址,即可看到登陆nginx的网页

安装tomcat

docker pull hub.c.163.com/library/tomcat:latest
docker images
docker history hub.c.163.com/library/tomcat
docker run -d --name=tm --restart=always hub.c.163.com/library/tomcat

当前文章:3容器管理
文章链接:http://hbruida.cn/article/iepgsj.html