【实验环境】

1
2
3
4
5
root@ubuntu18:~# docker --version
Docker version 24.0.2, build cb74dfc

root@ubuntu18:~# cat /etc/issue
Ubuntu 18.04.6 LTS \n \l

【所需文件】

1
2
root@ubuntu18:/data/dockerfile/nginx# ls
Dockerfile index.html nginx-1.18.0.tar.gz nginx.conf sources.list

【Dockerfile文件】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@ubuntu18:/data/dockerfile/nginx# cat Dockerfile
FROM ubuntu:18.04
LABEL author="harry wu"

ADD sources.list /etc/apt/sources.list

RUN apt update && apt -y install iproute2 ntpdate tcpdump nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc openssh-server iotop unzip zip make

ADD nginx-1.18.0.tar.gz /usr/local/src/


RUN mkdir -p /apps/nginx && useradd -r -s /sbin/nologin nginx && cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/apps/nginx/ --user=nginx --group=nginx && make && make install && ln -s /apps/nginx/sbin/nginx /usr/sbin/nginx && chown -R nginx:nginx /apps/nginx/ && mkdir -p /apps/nginx/run && mkdir /apps/nginx/html/myapp

ADD nginx.conf /apps/nginx/conf/
ADD index.html /apps/nginx/html/myapp/

EXPOSE 80 8008

CMD ["nginx","-g","daemon off;"]

【结果展示】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 根据 Dockerfile 文件构建镜像
docker build -t registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver:nginx_1.18_20231128 .

# 查看构建完成的镜像
root@ubuntu18:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver nginx_1.18_20231128 67ddb6dd4c78 5 days ago 396MB

# 登录个人仓库并将镜像上传
root@ubuntu18:~# docker login --username=wuhaolam registry.cn-hangzhou.aliyuncs.com
Password:
root@ubuntu18:~# docker push registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver:nginx_1.18_20231129

# 运行镜像成为容器
root@ubuntu18:~# docker run -itd --restart=always -p 8008:80 registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver:nginx_1.18_20231129
root@ubuntu18:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2f1fc8fae00 registry.cn-hangzhou.aliyuncs.com/wuhaolam/myserver:nginx_1.18_20231129 "nginx -g 'daemon of…" 5 days ago Up 5 days 8008/tcp, 0.0.0.0:8008->80/tcp, :::8008->80/tcp stoic_hoover

# 浏览器界面展示
image-20231205153749924