全步骤:
[[email protected] ~]# yum install yum-utils device-mapper-persistent-data lvm2 -y
[[email protected] ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[[email protected] ~]# yum install -y docker-ce
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# systemctl start docker.service
[[email protected] ~]# systemctl enable docker.service
[[email protected] ~]# tee /etc/docker/daemon.json <<-‘EOF‘
{
"registry-mirrors": ["https://w1ogxqvl.mirror.aliyuncs.com"]
}
EOF
[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl restart docker
[[email protected] ~]# docker pull centos:7
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 5e35e350aded 6 weeks ago 203MB
[[email protected] ~]# cd /opt/
[[email protected] opt]# mkdir nginx
[[email protected] opt]# cd nginx/
[[email protected] nginx]# vim Dockerfile
#基于基础镜像
FROM 5e35e350aded
#用户信息
MAINTAINER The is nginx <zjx>
#添加环境包
RUN yum install -y proc-devel gcc gcc-c++ zlib zlib-devel make openssl-devel wget
#下载nginx软件包
RUN wget http://nginx.org/download/nginx-1.9.7.tar.gz
RUN tar zxvf nginx-1.9.7.tar.gz
#指定工作目录
WORKDIR nginx-1.9.7/
RUN ./configure --prefix=/usr/local/nginx && make && make install
#指定http和https端口
EXPOSE 80
EXPOSE 443
#关闭守护进程
RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf
WORKDIR /root/nginx
#添加宿主机中run.sh到容器中
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
[[email protected] nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
#创建新镜像
[[email protected] nginx]# docker build -t nginx:new .
#启动容器进行测试
[[email protected] nginx]# docker run -d -P nginx:new
1e42f3a46caf349a964c4304b900ecb406102e3dc7e5beae16293f071470e54b
[[email protected] nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e42f3a46caf nginx:new "/run.sh" 8 seconds ago Up 7 seconds 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp hardcore_cerf
验证:回到宿主机在浏览器中访问CentOS的IP地址的32769端口,看是否可以访问Nginx主页
原文地址:https://blog.51cto.com/14464303/2461713
时间: 2024-10-03 15:15:34