Dockerfile基于centos镜像编译安装nginx

nginx实例(基于centos镜像)

从网上拉取centos镜像

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete 
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
leidazhuang/web   v0.3      613fd44bad96   3 days ago     1.23MB
leidazhuang/web   v0.2      6c5652212d19   3 days ago     1.23MB
busybox           latest    b97242f89c8a   7 weeks ago    1.23MB
httpd             latest    683a7aad17d3   7 weeks ago    138MB
centos            latest    300e315adb2f   2 months ago   209MB

编辑Dockerfile文件

[root@localhost ~]# vim nginx/Dockerfile 

FROM centos
  
LABEL MAINTAINER='leidazhuang 123@qq.com'

RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel

RUN  useradd -r -M -s /sbin/nologin nginx

ADD /nginx-1.19.7.tar.gz /usr/local

WORKDIR /usr/local/nginx-1.19.7

RUN ./configure --prefix=/usr/local/nginx --with-http_ssl_module

RUN make&&make install

EXPOSE 80

CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

通过Dockerfile文件build一个镜像

[root@localhost ~]# docker build -t nginx:v0.1 /root/nginx/

 ---> Running in 0612de4d1f54
Removing intermediate container 0612de4d1f54
 ---> a11f74ce1989
Successfully built a11f74ce1989
Successfully tagged nginx:v0.1

//查看创建好的镜像
[root@localhost nginx]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
nginx             v0.1      a11f74ce1989   3 minutes ago   392MB
leidazhuang/web   v0.3      613fd44bad96   3 days ago      1.23MB
leidazhuang/web   v0.2      6c5652212d19   3 days ago      1.23MB
busybox           latest    b97242f89c8a   7 weeks ago     1.23MB
httpd             latest    683a7aad17d3   7 weeks ago     138MB
centos            latest    300e315adb2f   2 months ago    209MB

使用docker run运行一个容器,开放80端口

[root@localhost nginx]# docker run -it -p 80:80 nginx:v0.1

#正常运行

打开一个终端,查看本机状态

[root@localhost ~]# 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                     [::]:*  

测试网页

[root@localhost ~]# curl 192.168.110.20
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

通过网页访问

posted @ 2021-03-05 15:10  我爱吃芹菜~  阅读(540)  评论(0编辑  收藏  举报
Title