制作镜像方式

docker commit通过修改现有的容器,将之手动构建为镜像
docker build 通过dockerfile文件,批量构建为镜像
用镜像做成容器,在容器的基础上定制一个镜像

手动制作镜像:
commit
基于busybox容器创建busybox:v1.0镜像
[root@ubuntu2004 ~]#docker commit -a li -m "init busybox" busybox busybox:v1.0
查询:
[root@ubuntu2004 ~]#docker images 
REPOSITORY                            TAG       IMAGE ID       CREATED          SIZE
busybox                               v1.0      588bb311bb05   42 seconds ago   1.24MB
运行新制作的镜像:
[root@ubuntu2004 ~]#docker run --name b3 -p 8080:80 busybox:v1.0 httpd -f

dockerfile制作镜像Ubuntu,nginx

Ubuntu:
dockerfile文件:
FROM ubuntu:22.04
LABEL Author=limanman version=ubuntu22.04-v1.0 ORG=M50
COPY sources.list /etc/apt/sources.list
RUN apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute nfs-common lrzsz tree iotop unzip zip && \
    ln -s  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV CLASS=M50 DATE=2022-10-18

RUN groupadd -g 80 www && useradd -u 80 -g www -s /sbin/nologin -M www
nginx:
下载nginx包到nginx目录下
[root@ubuntu2004 nginx]#wget https://nginx.org/download/nginx-1.22.0.tar.gz

dockerfile文件:
FROM ubuntu:22.04-20221018-v1.0
ENV VERSION=1.22.0
ENV NGINX_INSTALL_DIR=/apps/nginx
LABEL Author=limanman version=nginx-${VERSION}
RUN apt update && apt -y install gcc make  libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
ADD nginx-${VERSION}.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-${VERSION} && \
    ./configure --prefix=${NGINX_INSTALL_DIR} --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install

EXPOSE 80
COPY nginx.conf ${NGINX_INSTALL_DIR}/conf/nginx.conf
ADD index.html.tar.gz /apps/nginx/html/
COPY docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["/apps/nginx/sbin/nginx","-g","daemon off;"]
#CMD ["-g","daemon off;"]
#ENTRYPOINT ["/apps/nginx/sbin/nginx"]

写脚本测试文件:
docker-entrypoint.sh
mkdir -p /apps/nginx/conf/conf.d
cat > /apps/nginx/conf/conf.d/www.conf <<EOF
server {
    listen 80;
    server_name www.wang.org;
    root /data/website;
}
EOF

exec "$@"
加权限:
chmod +x docker-entrypoint.sh

基于镜像运行容器:
[root@ubuntu2004 nginx]#docker run -d -p 80:80 --name mynginx nginx:v1.22.0-2022-10-18

将容器上的官方编译文件拷到宿主机进行自定义编译:
[root@ubuntu2004 ubuntu]#docker cp mynginx:/apps/nginx/conf/nginx.conf /data/dockerfile/app/nginx/
worker_processes  auto;
include /apps/nginx/conf/conf.d/*.conf;
charset utf-8;                    #字符集
开启日志和压缩