dockerfile 分层构建业务镜像
作者:忙着长大#
- 2022-10-30 广东
本文字数:1917 字
阅读完需:约 6 分钟
本文介绍如何实现业务镜像的分层构建,以 nginx 为例。相看完整的 dockerfile 直接看第三步(3)。
1、构建系统镜像,主要是将 nginx 的相关依赖打包到系统镜像中。
dockerfile
FROM ubuntu:22.04
MAINTAINER "yt 1395001789@qq.com"
#ADD sources.list /etc/apt/sources.list
#RUN apt-get install -y apt-transport-https ca-certificates curl software-properties-common
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
复制代码
构建命令
docker build -t ubuntu-base:22.04 -f dockerfile .
复制代码
2、构建 nginx 镜像
dockerfile
FROM ubuntu-base:22.04
ADD nginx-1.22.1.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.22.1 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin
复制代码
构建命令
docker build -t local/ubuntu-nginx:v1 -f dockerfile .
复制代码
3、整合 nginx 镜像构建的 dockerfile,至此 nginx 的基础镜像已经构建完成,后续可以基于此镜像将前端代码打包到镜像内。
目录结构
root@yt:~/ubuntu-nginx# ls
docker-entrypoint.sh dockerfile nginx-1.22.1.tar.gz sources.list
复制代码
dockerfile
FROM ubuntu:22.04
MAINTAINER "yt 1395001789@qq.com"
## 下载加速
#ADD 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
ADD nginx-1.22.1.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.22.1 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin
RUN groupadd -g 2088 nginx && useradd -g nginx -s /usr/sbin/nologin -u 2088 nginx && chown -R nginx.nginx /apps/nginx
ADD nginx.conf /apps/nginx/conf/
EXPOSE 80 443
## 通过 ENTRYPOINT 的方式启动nginx
#ENTRYPOINT ["/apps/nginx/sbin/nginx","-g","daemon off;"]
## 通过 ENTRYPOINT + CMD 的方式启动nginx , CMD 会作为 ENTRYPOINT 的执行参数。
#ENTRYPOINT ["/apps/nginx/sbin/nginx"]
#CMD ["-g","daemon off;"]
## 通过启动脚本方式启动nginx
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod a+x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
复制代码
sources.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
复制代码
docker-entrypoint.sh
#!/bin/bash
/apps/nginx/sbin/nginx -g "daemon off;"
复制代码
构建命令
docker build -t local/ubuntu-nginx:v2 -f dockerfile .
复制代码
4、将前端代码打包到镜像。
dockerfile
FROM local/ubuntu-nginx:v2
#RUN groupadd -g 2088 nginx && useradd -g nginx -s /usr/sbin/nologin -u 2088 nginx && chown -R nginx.nginx /apps/nginx
ADD nginx.conf /apps/nginx/conf/
ADD frontend.tar.gz /apps/nginx/html/
ENTRYPOINT ["/apps/nginx/sbin/nginx","-g","daemon off;"]
复制代码
构建命令
docker build -t local/ubuntu-nginx:v3 -f dockerfile .
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
忙着长大#
关注
还未添加个人签名 2022-02-09 加入
还未添加个人简介
评论