Docker容器

Docker 04day

本文主要是介绍Docker 04day,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

04day

Dockerfile 实战缺少文件 所以 上传镜像 和 制作镜像 没有记录 周末更新

Dockerfile指令

FROM            #基础镜像 一切从这里开始搭建
MAINTAINER		#镜像是谁写的 姓名+邮箱
RUN				#镜像构建的时候需要运行的命令
ADD				#步骤:tomcat镜像 这个tomcat压缩包 添加内容
WORKDIR			#镜像工作的目录
VOLUME			#挂载的目录
EXPOSE			#保留端口配置
CMD				#指定这个容器启动的时候需要运行的命令,只有最后一个会生效 可被替代
ENTRYPOINT		#指定这个容器启动的时候需要运行的命令,可以追加命令
ONBUILD			#当构建一个被继承 Dockerfile 这个时候就会运行 	ONBUILD 的指令 触发指令
COPY			#类似ADD 将我们文件拷贝到镜像中
ENV				#构建的时候设置环境变量

实战测试

#构建自己的centos镜像

root@fanwd-virtual-machine:/home/text1# vim xiaohei
root@fanwd-virtual-machine:/home/text1# cat xiaohei 
FROM centos
MAINTAINER FANWD<320013624@qq.com
	
ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "---------end--------"
CMD /bin/bash


#通过文件构建镜像

root@fanwd-virtual-machine:/home/text1# docker build -f xiaohei -t mycentos:0.1 .  #命令构建


Successfully built 2e209a1255fd
Successfully tagged mycentos:0.1

#新建镜像进行测试

[root@9c21b634a633 local]# pwd
/usr/local
[root@9c21b634a633 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 21  bytes 2800 (2.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@9c21b634a633 local]# vim xiaohei123

# docker history 容器id  查看镜像变化的过程



CMD && ENTRYPOINT 的区别

 CMD     #指定这个容器启动的时候要运行的命令 只有最后一个会生效 可被替代
ENTRYPOINT  #指定这个容器启动的时候要运行的命令 可以追加命令


测试CMD

#测试
root@fanwd-virtual-machine:/home/text1# cat cesho1 
FROM centos
CMD ["ls","-a"]


root@fanwd-virtual-machine:/home/text1# docker build -f cesho1 -t centos:3.0 .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 300e315adb2f
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 00af7ce5a732
Removing intermediate container 00af7ce5a732
 ---> 3e4ea39097a2
Successfully built 3e4ea39097a2
Successfully tagged centos:3.0


#进入
root@fanwd-virtual-machine:/home/text1# docker run -it centos:3.0
.   .dockerenv	dev  home  lib64       media  opt   root  sbin	sys  usr
..  bin		etc  lib   lost+found  mnt    proc  run   srv	tmp  var
、
#追加-l测试报错
root@fanwd-virtual-machine:/home/text1# docker run 3e4ea39097a2 -l 
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled 

测试ENTRYPOINT

root@fanwd-virtual-machine:/home/text1# cat cesho1 
FROM centos
ENTRYPOINT ["ls","-a"]

#新建镜像
root@fanwd-virtual-machine:/home/text1# docker build -f cesho1 -t centos:4.0 .
Sending build context to Docker daemon   7.68kB
Step 1/2 : FROM centos
 ---> 300e315adb2f
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 6112f7cf447a
Removing intermediate container 6112f7cf447a
 ---> b148ce1f9d73
Successfully built b148ce1f9d73
Successfully tagged centos:4.0

#打开镜像
root@fanwd-virtual-machine:/home/text1# docker run -it centos:4.0
.   .dockerenv	dev  home  lib64       media  opt   root  sbin	sys  usr
..  bin		etc  lib   lost+found  mnt    proc  run   srv	tmp  var

#追加命令 -l
root@fanwd-virtual-machine:/home/text1# docker run -it b148ce1f9d73 -l
total 56
drwxr-xr-x   1 root root 4096 Mar 29 07:27 .
drwxr-xr-x   1 root root 4096 Mar 29 07:27 ..
-rwxr-xr-x   1 root root    0 Mar 29 07:27 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3 15:22 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar 29 07:27 dev
drwxr-xr-x   1 root root 4096 Mar 29 07:27 etc
drwxr-xr-x   2 root root 4096 Nov  3 15:22 home
lrwxrwxrwx   1 root root    7 Nov  3 15:22 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3 15:22 lib64 -> usr/lib64
drwx------   2 root root 4096 Dec  4 17:37 lost+found
drwxr-xr-x   2 root root 4096 Nov  3 15:22 media
drwxr-xr-x   2 root root 4096 Nov  3 15:22 mnt
drwxr-xr-x   2 root root 4096 Nov  3 15:22 opt
dr-xr-xr-x 301 root root    0 Mar 29 07:27 proc
dr-xr-x---   2 root root 4096 Dec  4 17:37 root
drwxr-xr-x  11 root root 4096 Dec  4 17:37 run
lrwxrwxrwx   1 root root    8 Nov  3 15:22 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3 15:22 srv
dr-xr-xr-x  13 root root    0 Mar 29 07:27 sys
drwxrwxrwt   7 root root 4096 Dec  4 17:37 tmp
drwxr-xr-x  12 root root 4096 Dec  4 17:37 usr
drwxr-xr-x  20 root root 4096 Dec  4 17:37 var

docker run、exec和attach使用和区别

docker run;创建和启动一个新的容器实例,操作对象是镜像,选项较多,如果你要创建和启动一个容器,只能用run;
docker exec: 在已运行的容器中,执行命令,操作对象是容器,如果你要进入已运行的容器,并且执行命令,用exec;
docker attach: 同样操作的是已运行的容器,可以将本机标准输入(键盘输入)输到容器中,也可以将容器的输出显示在本机的屏幕上,如果你想查看容器运行过程中产生的标准输入输出,用attach;
查看的一个大佬的博客 这是他的原文链接:

单击

docker网络

root@fanwd-virtual-machine:/fanwd/build/tomcat# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo   #本机回环地址
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d9:10:91 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.220.138/24 brd 192.168.220.255 scope global dynamic noprefixroute ens33
       valid_lft 1630sec preferred_lft 1630sec   ##### 内网地址
    inet6 fe80::f6ad:1e7d:41d8:a2d5/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:8f:1c:48:6f brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0  #### docker0 地址
       valid_lft forever preferred_lft forever
    inet6 fe80::42:8fff:fe1c:486f/64 scope link 
       valid_lft forever preferred_lft forever


#测试
root@fanwd-virtual-machine:/fanwd/build/tomcat# docker run -d -P --name tomcat01 tomcat

#查看容器内部的IP addr

root@fanwd-virtual-machine:/fanwd/build/tomcat# docker exec -it tomcat01 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever



#linux ping容器内部ip
fanw@fanwd-virtual-machine:~$ ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 比特,来自 172.17.0.2: icmp_seq=1 ttl=64 时间=0.101 毫秒
64 比特,来自 172.17.0.2: icmp_seq=2 ttl=64 时间=0.038 毫秒
64 比特,来自 172.17.0.2: icmp_seq=3 ttl=64 时间=0.051 毫秒
64 比特,来自 172.17.0.2: icmp_seq=4 ttl=64 时间=0.037 毫秒
64 比特,来自 172.17.0.2: icmp_seq=5 ttl=64 时间=0.048 毫秒


#veth-pair
,是一个成对的端口,所有从这对端口一 端进入的数据包都将从另一端出来,反之也是一样.


#测试
root@fanwd-virtual-machine:/fanwd/build/tomcat# docker run -d -P --name tomcat02 --link tomcat01 tomcat
c89596e3b47a36e7e65b37ebb8222aa861bcddb33e24a43358b700e8cb4cbbbf

 #tomcat02 ping tomcat01
 
root@fanwd-virtual-machine:/fanwd/build/tomcat# docker exec -it tomcat02 ping tomcat01
PING tomcat01 (172.17.0.2) 56(84) bytes of data.
64 bytes from tomcat01 (172.17.0.2): icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from tomcat01 (172.17.0.2): icmp_seq=2 ttl=64 time=0.044 ms
64 bytes from tomcat01 (172.17.0.2): icmp_seq=3 ttl=64 time=0.042 ms

#但是反向ping 是不行的
root@fanwd-virtual-machine:/fanwd/build/tomcat# docker exec -it tomcat01 ping tomcat02
ping: tomcat02: Name or service not known


#hosts

root@fanwd-virtual-machine:/fanwd/build/tomcat# docker exec -it tomcat02 cat /etc/hosts
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.17.0.2	tomcat01 900d0f1cd184   #映射tomcat01
172.17.0.3	c89596e3b47a


这篇关于Docker 04day的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!