目录

dockerfile

dockerfile

dockerfile文件

以centos7.9为基础镜像构建nginx方向代理

# 使用官方的CentOS 7.9基础镜像
FROM centos:7.9.2009

# 设置维护者信息
LABEL maintainer="dan"
# 备份yum源
RUN  mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 下载国产yum源
RUN  curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 更新yum源
RUN yum clean all && yum makecache

# 创建非特权用户 
RUN groupadd -r nginx && useradd -r -g nginx -s /sbin/nologin nginx 
 
# 拷贝Nginx
COPY nginx /usr/local/nginx
RUN chmod +x /usr/local/nginx/sbin/nginx

RUN chmod -R 755 /usr/local/nginx/logs && chown -R nginx:nginx /usr/local/nginx 
 
WORKDIR /usr/local/nginx/sbin
 
VOLUME /usr/local/nginx/conf /usr/local/nginx/logs

# 暴露端口8099
EXPOSE 8099

# 设置容器启动时执行的命令
USER nginx 
CMD ["./nginx", "-g", "daemon off;"]

构建镜像

# 在dockerfile文件目录下构建
docker build -t 镜像名:镜像版本 .
# 在dockerfile文件目录下,不适用缓存构建
docker build --no-cache -t 镜像名:镜像版本 .

试运行镜像看能否达到要求

docker run -it --rm -p 宿主机端口:容器端口 -v 宿主机目录:容器目录 镜像名:镜像版本 /bin/bash