vim Dockerfile
内容如下:
FROM centos:centos7.8.2003 ##选择centos7.8版本作为基础镜像
LABEL maintainer="zhangtiaze <1185673631@qq.com>" version="v1.0" ##作者信息
RUN rm -f /etc/localtime && ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime && ##修改容器内的时间为上海时间
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak && ## 把官方的源mv改名作为备份
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && ##下载阿里云源
sed -ri '/mirrors.aliyuncs.com|mirrors.cloud.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && ##把阿里内网源删除
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && ##下载阿里的epel源
yum -y install wget vim net-tools iproute psmisc bash-completion lsof tcpdump tree file && ##安装常用工具
rm -rf /var/cache/yum/* && useradd -u 80 -r -s /sbin/nologin www ##清空yum缓存并且添加www账户
docker build -t centos7.8:prod_v1.0 . 制作基于centos7.8的镜像
利用centos镜像制作nginx镜像 ##再利用刚刚做好的镜像部署nginx
mkdir nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
vim Dockerfile
内容如下:
FROM centos7.8:prod_v1.0
LABEL maintainer="liyusheng <29728496@qq.com>" version="v1.0"
ADD nginx-1.18.0.tar.gz /usr/local/src/
RUN yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel &&
rm -rf /var/cache/yum/* && cd /usr/local/src/nginx-1.18.0 &&
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module
--with-http_realip_module --with-http_sub_module && make && make install && mkdir -p /data/web &&
echo "nginx is running in docker" > /data/web/index.html &&
sed -ri 's@#(user )nobody@1www@' /usr/local/nginx/conf/nginx.conf &&
sed -ri 's@(worker_processes )1@1auto@' /usr/local/nginx/conf/nginx.conf &&
sed -ri 's@#(error_log logs/error.log )info@1warn@' /usr/local/nginx/conf/nginx.conf &&
sed -ri '21,25s@#@@' /usr/local/nginx/conf/nginx.conf &&
sed -ri '44s@html@/data/web@' /usr/local/nginx/conf/nginx.conf &&
sed -ri '3a daemon off;' /usr/local/nginx/conf/nginx.conf
EXPOSE 80 443
CMD /usr/local/nginx/sbin/nginx
docker build -t centos7.8:nginx_v1.0 .
运行一个nginx容器
docker run -d -it --name nginx1 centos7.8:nginx_v1.0
测试
curl 172.17.0.2 ##测试获取页面信息