Docker分层打镜像

1自定义Centos基础镜像

[root@cent7-yzil ~]# docker pull centos:7.9.2009

[root@cent7-yzil ~]# mkdir /opt/dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}} -pv

[root@cent7-yzil ~]# cd /opt/dockerfile/system/centos/
[root@cent7-yzil centos]# cat Dockerfile 
FROM centos:7.9.2009
LABEL maintainer "yzl calm_yzl@163.com"
RUN yum install epel-release -y && yum -y install vim-enhanced tcpdump lrzsz tree telnet bash-completion net-tools wget bzip2 lsof tmux man-pages zip unzip nfs-utils gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl  openssl-devel systemd-devel zlib-devel screen  strace redhat-lsb-core

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

[root@cent7-yzil centos]# cat build-command.sh 
#!/bin/bash
docker build -t centos7.9.2009:v1 .

[root@cent7-yzil centos]# bash build-command.sh 

[root@cent7-yzil centos]# docker images 
REPOSITORY       TAG        IMAGE ID       CREATED         SIZE
centos7.9.2009   v1         74f3c49ec716   3 minutes ago   579MB

2手动制作基于centos底层镜像编译nginx镜像

#进入到指定的Dockerfile目录
[root@cent7-yzil opt]# cd dockerfile/web/nginx/
[root@cent7-yzil nginx]# pwd
/opt/dockerfile/web/nginx
#编写Dockerfile
[root@cent7-yzil nginx]# cat Dockerfile 
FROM  centos7.9.2009:v1 
LABEL maintainer "yzl calm_yzl@163.com"
ADD nginx-1.18.0.tar.gz /usr/local/src/
RUN groupadd -g 1000 nginx && useradd -g 1000 -u 1000 nginx
RUN cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module && make && make install
RUN cd /apps/nginx/
#ADD nginx.conf /apps/nginx/conf/nginx.conf
RUN ln -s /apps/nginx/sbin/nginx /usr/bin
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]

[root@cent7-yzil nginx]# ls
build-command.sh  Dockerfile  nginx-1.18.0.tar.gz

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

[root@cent7-yzil nginx]# bash build-command.sh 

[root@cent7-yzil nginx]# docker images
REPOSITORY       TAG        IMAGE ID       CREATED          SIZE
nginx-1.18       v1         b44b5d8d58d3   26 seconds ago   612MB

#验证是否成功,浏览器打开http://10.0.0.123/
[root@cent7-yzil nginx]# docker run -it -d -p 80:80 -p 443:443 --name nginx-1.18-v1 nginx-1.18:v1 
#导出镜像nginx的配置文件到nginx下目录,重新改配置在重新打镜像
[root@cent7-yzil nginx]# docker run -it --rm nginx-1.18:v1 bash

[root@cent7-yzil nginx]# docker run -it --rm nginx-1.18:v1 bash
[root@fb76e408d239 /]# ls /apps/nginx/conf/nginx.conf
/apps/nginx/conf/nginx.conf
[root@fb76e408d239 /]# 

[root@cent7-yzil nginx]# docker cp fb76e408d239:/apps/nginx/conf/nginx.conf .
[root@cent7-yzil nginx]# ls
build-command.sh  Dockerfile  nginx-1.18.0.tar.gz  nginx.conf

[root@cent7_6 ~]# scp /etc/nginx/nginx.conf 10.0.0.123:/opt/dockerfile/web/nginx/

#修改Dockerfile,取消复制nginx
[root@cent7-yzil nginx]# cat Dockerfile 
FROM  centos7.9.2009:v1 
LABEL maintainer "yzl calm_yzl@163.com"

ADD nginx-1.18.0.tar.gz /usr/local/src/
RUN groupadd -g 1000 nginx && useradd -g 1000 -u 1000 nginx
RUN cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module && make && make install
RUN cd /apps/nginx/
ADD nginx.conf /apps/nginx/conf/nginx.conf    #取消这行注释
RUN ln -s /apps/nginx/sbin/nginx /usr/bin
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]
#nginx配置更改
[root@cent7-yzil nginx]# egrep -v "^#|^$" nginx.conf 
user nginx;
worker_processes auto;
error_log /apps/nginx/logs/error.log;
pid /usr/nginx/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /apps/nginx/logs/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    include             /apps/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /apps/nginx/conf.d/*.conf;
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /apps/nginx/html;
        # Load configuration files for the default server block.
        include /apps/nginx/default.d/*.conf;
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

3自定义tomcat业务镜像

[root@cent7-yzil ~]# cd /opt/dockerfile/web/jdk/
[root@cent7-yzil jdk]# ls
build-command.sh  Dockerfile  jdk-8u291-linux-x64.tar.gz  profile

[root@cent7_6 ~]# scp /etc/profile 10.0.0.123:/opt/dockerfile/web/jdk/

[root@cent7-yzil jdk]# cat build-command.sh 
#!/bin/bash
docker build -t jdk-base:v8.291 .

#第一种
[root@cent7-yzil jdk]# cat Dockerfile 
#JDK Base image
FROM  centos7.9.2009:v1
LABEL maintainer "yzl calm_yzl@163.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


#第二种 $前需要转移 \$,要不然会出错
[root@cent7-yzil jdk]# cat Dockerfile 
#JDK Base image
FROM  centos7.9.2009:v1
LABEL maintainer "yzl calm_yzl@163.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
RUN echo "export JAVA_HOME=/usr/local/jdk" > /etc/profile.d/jdk.sh
RUN echo "export PATH=$PATH:\$JAVA_HOME/bin" >> /etc/profile.d/jdk.sh
RUN echo "JRE_HOME=\$JAVA_HOME/jre" >> /etc/profile.d/jdk.sh
RUN echo "export CLASSPATH=\$JAVA_HOME/lib/:\$JRE_HOME/lib/" /etc/profile.d/jdk.sh
RUN source /etc/profile.d/jdk.sh
#验证是否安装成功
[root@cent7-yzil jdk]# docker run -it --rm jdk-bash:v8.291 bash
[root@80e586822463 /]# 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)

4从JDK镜像构建tomcat 8 Base 镜像5

[root@cent7-yzil tomcat]# ls
apache-tomcat-8.5.64.tar.gz  build-command.sh  Dockerfile

#第一种
[root@cent7-yzil tomcat]# cat Dockerfile 
#Tomcat Base
FROM jdk-base:v8.291
LABEL maintainer "yzl calm_yzl@163.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.64
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR ${CATALINA_HOME}/webapps

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

RUN groupadd tomcat -g 2000 && useradd tomcat -u 2000 -g tomcat
RUN chown tomcat.tomcat /apps/ -R

#第二种
[root@cent7-yzil tomcat]# cat Dockerfile 
#Tomcat Base
FROM jdk-base:v8.291
LABEL maintainer "yzl calm_yzl@163.com"

RUN echo "PATH=/apps/tomcat/bin:$PATH" > /etc/profile.d/tomcat.sh
RUN source /etc/profile.d/tomcat.sh

#tomcat
RUN mkdir /apps
ADD apache-tomcat-8.5.64.tar.gz /apps
RUN ln -s /apps/apache-tomcat-8.5.64/ /apps/tomcat

RUN groupadd tomcat -g 2000 && useradd tomcat -u 2000 -g tomcat
RUN chown tomcat.tomcat /apps/ -R


[root@cent7-yzil tomcat]# cat build-command.sh 
#/bin/bash
docker build -t tomcat-base:v8.5.64 .

[root@cent7-yzil tomcat]# bash build-command.sh 

#测试是否可以访问http://10.0.0.123:8080/
[root@cent7-yzil ~]# docker run -it -p 8080:8080 -d tomcat-base:v8.5.64
#内部启动tomcat
[root@cent7-yzil ~]# docker exec -it 0e0bf1ed74fc bash
[root@0e0bf1ed74fc /]# startup.sh

5tomcat镜像  

[root@cent7-yzil tomcat]# cat Dockerfile 
#Tomcat Base
FROM jdk-base:v8.291
LABEL maintainer "yzl calm_yzl@163.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.64
RUN echo "PATH=/usr/local/jdk/bin:/apps/tomcat/bin:$PATH" > /etc/profile.d/tomcat.sh   #变量传给普通用户
RUN source /etc/profile.d/tomcat.sh
#ENV CATALINA_HOME /apps/tomcat
#ENV APP_DIR ${CATALINA_HOME}/webapps

ENV JAVA_HOME /usr/local/jdk             #变量传给root用户
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin

#tomcat
RUN mkdir /apps
ADD apache-tomcat-8.5.64.tar.gz /apps
ADD profile /etc/
RUN ln -s /apps/apache-tomcat-8.5.64 /apps/tomcat

RUN groupadd tomcat -g 2000 && useradd tomcat -u 2000 -g tomcat
RUN chown tomcat.tomcat /apps/ -R

CMD { /apps/tomcat/bin/catalina.sh start; tail -f /etc/hosts; }    #开机启动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

y_zilong

一分钱的肯定

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值