dockerfile常用指令:

FROM:指定base镜像;如果本地有从本地调取,如果本地不存在会从远程仓库下载。
MAINTAINER:设置镜像的作者,比如用户邮箱等。
COPY:把文件从build context复制到镜像。支持两种形式:COPY src dest和COPY ["src","dest"]
    src必须指定build context中的文件或目录。
ADD:用法和COPY类似,不同的是src可以是归档压缩文件,文件会被自动解压到dest,
   也可以自动下载URL并拷贝到镜像:
   ADD html.tar /var/www           ADD http://ip/html.tar/var/www
ENV:设置环境变量,变量可以被后续的指令使用。 ENV HOSTNAME server1.example.com
EXPOSE:如果容器中运行应用服务,可以把服务端口暴露出去。 EXPOSE 80
VOLUME:申明数据卷,通常指定的是应用的数据挂载点。 VOLUME ["/var/www/html"]
WORKDIR:为RUN、CMD、ENTRYPOINT、ADD和COPY指令设置镜像中的当前工作目录,
                    如果目录不存在会自动创建。
RUN:在容器中运行命令并创建新的镜像层,常用于安装软件包。 RUN yum install -y vim
CMD:用于设置容器启动后执行的命令,会被docker run后面的命令行覆盖。
ENTRYPOINT:用于设置容器启动后执行的命令,不会被忽略,一定会被执行。
           docker run后面的参数可以传递给ENTRYPOINT指令当作参数。
           Dockerfile中只能指定一个ENTRYPOINT,如果指定了多个,只有最后一个有效。
           shell格式底层会调用/bin/sh -c来执行命令,可以解析变量,而exec格式不会。
           FROM busybox      【shell格式】            FROM busybox    【exec格式】
           ENV name world                                       ENV name world
           ENTRYPOINT echo "hello,$name"         ENTRYPOINT ["/bin/echo","hello,$name"]
           需要改写为ENTRYPOINT ["/bin/sh","-c","echo hello,$name"]
           在exec格式时,ENTRYPOINT可以通过CMD提供额外参数,CMD的额外参数可以在容器启动时动态替换。
           在shell格式时,ENTRYPOINT会忽略任何CMD或docker run提供的参数。
           FROM busybox
           ENTRYPOINT ["/bin/echo","hello"]
           CMD ["world"]    【官方推荐使用exec格式】

实例:

结构查看:
[root@localhost ~]# tree /opt/haproxy/
/opt/haproxy/
├── Dockerfile
└── files
    ├── haproxy-2.4.0.tar.gz
    ├── haproxy.cfg
    ├── install.sh
    └── start.sh

1 directory, 5 files

[root@localhost ~]# tree test/
test/
├── html1
│   └── index.html
└── html2
    └── index.html

2 directories, 2 files

[root@localhost ~]# mkdir -p  /opt/haproxy/{conf,files}/
[root@localhost ~]# cd /opt/haproxy/
[root@localhost haproxy]# touch Dockerfile
编写Dockerfile
[root@localhost haproxy]# vim Dockerfile 
[root@localhost haproxy]# cat Dockerfile 
#基础镜像
FROM centos

#作者信息
LABEL MAINTAINER "sean "

#环境变量
ENV version 2.4.0

#传输文件
COPY files/haproxy-${VERSION}.tar.gz  /usr/src
COPY files/haproxy.cfg  /usr/local/haproxy/conf/haproxy.cfg
ADD files/start.sh  /scripts/
COPY files/install.sh /scripts/

#安装
RUN ["/bin/bash","-c","/usr/src/install.sh"]

#暴露端口
EXPOSE 80 8189


#启动命令
CMD ["/scripts/start.sh"]

查看Dockerfile所需的文件
[root@localhost haproxy]# cat files/install.sh 
#!/bin/bash

#删除yum并重新拉取
rm -rf /etc/yum.repos.d/*  
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F'"' 'NR==5{print $2}'  /etc/os-release).repo 
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo 
yum clean all && yum makecache 

#安装依赖包
yum -y install make gcc gcc-c++ pcre-devel bzip2-devel openssl-devel systemd-devel

#创建用户
useradd -r -M -s /sbin/nologin haproxy 

#解压缩包
cd /usr/src/
tar xf haproxy-${VERSION}.tar.gz
cd /usr/src/haproxy-${VERSION}  
make clean && \
make -j $(nproc)  \
    TARGET=linux-glibc  \
    USE_OPENSSL=1  \
    USE_ZLIB=1  \
    USE_PCRE=1  \
    USE_SYSTEMD=1 && \
make install PREFIX=/usr/local/haproxy 

#优化命令
cp haproxy  /usr/sbin/  

#配置各个负载的内核参数
echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf 
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf

#安装完成清理可删除的东西
rm -rf /usr/src/haproxy-2.4.0/  /var/cache/*
yum -y remove make gcc gcc-c++

[root@localhost haproxy]# cat files/haproxy.cfg 
#--------------全局配置----------------
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats
    stats realm Haproxy\ Statistics
    stats auth admin:admin
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode tcp
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.0.2:80 check inter 2000 fall 5
    server web02 192.168.0.3:80 check inter 2000 fall 5

启动脚本
[root@localhost haproxy]# cd files/
[root@localhost files]# ls
haproxy-2.4.0.tar.gz  haproxy.cfg  install.sh
[root@localhost files]# vim start.sh
[root@localhost files]# cat start.sh 
#!/bin/bash

haproxy -f /usr/local/haproxy/conf/haproxy.cfg
/bin/bash

给权限
[root@localhost files]# chmod +x haproxy.cfg 
[root@localhost files]# chmod +x install.sh 
[root@localhost files]# chmod +x start.sh 

[root@localhost haproxy]# docker build -t haproxy:latest .
Sending build context to Docker daemon  3.601MB
Step 1/10 : FROM centos
 ---> 5d0da3dc9764
Step 2/10 : LABEL MAINTAINER "sean 
 ---> Using cache
 ---> a9bbddf967c6
Step 3/10 : ENV VERSION 2.4.0
 ---> Running in bbc44406b1df
Removing intermediate container bbc44406b1df
 ---> f2e77d473c69
Step 4/10 : COPY files/haproxy-${VERSION}.tar.gz  /usr/src/
 ---> 5071505d2710
Step 5/10 : COPY files/haproxy.cfg  /usr/local/haproxy/conf/haproxy.cfg
 ---> 3b13bf8bc7f5
Step 6/10 : COPY files/install.sh /scripts/
 ---> ede9d99a2d59
Step 7/10 : RUN ["/bin/bash","-c","/scripts/install.sh"]
 ---> Running in 2bd21abf97cf
 
 ----------分界线
 
Removing intermediate container 39dc8f49a05f
 ---> ace9d5c950df
Step 9/10 : EXPOSE 80 8189
 ---> Running in 652a0bb47227
Removing intermediate container 652a0bb47227
 ---> 69b9e73bf411
Step 10/10 : CMD ["/scripts/start.sh"]
 ---> Running in 9eb14f994691
Removing intermediate container 9eb14f994691
 ---> 1849bdc412fd
Successfully built 1849bdc412fd
Successfully tagged haproxy:latest

[root@localhost haproxy]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
haproxy      latest    1849bdc412fd   35 seconds ago   399MB
httpd        0.1       411a4b3fb4ed   6 hours ago      253MB
centos       latest    5d0da3dc9764   2 months ago     231MB

测试一下
[root@localhost haproxy]# docker run --name haproxy -dit -p 80:80 haproxy:latest
1b7508cf90befb15b745fc18f5f334a65c8a28793b74199f924f99a0142c8dbc

[root@localhost haproxy]# docker exec -it haproxy /bin/bash
[root@1b7508cf90be /]# ss -anlt
State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:80           0.0.0.0:*              
LISTEN  0       128            0.0.0.0:8189         0.0.0.0:*     

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE            COMMAND               CREATED        STATUS        PORTS                                         NAMES
1b7508cf90be   haproxy:latest   "/scripts/start.sh"   13 hours ago   Up 13 hours   0.0.0.0:80->80/tcp, :::80->80/tcp, 8189/tcp   haproxy
[root@localhost ~]# docker port haproxy
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
[root@localhost ~]# mkdir -p test/{html1,html2}
[root@localhost ~]# vim test/html1/index.html
[root@localhost ~]# cat test/html1/index.html 
这是第一台测试
[root@localhost ~]# vim test/html2/index.html
[root@localhost ~]# cat test/html2/index.html 
这是第二台测试
[root@localhost ~]# docker run -d -v /root/test/html1:/usr/local/apache/htdocs --name web1 1163582076/ll-httpd:latest
27cfae6eb744049ba6245e641d57fe560423d664e33ea7d28bd4ec77d5a4aaf3
[root@localhost ~]# docker run -d -v /root/test/html2:/usr/local/apache/htdocs --name web2 1163582076/ll-httpd:latest
54cf2568b83abfb5de60e79bfb021275f6ff18c56310f61179bc07504aae9e59
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS          PORTS                                         NAMES
54cf2568b83a   1163582076/ll-httpd:latest   "/usr/local/apache/b…"   7 seconds ago    Up 6 seconds    80/tcp, 443/tcp                               web2
27cfae6eb744   1163582076/ll-httpd:latest   "/usr/local/apache/b…"   35 seconds ago   Up 34 seconds   80/tcp, 443/tcp                               web1
2ff93fe05d58   1163582076/ll-httpd:latest   "/usr/local/apache/b…"   3 minutes ago    Up 3 minutes    80/tcp, 443/tcp                               httpd
1b7508cf90be   haproxy:latest               "/scripts/start.sh"      13 hours ago     Up 13 hours     0.0.0.0:80->80/tcp, :::80->80/tcp, 8189/tcp   haproxy
  
查看ip
[root@localhost ~]# docker inspect 27cfae6eb744

          "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    
[root@localhost ~]# docker inspect 54cf2568b83a

                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.5",

[root@localhost ~]# curl 172.17.0.4
test:web1
[root@localhost ~]# curl 172.17.0.5
test:web2

dockerfile如何加载一个docker容器 dockerfile添加host_html


dockerfile如何加载一个docker容器 dockerfile添加host_运维_02