Docker容器简单使用

在容器访问网络之前,确保没有运行的容器,这样后面使用起来会比较方便

[root@localhost ~]# docker container ls -a
CONTAINER ID      IMAGE         COMMAND        CREATED      STATUS        PORTS         NAMES
[root@localhost ~]#

启动两个容器,并查看IP地址

[root@localhost ~]# docker container run -it --name="wangju.1" centos:6.9
[root@68ab89110abc /]# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link[root@localhost ~]# docker container run -it --name="wangju.2" centos:6.9
[root@58d66407e136 /]# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:03  
          inet addr:172.17.0.3  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link

进行网络访问,使用ping命令

[root@68ab89110abc /]# ping 172.17.0.3
bash: ping: command not found   出现此问题,通过查阅资料需要安装软件yum install -y iproute*

现在就可以使用ping命令了,使用ping 172.17.0.3 就可以ping通

 

docker容器访问网络,当指定映射时,会自动添加一条iptables规则来实现端口映射

[root@localhost ~]# docker container run -d -p 8080:80 --name='n2' nginx:1.14      #规划好映射端口,即将容器内部的80 端口映射到外部网络(Windows系统)8080端口
[root@localhost ~]# docker container run -d -p  192.168.146.100:8080:80 --name='n3' nginx:1.14    #映射到某一个主机地址的端口
[root@localhost ~]# docker container run -d -p 80 --name='n5' nginx:1.14        #随机映射
[root@localhost ~]# docker container run -d -p 192.168.146.100::80 --name='n6' nginx:1.14
[root@localhost ~]# docker container run -d -p 33060:3306  -p 2222:22  -p 12345:80   nginx:1.14   #  映射多个端口

 查看容器的进程docker container top +ID号

[root@localhost ~]# docker container ls -a
CONTAINER ID       IMAGE          COMMAND      CREATED               STATUS                                        PORTS                 NAMES
121a6b9e9ebc        centos:6.9      "/bin/bash"       47 minutes ago        Exited (0) About a minute ago                                   wangju.2
fe73e57fa28c          centos:6.9      "/bin/bash"      48 minutes ago         Up 7 seconds                                                             wangju.1
[root@localhost ~]# docker container top fe73e57fa28c  #注意此时的ID号必须是启动状态的容器的ID号
UID         PID           PPID      C        STIME       TTY       TIME          CMD
root        12686       12670     0         21:53         pts/2      00:00:00     /bin/bash

查看日志

[root@localhost ~]# docker logs testxx(容器名)    #也可以在docker logs 后面加ID地址查看日志

[root@localhost ~]# docker logs  -tf testxx(容器名)      #后面的-f 表示实时监控日志, -tf显示的更加详细,日志中带用时间戳

删除所有的docker容器的时候,使用如下命令,一定要注意使用的是``引号,而非单引号‘’ 

[root@localhost ~]# docker container rm -f `docker container ls -a -q`
121a6b9e9ebc
fe73e57fa28c

重新启动容器

[root@localhost ~]# docker run -d --name="n1" -p 8080:80 nginx     #我将容器的端口映射到了Windows系统的8080端口
041d3a3b67775566a0c35a241e21cf09f1ae4b136c254f378e312dbe99d60c0c

启动之后可以在Windows上访问

如何查看容器线程数 查看容器进程_docker

此时我们看到的页面是Nginx的访问页面,我们也可以按照自己的意愿,编写访问页面

[root@localhost ~]# docker container exec -it n1 /bin/bash    #登录容器进行调试
root@041d3a3b6777:/#root@041d3a3b6777:/# cd /usr/share/nginx/html/   进入指定目录,进行配置,
root@041d3a3b6777:/usr/share/nginx/html# ls
50x.html  index.htmlroot@041d3a3b6777:/usr/share/nginx/html# vim index.html    #编辑文件index.html,发现不能使用vim命令
bash: vim: command not found

查看系统,发现并不是我们常用的centos系统,因此一些命令时无法使用的

root@041d3a3b6777:/usr/share/nginx/html# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@041d3a3b6777:/usr/share/nginx/html#

但是我们可以使用echo命令与才命令,进行编辑index.html文件,来修改网页

root@041d3a3b6777:/usr/share/nginx/html# echo "wangju yu feifei "> index.html
root@041d3a3b6777:/usr/share/nginx/html# cat index.html
wangju yu feifei

现在刷新网页,显示的就是我们修改的内容

如何查看容器线程数 查看容器进程_nginx_02

由于一些命令不可用,所以我们可以使用宿主机把文件cp到容器中,这样做就省事了好多。

但在这之前要查好docker容器的文件地址,方便后期拷贝使用

root@041d3a3b6777:/usr/share/nginx/html# pwd
/usr/share/nginx/html

进入宿主机,创建测试环境

[root@localhost ~]# cd /opt
[root@localhost opt]# ls
apache-tomcat-8.5.65  nginx.tar.gz  tomcat
[root@localhost opt]# echo "wangju test docker" > index.html
[root@localhost opt]# ls
apache-tomcat-8.5.65  index.html

拷贝文件到docker容器中

[root@localhost opt]# docker container cp index.html n1:/usr/share/nginx/html

现在刷新网页,发现网页内容变成我们修改的内容了

如何查看容器线程数 查看容器进程_如何查看容器线程数_03

 我们也可以将docker容器中的文件拷贝到宿主机中

[root@localhost opt]# docker container cp n1:/usr/share/nginx/html/50x.html ./
[root@localhost opt]# ls
50x.html

使用上面的方法修改一个容器的网页比较方便,但是如果要修改的很多就不方便了,所以,我们可以建立数据卷,用来来实现宿主机与容器的数据共享,相关内容可以参考我的下一篇博客。