docker学习笔记(五):docker镜像格式_dockerfile格式

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1docker 镜像格式:

创建镜像的方法

1)、可以通过现有的镜像做成容器, 然后在容器里做一些操作后,然后在导出成镜像;

2)、去openvz的官网去下载一个模板;

3)、通过dockerfile 来创建镜像;

dockerfile使用;nginx的镜像;

在当前目录新建 Dockerfile文件,文件内容如下,随后执行;

[root@localhost_001 ~]# cat Dockerfile 
## Set the base image to CentOS
FROM centos
# File Author / Maintainer
MAINTAINER yuanhh [email protected]
# Install necessary tools
RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
# Install Nginx
ADD http://nginx.org/download/nginx-1.8.0.tar.gz .
RUN tar zxvf nginx-1.8.0.tar.gz
RUN mkdir -p /usr/local/nginx
RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -fv /usr/local/nginx/conf/nginx.conf
ADD http://www.apelearn.com/study_v2/.nginx_conf  /usr/local/nginx/conf/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute when creating a new container
ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd
#tail  -f  表示参数表示这个容器一直执行,一直不退出;

执行:  docker   build  -t  centos_nginx  .

先执行下容器是否可以上网,因为需要包需要联网;
[root@localhost_001 ~]# docker run -itd centos     
4a1bc3528bd3d780b2d0595f0c51ebcde805c5534e3c8121dd0174885e9e8cbd
[root@localhost_001 ~]# docker exec -it 4a1bc3 bash
[root@4a1bc3528bd3 /]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=127 time=7.77 ms
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 7.777/7.777/7.777/0.000 ms
[root@4a1bc3528bd3 /]# exit
exit
开始执行;
[root@localhost_001 ~]# docker build -t centos6_nginx .
Sending build context to Docker daemon  571.8MB
Step 1/11 : FROM centos                  #第一步
 ---> afa1f6db949f
Step 2/11 : MAINTAINER yuanhh [email protected]       #第二步
 ---> Using cache
 ---> aa3feac756d0
Step 3/11 : RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
 ---> Running in 723f96cbe94b
Loaded plugins: fastestmirror
Setting up Install Process
Resolving Dependencies
--> Running transaction check
Step 8/11 : RUN rm -fv /usr/local/nginx/conf/nginx.conf           #删除这个文件
 ---> Running in a479c3faf99c
removed '/usr/local/nginx/conf/nginx.conf'
Removing intermediate container a479c3faf99c
 ---> 5fe53914a0fe
Step 9/11 : ADD http://www.apelearn.com/study_v2/.nginx_conf  /usr/local/nginx/conf/nginx.conf
Downloading [==================================================>]  1.678kB/1.678kB
 ---> 59650a3fb20e 
Step 10/11 : EXPOSE 80                   #映射端口
 ---> Running in cc9812e02957
Removing intermediate container cc9812e02957
 ---> 6cae38f00587
Step 11/11 : ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd    #执行
 ---> Running in 7f9b5b7f76bb
Removing intermediate container 7f9b5b7f76bb
 ---> 2253fb95f446
Successfully built 2253fb95f446
Successfully tagged centos6_nginx:latest

注释:如上,centos新建的包含nginx的镜像生成;            docker  images 

[root@localhost_001 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
centos6_nginx                  latest              2253fb95f446        2 minutes ago       379MB

启动这个镜像;     docker  run    -itd  -p   81:80   centos6_nginx   bash

[root@localhost_001 ~]# docker run -itd -p 81:80 centos6_nginx bash
f3f493c910942b2fcac68ab347596d6f08eb9b6ab8104b7b722a1a9829e32b9e

[root@localhost_001 ~]# docker exec -it f3f493 bash
[root@f3f493c91094 /]# ps aux |grep nginx
root          1  0.8  0.1  11680  1356 pts/0    Ss+  02:47   0:00 /bin/sh -c /usr/local/nginx/sbin/nginx && tail -f /etc/passwd bash
root          7  0.0  0.0  24880   792 ?        Ss   02:47   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody        8  0.0  0.3  27324  3364 ?        S    02:47   0:00 nginx: worker process
nobody        9  0.0  0.3  27324  3364 ?        S    02:47   0:00 nginx: worker process
root         25  0.0  0.0   9088   672 pts/1    S+   02:48   0:00 grep --color=auto nginx
[root@f3f493c91094 /]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7/nginx: master pro 
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path

注释:发现容器已经启动了nginx服务,镜像到本机的81端口;

在宿主机访问81端口;   curl  localhost:81

[root@localhost_001 ~]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      829/sshd            
tcp        0      0 0.0.0.0:56888           0.0.0.0:*               LISTEN      829/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1062/master         
tcp6       0      0 :::81                   :::*                    LISTEN      5959/docker-proxy  
[root@f3f493c91094 /]# curl localhost 81



Welcome to nginx!