:
Dockerfile实战
Dockerfile制作基于基础镜像的nginx镜像
[root@kv1 ~]#mkdir -p /data/dockerfile/{web/{nginx,apache,tomcat,jdk},system/{centos,ubuntu,alpine,debian}} -p
[root@kv1 ~]#docker pull rockylinux:8.9.20231119-minimal
[root@kv1 ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rockylinux 8.9.20231119-minimal a45448f0eb32 4 months ago 93.3MB
[root@kv1 centos]#cat Dockerfile
FROM rockylinux:8.9.20231119
LABEL maintainer="yaya"
Run sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/Rocky-*.repo\
&& dnf makecache \
&& yum -y install vim-enhanced tcpdump lrzsz tree telnet bash-completion net-tools wget curl bzip2 lsof zip unzip nfs-utils gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel \
&& yum clean all \
&& rm -f /etc/localtime \
&& ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@kv1 centos]#cat build.sh
#!/bin/bash
docker build -t testrocky-8.9:v1 .
[root@kv1 centos]#chmod +x build.sh
[root@kv1 centos]#./build.sh
[root@kv1 nginx]#cat Dockerfile
FROM testrocky-8.9:v1
Label maintainer="yaya"
Env version 1.22.1
EXPOSE 80
Add nginx-$version.tar.gz /usr/local/app/
Run useradd -r -s /sbin/nologin nginx && cd /usr/local/app/nginx-$version && ./configure --prefix=/apps/nginx && make && make install && mkdir -p /apps/nginx/conf.d/ && mkdir -p /apps/nginx/run/
COPY nginx.conf /apps/nginx/conf/nginx.conf
COPY www.yaya.com.conf /apps/nginx/conf.d/www.yaya.com.conf
COPY index.html /apps/nginx/html/index.html
Run ln -s /apps/nginx/sbin/nginx /usr/bin/nginx
CMD ["nginx","-g","daemon off;"]
[root@kv1 nginx]#cat nginx.conf
worker_processes 1;
pid /apps/nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /apps/nginx/conf.d/*.conf;
}
[root@kv1 nginx]#cat index.html
this page from testrocky-8.9-v1-nginx
[root@kv1 nginx]#cat www.yaya.com.conf
server {
listen 80;
root /apps/nginx/html;
}
基于nginx的优化
第一次构建镜像nginx是在rockylinux中构建的,设想如果移植到apline上,可能镜像占用的空间小一些,然后就移植到apline系统上
[root@kv1 nginx]#cat Dockerfile
FROM testrocky-8.9:v1 as builder
Label maintainer="yaya"
Env version 1.22.1
EXPOSE 80
Add nginx-$version.tar.gz /usr/local/app/
Run useradd -r -s /sbin/nologin nginx && cd /usr/local/app/nginx-$version && ./configure --prefix=/apps/nginx && make && make install && mkdir -p /apps/nginx/conf.d/ && mkdir -p /apps/nginx/run/
Copy 1.sh /apps/nginx/sbin/2.sh
COPY nginx.conf /apps/nginx/conf/nginx.conf
COPY www.yaya.com.conf /apps/nginx/conf.d/www.yaya.com.conf
COPY index.html /apps/nginx/html/index.html
From alpine:20240329
Run sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/' /etc/apk/repositories && apk update && addgroup -S nginx && adduser -S -s /sbin/nologin -G nginx nginx
Copy --from=builder /apps/nginx /apps/nginx
Run ln -sf /apps/nginx/sbin/nginx /usr/bin/nginx
Run chown -R nginx.nginx /apps/nginx/
CMD ["nginx","-g","daemon off;"]
[root@kv1 nginx]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
testrocky-8.9-nginx v4.0 4e7a3ace3288 16 hours ago 20.2MB
testrocky-8.9-nginx v1.0 a64e83476c90 16 hours ago 477MB
查看镜像确实变小很多,但是启动时发生了问题
发现在apline系统中,找不到nginx命令。初步怀疑是环境变量问题,然后添加了环境变量,发现还是not found nginx,然后怀疑是不是apline系统中无法执行脚本,然后在父镜像相同目录中添加了一个简单脚本,发现可以执行。然后单独执行nginx的时候。发现报错了。怀疑不同的操作系统应用所依赖的底层环境不一样
多阶段构建
多阶段构建golang应用
[root@kv1 go-hello]#cat hello.go
package main
import (
"fmt"
"time"
)
func main() {
for {
fmt.Println("hello world!")
time.Sleep(time.Second)
}
}
第一次
[root@kv1 go-hello]#cat Dockerfile
From golang:alpine3.19
COPY hello.go /opt
Workdir /opt
run go build hello.go
CMD "./hello"
[root@kv1 go-hello]#docker build -t go-hello:v1.0 .
[root@kv1 go-hello]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go-hello v1.0 146d55b482de About a minute ago 259MB
第二次优化
[root@kv1 go-hello]#cat Dockerfile
From golang:alpine3.19 as builder
COPY hello.go /opt
Workdir /opt
run go build hello.go
From alpine:20240329
Copy --from=builder /opt/hello /hello
CMD "./hello"
[root@kv1 go-hello]#docker build -t go-hello:v2.0 .
[root@kv1 go-hello]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go-hello v2.0 2eb275035e9c 8 seconds ago 9.64MB
go-hello v1.0 146d55b482de 11 minutes ago 259MB
第三次优化
[root@kv1 go-hello]#cat Dockerfile
From golang:alpine3.19 as builder
COPY hello.go /opt
Workdir /opt
run go build hello.go
From scratch
Copy --from=builder /opt/hello /hello
CMD "./hello"
[root@kv1 go-hello]#docker build -t go-hello:v3.0 .
[root@kv1 go-hello]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go-hello v3.0 0c06e60ae5e1 5 seconds ago 1.89MB
go-hello v2.0 2eb275035e9c 24 minutes ago 9.64MB
go-hello v1.0 146d55b482de 35 minutes ago 259MB