Docker容器

Dockerfile

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

Dockerfile规则

格式:

1.#为注释
2.指令(大写)内容(小写)
3.尽管指令是大小写不敏感的,但是,我们强烈建议指令用大写,内容用小写表示。
Dockerfile是按顺序执行Dockerfile里的指令集合的(从上到下依次执行)

每个Dockerfile的第一个非注释行指令,必须是 “FROM” 指令,用于为镜像文件构建过程中,指定基准镜像,后续的指令运行于此基准镜像所提供的运行环境中

  	实践中,基准镜像可以是任何可用镜像文件,默认情况下,docker build 会在docker主机(本地)上查找指定的镜像文件,当其在本地不存在时,则会从Docker registry(远端)上拉取所需镜像文件。
4组核心的Dockerfile指令

USER,WORKDIR

[root@localhost ~]# vim /data/dockerfile/Dockerfile
FROM docker.io/llxuan518/nginx:v1.16.6
USER nginx
WORKDIR /usr/share/nginx/html

测试

[root@localhost ~]# cd /data/dockerfile/

[root@localhost dockerfile]# docker build . -t docker.io/llxuan518/nginx:v1.16.6_user_workdir

[root@localhost dockerfile]# docker run --rm -it --name nginx123 llxuan518/nginx:v1.16.6_user_workdir /bin/bash
nginx@fbd87fe92f54:/usr/share/nginx/html$ pwd
/usr/share/nginx/html

ADD,EXPOSE( EXPOSE 向外暴露端口只是声明而已)

说明:
ADD:比COPY命令多的功能是,可以自动解压 .tar*的软件包到目标目录下,可以指定源文件为URL地址。

COPY:从Dockerfile所在目录拷贝目标文件到容器的制定目录下,可以支持通配符,如果拷贝的是目录,只拷贝目录下的子文件子目录。

[root@localhost dockerfile]# vim Dockerfile
FROM docker.io/llxuan518/nginx:v1.16.6
ADD index.html /usr/share/nginx/html/index.html
EXPOSE 80

测试

[root@localhost dockerfile]# docker build ./ -t llxuan518/nginx:v1.16.6_user_index

[root@localhost dockerfile]# docker run --rm -it --name nginx2333  -P llxuan518/nginx:v1.16.6_user_index  /bin/bash

RUN,ENV( RUN 构建镜像过程中运行的命令,ENV 设定环境变量)

ENV功能为设置环境变量

语法有两种

  1. ENV
  2. ENV = ...
    两者的区别就是第一种是一次设置一个,第二种是一次设置多个
[root@localhost dockerfile]# vim Dockerfile
FROM centos:7
ENV VER 9.11.4
RUN yum install bind-$VER -y

查看bind版本号

[root@localhost dockerfile]# yum list bind --show-duplicates

测试

[root@localhost dockerfile]# docker build ./ -t llxuan518/bind:v9.11.4_env

[root@localhost dockerfile]# docker run -it --rm --name bind_env llxuan518/bind:v9.11.4_env /bin/bash

CMD,ENTRYPOINT( CMD 使用镜像启动容器时运行的命令)

[root@localhost dockerfile]# vim Dockerfile
FROM centos:7
RUN yum install httpd -y
CMD ["httpd","-D","FOREGROUND"]

构建测试

[root@localhost dockerfile]# docker build ./ -t llxuan518/httpd:test

后台运行

[root@localhost dockerfile]# docker run --rm -d --name my-http llxuan518/httpd:test

ENTRYPOINT

[root@localhost dockerfile]# vim entrypoint.sh
#!/bin/bash
/shin/nginx -g "daemon off;"

[root@localhost dockerfile]# chmod +x entrypoint.sh

[root@localhost dockerfile]# vim Dockerfile 
FROM centos:7
ADD entrypoint.sh /entrypoint.sh
RUN yum install epel-release -q -y && yum install nginx -y
ENTRYPOINT /entrypoint.sh

测试

[root@localhost dockerfile]# docker build ./ -t llxuan518/nginx:mynginx
它不用-d后台,因为它脚本已经定义,只能前台挂着

[root@localhost dockerfile]# docker run --rm -p87:80 llxuan518/nginx:mynginx

VOLUME ["/var/www/html","data/mysql/data"] 数据卷??

范例:编译安装nginx

[root@long docker]# pwd
/opt/docker

[root@long docker]# ll
总共是4个文件
-rw-r--r-- 1 root root     890 Jul 15 02:17 Dockerfile
-rw-r--r-- 1 root root      24 Jul 14 13:06 index.html
-rw-r--r-- 1 root root 1039530 Apr 21  2020 nginx-1.18.0.tar.gz
-rw-r--r-- 1 root root    2659 Jul 15 01:32 nginx.conf

Dockerfile配置文件(文件不能有中文注释)

[root@long docker]# cat Dockerfile
#Nginx Base Image
FROM centos:7.9.2009
LABEL maintainer="lvxuan xxx@qq.com"
LABEL project="N520"

ENV password="123456"
ENV user="long"
ENV VER=1.18.0
ENV SRC=/usr/local/src
#ENV CPU="$(lscpu | awk '/^CPU\(s\):/{print $NF}')"
ENV CPU=2
ENV GROUP=80
ENV USER=80
ENV PORT=80

RUN groupadd -g ${GROUP} nginx && useradd -s /sbin/nologin -r -g ${GROUP} -u ${USER} nginx
RUN yum install vim -y && yum install -y vim wget tree  lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
ADD nginx-${VER}.tar.gz ${SRC}
RUN cd ${SRC}/nginx-${VER} && ./configure --prefix=/apps/nginx && make -j ${CPU} && make install
COPY nginx.conf /apps/nginx/conf/
ADD index.html /apps/nginx/html/index.html

VOLUME ["/static","/data1","/data2"]
RUN chown -R nginx.nginx /apps/
EXPOSE ${PORT}

#ENTRYPOINT ["/apps/nginx/sbin/nginx","-g","daemon off;"]
CMD ["/apps/nginx/sbin/nginx","-g","daemon off;"]

执行构建

[root@long docker]# docker build -t harbor.test.com/centos7:v1 .

基于构建的镜像后台启动容器

[root@long docker]# docker run -itd -p 81:80 --name nginx-v3 harbor.test.com/centos7:v1

案例:自定义Tomcat业务镜像

容器里面jdk的/etc/profile文件的环境是给普通用户使用的

镜像叠加形成

先做出CentOS系统base基础镜像

[root@long-ubuntu centos-7.9]# mkdir -p /opt/dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}}

制作Tomcat镜像叠加形成图形

创建目录

[root@long-ubuntu centos-7.9]# mkdir centos-7.9
[root@long-ubuntu centos-7.9]# pwd
/opt/dockerfile/system/centos/centos-7.9
[root@long-ubuntu centos-7.9]# ll
total 16
-rw-r--r-- 1 root root   49 Jul 16 13:29 build-command.sh
-rw-r--r-- 1 root root  275 Jul 16 13:28 Dockerfile

制作dockerfile

[root@long-ubuntu centos-7.9]# cat Dockerfile
# Centos Base Image
FROM centos:7.9.2009
LABEL maintainer="lvxuan xxx@qq.com"

RUN yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
RUN groupadd long -g 2021 && useradd long -u 2021 -g long

写个构建脚本

[root@long-ubuntu centos-7.9]# cat build-command.sh
#!/bin/bash

docker build -t centos7.9-base:v1 .

执行

[root@long-ubuntu centos-7.9]# bash build-command.sh
...
Step 4/4 : RUN groupadd long -g 2021 && useradd long -u 2021 -g long
 ---> Running in 79bcabe19504
Removing intermediate container 79bcabe19504
 ---> 873548f656bc
Successfully built 873548f656bc
Successfully tagged centos7.9-base:v1

基于自己构建出来的CentOS系统镜像构建一个JDK

[root@long-ubuntu jdk]# ll
total 141556
-rw-r--r-- 1 root root        47 Jul 16 13:41 build-command.sh
-rw-r--r-- 1 root root       398 Jul 16 13:40 Dockerfile
-rw-r--r-- 1 root root 144935989 Jul 16 13:40 jdk-8u291-linux-x64.tar.gz

制作dockerfile文件,记得上传jdk压缩包

写个构建脚本

[root@long-ubuntu jdk]# cat build-command.sh
#!/bin/bash

docker build -t jdk-base:v8.291 .

执行

[root@long-ubuntu jdk]# bash build-command.sh
...
Removing intermediate container 0c00720745fe
 ---> ede217fbdd43
Successfully built ede217fbdd43
Successfully tagged jdk-base:v8.291

拷贝docker配置文件出来

root@long-ubuntu:~# docker cp 85e509e1976a:/etc/profile .

改配置文件

[root@long-ubuntu jdk]# vim profile
# jdk
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin

修改dockerfile并重新构建

[root@long-ubuntu jdk]# cat Dockerfile
#JDK Base Image
FROM centos7.9-base:v1
LABEL maintainer="lvxuan xxx@qq.com"

ADD jdk-8u291-linux-x64.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/jdk1.8.0_291 /usr/local/jdk
ADD profile /etc/profile

ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin

RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

查看当前目录所有

[root@long-ubuntu jdk]# ll
total 141560
-rw-r--r-- 1 root root        47 Jul 16 13:41 build-command.sh
-rw-r--r-- 1 root root       423 Jul 16 14:04 Dockerfile
-rw-r--r-- 1 root root 144935989 Jul 16 13:40 jdk-8u291-linux-x64.tar.gz
-rw-r--r-- 1 root root       731 Jul 16 14:03 profile

执行

[root@long-ubuntu jdk]# bash build-command.sh
...
Removing intermediate container 0c00720745fe
 ---> ede217fbdd43
Successfully built ede217fbdd43
Successfully tagged jdk-base:v8.291

测试验证(这才是正常普通用户也有java权限看到)

[root@long-ubuntu jdk]# docker run -it --rm jdk-base:v8.291 bash
[root@e8074749c67e /]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@e8074749c67e /]# su - long
[long@e8074749c67e ~]$ pwd
/home/long
[long@e8074749c67e ~]$ id
uid=2021(long) gid=2021(long) groups=2021(long)
[long@e8074749c67e ~]$ java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)

基于上面的JDK基础镜像制作Tomcat-base基础镜像

base里所有的文件,记得上传Tomcat压缩包

[root@ubuntu-1804 tomcat8-base]# ll
-rw-r--r-- 1 root root 10312541 Jul 20 01:40 apache-tomcat-8.5.54.tar.gz
-rw-r--r-- 1 root root       52 Jul 20 01:40 build-command.sh
-rw-r--r-- 1 root root      383 Jul 20 01:38 Dockerfile

写dockerfile文件

[root@long-ubuntu tomcat-base]# vim Dockerfile
#Tomcat Base Image
FROM jdk-base:v8.291
LABEL maintainer="lvxuan xxx@qq.com"

#env
ENV TZ "Asia/Shanghai" ENV LANG en_US.UTF-8
ENV TERM xterm
ENV TOMCAT_MAJOR_VERSION 8
ENV TOMCAT_MINOR_VERSION 8.5.54
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR ${CATALINA_HOME}/webapps

#tomcat
RUN mkdir /apps
ADD apache-tomcat-8.5.54.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-8.5.54 /apps/tomcat

写个构建脚本

[root@long-ubuntu tomcat-base]# vim build-command.sh
#!/bin/bash

docker build -t tomcat-base:v8.5.54 .

执行

[root@long-ubuntu tomcat-base]# bash build-command.sh
...
Removing intermediate container 8cc5c96df0c7
 ---> 7dc7e5874247
Successfully built 7dc7e5874247
Successfully tagged tomcat-base:v8.5.54

测试 启动并设置退出就删除

[root@long-ubuntu tomcat-base]# docker run -it --rm -p 8080:8080 tomcat-base:v8.5.54 bash

容器启动后需要进入然后启动Tomcat

[root@98559cc53536 /]# /apps/tomcat/bin/catalina.sh run
Using CATALINA_BASE:   /apps/tomcat
Using CATALINA_HOME:   /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME:        /usr/local/jdk/jre

制作Tomcat业务镜像比如app1,app2

准备一个tar包或者说假设从开发哪里得到的tar包

[root@long-ubuntu tomcat-app1]# pwd
/opt/dockerfile/web/tomcat/tomcat-app1

[root@long-ubuntu tomcat]# mkdir tomcat-app1
[root@long-ubuntu tomcat]# cd tomcat-app1/

[root@long-ubuntu tomcat-app1]# mkdir myapps

[root@long-ubuntu tomcat-app1]# echo "Tomcat Web1 Page" > myapps/index.html
[root@long-ubuntu tomcat-app1]# cat myapps/index.html
Tomcat Web1 Page

# 假设(自己制作压缩包)
[root@long-ubuntu tomcat-app1]# tar zcf myapps.tar.gz myapps/*

配置dockerfile文件

# 默认路径
[root@long-ubuntu tomcat-app1]# cat Dockerfile
#Tomcat Web Image
FROM tomcat-base:v8.5.54
LABEL maintainer="lvxuan xxx@qq.com"

ADD myapps/* /apps/tomcat/webapps/myapps/
RUN chown -R long.long /apps/tomcat/

写个构建脚本

[root@long-ubuntu tomcat-app1]# vim build-command.sh
#!/bin/bash
#
docker build -t tomcat-web:app1 .

执行

[root@long-ubuntu tomcat-app1]# bash build-command.sh
...
 ---> ec9da7cf9d4d
Successfully built ec9da7cf9d4d
Successfully tagged tomcat-web:app1

所有

[root@ubuntu-1804 tomcat-app1]# ll
-rw-r--r-- 1 root root   48 Jul 20 01:47 build-command.sh
-rw-r--r-- 1 root root  158 Jul 20 01:46 Dockerfile
drwxr-xr-x 2 root root 4096 Jul 20 01:45 myapp/
-rw-r--r-- 1 root root  146 Jul 20 01:46 myapp.tar.gz

测试

# 启动容器
[root@long-ubuntu tomcat-app1]# docker run -it --rm -p 8080:8080 tomcat-web:app1 bash

# 进入容器启动Tomcat
[root@569f4535d70d /]# /apps/tomcat/bin/catalina.sh run

# false 是关闭自动解包自动部署
<Host name="localhost"  appBase="/data/tomcat/webapps"
            unpackWARs="false" autoDeploy="false">

拷贝容器配置文件出来并修改

root@long-ubuntu:~# docker cp 9c68593ce1bd:/apps/tomcat/conf/server.xml .

重新修改Dockerfile文件指向自定义路径

[root@long-ubuntu tomcat-app1]# cat Dockerfile
#Tomcat Web Image
FROM tomcat-base:v8.5.54
LABEL maintainer="lvxuan xxx@qq.com"

ADD run_tomcat.sh /apps/tomcat/bin
ADD server.xml /apps/tomcat/conf/server.xml
RUN mkdir -p /data/tomcat/webapps
ADD myapps.tar.gz /data/tomcat/webapps/
RUN chown -R long.long /apps/tomcat/ /data/tomcat/webapps

EXPOSE 8080
CMD ["/apps/tomcat/bin/run_tomcat.sh"]

准备tomcat容器启动执行脚本这样就不用Tomcat自带的启动命令了

[root@long-ubuntu tomcat-app1]# cat run_tomcat.sh
#!/bin/bash
echo "1.1.1.1 abc.test.com" >> /etc/hosts

# 不能在使用run,一定要使用start,如果使用run就会一直卡住,这样就不能达到想要的效果,比如有个pid为1的进程
su - long -c "/apps/tomcat/bin/catalina.sh start"
tail -f /etc/hosts

如果dockerfile文件有写启动脚本,用命令行启动就不要加bash直接启动即可

[root@long-ubuntu tomcat-app1]# docker run -it --rm -p 8080:8080 tomcat-web:app1
Using CATALINA_BASE:   /apps/tomcat
Using CATALINA_HOME:   /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME:        /usr/local/jdk/jre
Using CLASSPATH:       /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Tomcat started.
...
172.17.0.2      387f94ddd9dc
1.1.1.1 abc.test.com

改装后的所有文件

[root@ubuntu-1804 tomcat-app1]# ll
-rw-r--r-- 1 root root   48 Jul 20 01:47 build-command.sh
-rw-r--r-- 1 root root  343 Jul 20 01:54 Dockerfile
drwxr-xr-x 2 root root 4096 Jul 20 01:45 myapp/
-rw-r--r-- 1 root root  146 Jul 20 01:46 myapp.tar.gz
-rw-r--r-- 1 root root  274 Jul 20 01:54 run_tomcat.sh
-rw------- 1 root root 7603 Jul 20 01:53 server.xml

浏览器访问

制作第二个Tomcat-app2

可以直接拷贝方式

[root@long-ubuntu tomcat]# mkdir tomcat-app2
[root@long-ubuntu tomcat]# cp -r tomcat-app1/* tomcat-app2

修改脚本

[root@long-ubuntu tomcat-app2]# vim build-command.sh
#!/bin/bash
#
docker build -t tomcat-web:app2 .

修改tar包里面的信息或者说这个tar是开发给的tar包

[root@long-ubuntu tomcat-app2]# mkdir myapps
[root@long-ubuntu tomcat-app2]# echo "Tomcat Web2 Page" > myapps/index.html
[root@long-ubuntu tomcat-app2]# cat myapps/index.html
Tomcat Web2 Page

# 打包
[root@long-ubuntu tomcat-app2]# tar zcvf myapps.tar.gz myapps
myapps/
myapps/index.html

构建

[root@long-ubuntu tomcat-app2]# bash build-command.sh
...
 ---> 39134c7a2142
Successfully built 39134c7a2142
Successfully tagged tomcat-web:app2

测试

[root@long-ubuntu tomcat-app2]# docker run -it -p 8080:8080 tomcat-web:app2
Using CATALINA_BASE:   /apps/tomcat
Using CATALINA_HOME:   /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME:        /usr/local/jdk/jre
Using CLASSPATH:       /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Tomcat started.
...
1.1.1.1 abc.test.com

所有的镜像

[root@long-ubuntu ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat-web          app2                39134c7a2142        3 minutes ago       886MB
tomcat-web          app1                bfbb82aa0dfe        19 minutes ago      886MB
tomcat-base         v8.5.54             7dc7e5874247        13 hours ago        871MB
jdk-base            v8.291              da87eec5a41e        13 hours ago        857MB
centos7.9-base      v1                  873548f656bc        13 hours ago        497MB
centos              7.9.2009            8652b9f0cb4c        8 months ago        204MB

制作 HAproxy镜像

dockerfile镜像制作流程,先把lua和haproxy编译出来保证其能正常编译不报错在加其他参数

dockerfile配置文件

[root@long-ubuntu haproxy]# cat Dockerfile
#Haproxy Base Image
FROM centos7.9-base:v1
LABEL maintainer="lvxuan xxx@qq.com"

RUN yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel readline-devel openssl openssl-devel systemd-devel \
    net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate

ADD lua-5.3.5.tar.gz /usr/local/src
RUN cd /usr/local/src/lua-5.3.5 && make linux test

ADD haproxy-2.1.7.tar.gz /usr/local/src/
RUN cd /usr/local/src/haproxy-2.1.7 && make ARCH=x86_64 TARGET=linux-glibc \
    USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 \
    USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ && make install PREFIX=/apps/haproxy

RUN ln -s /apps/haproxy/sbin/haproxy /usr/sbin/haproxy

RUN mkdir /apps/haproxy/run
ADD haproxy.cfg /etc/haproxy/haproxy.cfg
ADD run_haproxy.sh /usr/bin
EXPOSE 80 9999

CMD ["/usr/bin/run_haproxy.sh"]

启动脚本(haproxy -f 后面跟以cfg结尾的配置就可以启动)

[root@long-ubuntu haproxy]# cat run_haproxy.sh
#!/bin/bash
echo "1.1.1.1 aa.xx.com" >> /etc/hosts

/apps/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts

构建

[root@long-ubuntu haproxy]# cat build-command.sh
#!/bin/bash
#
docker build -t haproxy-2.1.7-base:v1 .

执行

[root@long-ubuntu haproxy]# bash build-command.sh
...
 ---> 0f268f9b7022
Successfully built 0f268f9b7022
Successfully tagged haproxy-2.1.7-base:v1

所有文件如下

[root@long-ubuntu haproxy]# ll
total 2972
-rw-r--r-- 1 root root      54 Jul 17 08:36 build-command.sh
-rw-r--r-- 1 root root     883 Jul 17 08:33 Dockerfile
-rw-r--r-- 1 root root 2709999 Jul 17 07:52 haproxy-2.1.7.tar.gz
-rw-r--r-- 1 root root     655 Jul 17 08:36 haproxy.cfg
-rw-r--r-- 1 root root  303543 Jul 17 07:52 lua-5.3.5.tar.gz
-rwxr-xr-x 1 root root     126 Jul 17 08:32 run_haproxy.sh*

上传镜像到harbor仓库

[root@long-ubuntu haproxy]# docker tag haproxy-2.1.7-base:v1 harbor.longxuan.vip/m44/haproxy-2.1.7-base:v1

[root@long-ubuntu haproxy]# docker push harbor.longxuan.vip/m44/haproxy-2.1.7-base:v1

扩展 HAproxy + nginx + Tomcat

这个图缺个keepalived的虚拟VIP

nginx修改配置文件

[root@long-ubuntu nginx]# vim nginx.conf

    upstream tomcat-server {
        server 172.31.0.29:8080;
        server 172.31.0.39:8080;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /myapps {
            proxy_pass http://tomcat-server;
        }

haproxy修改配置文件

[root@long-ubuntu nginx]# cat ../haproxy/haproxy.cfg
global
chroot /apps/haproxy
stats socket /apps/haproxy/run/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
nbproc 1
pidfile /apps/haproxy/run/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms

listen stats
  mode http
  bind 0.0.0.0:9999
  stats enable
  log global
  stats uri /haproxy-status
  stats auth haadmin:123456

listen web_port
  bind 0.0.0.0:80
  mode http
  log global
  balance roundrobin
  server web1 172.31.0.29:86 check inter 3000 fall 2 rise 5
  server web2 172.31.0.39:86 check inter 3000 fall 2 rise 5

另外一台机器需要安装好docker环境,镜像可以上传到私有仓库然后下载,如果没有私有仓库也可以使用打包镜像方式传到机器

范例:使用save打包

[root@long-ubuntu nginx]# docker save f585766e2f1e > haproxy-2.1.7-base.tar.gz

# 把tar包拷贝到其他机器上
[root@long-ubuntu nginx]# scp haproxy-2.1.7-base.tar.gz 172.31.0.19:/opt

# 在其他机器导入
[root@Bj-Ubuntu ~/harbor]# docker load < /opt/haproxy-2.1.7-base.tar.gz

范例:使用私有仓库

# tomcat-app2镜像上传
[root@long-ubuntu nginx]# docker tag tomcat-web:app2 harbor.longxuan.vip/m44/tomcat-web:app2
[root@long-ubuntu nginx]# docker push harbor.longxuan.vip/m44/tomcat-web:app2

# haproxy镜像上传
[root@long-ubuntu haproxy]# docker tag haproxy-2.1.7-base:v1 harbor.longxuan.vip/m44/haproxy-2.1.7-base:v1
[root@long-ubuntu haproxy]# docker push harbor.longxuan.vip/m44/haproxy-2.1.7-base:v1

# nginx镜像上传
[root@long-ubuntu nginx]# docker tag centos7-nginx-1.18:v1 harbor.longxuan.vip/m44/centos7-nginx-1.18:v1
[root@long-ubuntu nginx]# docker push harbor.longxuan.vip/m44/centos7-nginx-1.18:v1

另外一台机器操作到私有仓库拉取镜像

[root@long-ubuntu ~]# docker pull harbor.longxuan.vip/m44/centos7-nginx-1.18:v1

[root@long-ubuntu ~]# docker pull harbor.longxuan.vip/m44/haproxy-2.1.7-base:v1

[root@long-ubuntu ~]# docker pull harbor.longxuan.vip/m44/tomcat-web:app2

基于三个镜像启动

[root@long-ubuntu ~]# docker run -itd -p 8080:8080 harbor.longxuan.vip/m44/tomcat-web:app2

[root@long-ubuntu ~]# docker run -itd -p 86:80 harbor.longxuan.vip/m44/centos7-nginx-1.18:v1

[root@long-ubuntu ~]# docker run -itd -p 80:80 -p 9999:9999 harbor.longxuan.vip/m44/haproxy-2.1.7-base:v1

浏览器验证

案例:基于官方 alpine 基础镜像制作自定义镜像

[root@long-ubuntu alpine]# ll
total 1040
-rw-r--r-- 1 root root      53 Jul 17 12:21 build-command.sh
-rw-r--r-- 1 root root     847 Jul 17 12:54 Dockerfile
-rw-r--r-- 1 root root 1039530 Jul 17 12:21 nginx-1.18.0.tar.gz
-rw-r--r-- 1 root root    2848 Jul 17 12:34 nginx.conf
-rw-r--r-- 1 root root     175 Jul 17 12:57 static.tar.gz

dockerfile配置文件

[root@long-ubuntu alpine]# cat Dockerfile
#Images alpine create
FROM alpine:latest
LABEL maintainer="lvxuan XXX@qq.com"

#COPY repositories /etc/apk/repositories
RUN sed -i -e 's/http:/https:/' /etc/apk/repositories
RUN apk update && apk add iotop gcc libgcc libc-dev libcurl libc-utils pcre-dev \
    zlib-dev libnfs make pcre pcre2 zip unzip net-tools pstree wget libevent libevent-dev iproute2

ADD nginx-1.18.0.tar.gz /opt/
RUN cd /opt/nginx-1.18.0 && ./configure --prefix=/apps/alpnginx && make && make install && ln -sv /apps/alpnginx/sbin/nginx /usr/bin/

RUN mkdir -p /data/alpnginx
RUN addgroup -g 2021 -S nginx && adduser -s /sbin/nologin -S -D -u 2021 -G nginx nginx
COPY nginx.conf /apps/alpnginx/conf/nginx.conf
ADD static.tar.gz /apps/alpnginx/html

RUN chown nginx.nginx /data/alpnginx/ /apps/alpnginx/ -R

EXPOSE 80 443
CMD ["/apps/alpnginx/sbin/nginx","-g","daemon off;"]

构建脚本

[root@long-ubuntu alpine]# cat build-command.sh
#!/bin/bash
#
docker build -t alpine-nginx-1.18:v1 .

启动测试

[root@long-ubuntu alpine]# docker run -it --rm -p 81:80 alpine-nginx-1.18:v1

案例:基于官方 Ubuntu 基础镜像制作自定义镜像

[root@long-ubuntu ubuntu]# ll
total 1044
-rw-r--r-- 1 root root      57 Jul 17 13:13 buil-command.sh
-rw-r--r-- 1 root root    1138 Jul 17 13:30 Dockerfile
-rw-r--r-- 1 root root 1039530 Jul 17 13:11 nginx-1.18.0.tar.gz
-rw-r--r-- 1 root root    2848 Jul 17 13:12 nginx.conf
-rw-r--r-- 1 root root     814 Jul 17 13:29 sources.list
-rw-r--r-- 1 root root     175 Jul 17 13:12 static.tar.gz

Dockerfile配置文件(目前遇到不是使用官方的apt源就会报错)

[root@long-ubuntu ubuntu]# cat Dockerfile
#Images ubuntu-18.04 create
FROM ubuntu:18.04
LABEL maintainer="lvxuan xxx@qq.com"

COPY sources.list /etc/apt/sources.list
RUN apt update && apt install -y iproute2 ntpdate tcpdump telnet traceroute \
    nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev \
    zlib1g-dev ntpdate tcpdump telnet traceroute gcc openssh-server lrzsz tree \
    openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet \
    traceroute iotop unzip zip make \
    && touch /tmp/linux.txt

ADD nginx-1.18.0.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-1.18.0 \
    && ./configure --prefix=/apps/ubtnginx \
    && make && make install && ln -sv /apps/ubtnginx/sbin/nginx /usr/bin \
    && rm -rf /usr/local/src/nginx-1.18.0 \
    && rm -rf /usr/local/src/nginx-1.18.0.tar.gz

ADD nginx.conf /apps/ubtnginx/conf/nginx.conf
ADD static.tar.gz /apps/ubtnginx/html

RUN groupadd -g 2022 nginx && useradd -g nginx -s /usr/sbin/nologin -u 2022 nginx \
    && mkdir -p /data/ubtnginx \
    && chown -R nginx.nginx /apps/ubtnginx /data/ubtnginx

EXPOSE 80 443
CMD ["/apps/ubtnginx/sbin/nginx","-g","daemon off;"]

构建脚本

[root@long-ubuntu ubuntu]# cat buil-command.sh
#!/bin/bash
#
docker build -t ubuntu-18.04-nginx-18:v1 .

执行构建

[root@long-ubuntu ubuntu]# bash buil-command.sh
 ---> 6302f193fb87
Successfully built 6302f193fb87
Successfully tagged ubuntu-18.04-nginx-18:v1

启动容器验证

[root@long-ubuntu ubuntu]# docker run -it --rm -p 82:80 ubuntu-18.04-nginx-18:v1
这篇关于Dockerfile的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!