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 ~]
/opt/haproxy/
├── Dockerfile
└── files
├── haproxy-2.4.0.tar.gz
├── haproxy.cfg
├── install.sh
└── start.sh
1 directory, 5 files
[root@localhost ~]
test/
├── html1
│ └── index.html
└── html2
└── index.html
2 directories, 2 files
[root@localhost ~]
[root@localhost ~]
[root@localhost haproxy]
编写Dockerfile
[root@localhost haproxy]
[root@localhost haproxy]
FROM centos
LABEL MAINTAINER "sean 1163582076@qq.com"
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]
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]
global
log 127.0.0.1 local0 info
maxconn 20480
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
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 admin if TRUE
stats refresh 30s
listen webcluster
bind 0.0.0.0:80
mode tcp
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]
[root@localhost files]
haproxy-2.4.0.tar.gz haproxy.cfg install.sh
[root@localhost files]
[root@localhost files]
haproxy -f /usr/local/haproxy/conf/haproxy.cfg
/bin/bash
给权限
[root@localhost files]
[root@localhost files]
[root@localhost files]
[root@localhost haproxy]
Sending build context to Docker daemon 3.601MB
Step 1/10 : FROM centos
---> 5d0da3dc9764
Step 2/10 : LABEL MAINTAINER "sean 1163582076@qq.com"
---> 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]
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]
1b7508cf90befb15b745fc18f5f334a65c8a28793b74199f924f99a0142c8dbc
[root@localhost haproxy]
[root@1b7508cf90be /]
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 ~]
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 ~]
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
这是第一台测试
[root@localhost ~]
[root@localhost ~]
这是第二台测试
[root@localhost ~]
27cfae6eb744049ba6245e641d57fe560423d664e33ea7d28bd4ec77d5a4aaf3
[root@localhost ~]
54cf2568b83abfb5de60e79bfb021275f6ff18c56310f61179bc07504aae9e59
[root@localhost ~]
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 ~]
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
[root@localhost ~]
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.5",
[root@localhost ~]
test:web1
[root@localhost ~]
test:web2
|