首先创建nginx目录,然后进入目录编辑Dockerfile
[root@Raygussie ~]# mkdir nginx
[root@Raygussie ~]# ls
anaconda-ks.cfg nginx nginx-1.14.2.tar.gz nginx-1.19.7 nginx-1.19.7.tar.gz
[root@Raygussie ~]# cd nginx/
[root@Raygussie nginx]# ls
[root@Raygussie nginx]# vi Dockerfile
//第一行FROM指令后输入基础镜像
FROM centos
//指定镜像用户的信息
LABEL MAINTAINER='mufeng 123@qq.com'
//安装编译nginx的依赖的安装包
RUN yum -y install gcc gcc-c++ make libtool zlib-devel openssl openssl-devel pcre pcre-devel vim wget unzip zip
//创建nginx账户,设置不允许登录
RUN useradd -r -M -s /sbin/nologin nginx
//使用ADD指令解压nginx软件包
ADD /nginx-1.19.7.tar.gz /usr/local
//定义nginx工作路径
WORKDIR /usr/local/nginx-1.19.7
//编译安装nginx
RUN /bin/bash ./configure --prefix=/usr/local/nginx --with-http_ssl_module
//通过make编译安装nginx
RUN make&&make install
//定义向外暴露的端口号
EXPOSE 80
//启动容器执行指令
CMD ["/usr/local/nginx/sbin/nginx", "-g","daemon off;"]
通过docker build命令来创建镜像
[root@Raygussie ~]# docker build -t mf/web:v0.4 nginx/ Sending build context to Docker daemon 1.059MB Step 1/10 : FROM centos:latest ---> 300e315adb2f Step 2/10 : LABEL MAINTAINER='mufeng 123@qq.com' ---> Using cache ---> ef505ae03fd5 Step 3/10 : RUN yum -y install gcc gcc-c++ make libtool zlib-devel openssl openssl-devel pcre pcre-devel vim wget unzip zip ---> Using cache ---> d737b4312821 Step 4/10 : RUN useradd -r -M -s /sbin/nologin nginx ---> Using cache ---> 7710a60f69b4 Step 5/10 : ADD /nginx-1.19.7.tar.gz /usr/local ---> Using cache ---> 06ddaeed1b87 Step 6/10 : WORKDIR /usr/local/nginx-1.19.7 ---> Using cache ---> 6945223b89f3 Step 7/10 : RUN /bin/bash ./configure --prefix=/usr/local/nginx --with-http_ssl_module ---> Using cache ---> 5c2b3129a737 Step 8/10 : RUN make&&make install ---> Using cache ---> 794c64b8b14e Step 9/10 : EXPOSE 80 ---> Using cache ---> 6c2095e0a0cb Step 10/10 : CMD ["/usr/local/nginx/sbin/nginx" ,"-g" ,"daemon off;"] ---> Running in 81bc74269672 Removing intermediate container 81bc74269672 ---> 562f44bd9793 Successfully built 562f44bd9793 Successfully tagged mf/web:v0.4
运行容器,端口为80端口
[root@Raygussie ~]# docker run -it -p 80:80 --name mf3 mf/web:v0.4
在另一个终端查看端口
[root@Raygussie ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
最后打开网站,输入主机的ip,界面里出现了Welcome to nginx!