Dockerfile实战

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

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值