Docker容器

Docker(二) 基本应用——docker安装nginx

本文主要是介绍Docker(二) 基本应用——docker安装nginx,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

作为一个最基本的练习,尝试在docker中部署运行nginx。

 

第一步:在仓库中搜索nginx镜像

docker search nginx

[root@aris ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15311     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2059                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   815                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   233                  
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   151                  
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   138                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        131                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   105                  [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   70                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   56                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   55                   
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  46                   
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   24                   [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   19                   
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   16                   
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   15                   
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
bitwarden/nginx                   The Bitwarden nginx web server acting as a r…   11                   
mailu/nginx                       Mailu nginx frontend                            9                    [OK]
devilbox/nginx-stable             Devilbox's Nginx stable (based on official N…   4                    
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          2                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]
arnau/nginx-gate                  Docker image with Nginx with Lua enabled on …   1                    [OK]
centos/nginx-110-centos7          Platform for running nginx 1.10 or building …   0                    
[root@aris ~]# 

 

第二步:下载镜像

docker pull nginx

[root@aris ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
33847f680f63: Already exists 
dbb907d5159d: Pull complete 
8a268f30c42a: Pull complete 
b10cf527a02d: Pull complete 
c90b090c213b: Pull complete 
1f41b2f2bf94: Pull complete 
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@aris ~]# 

 

第三步:运行容器

[root@aris ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    08b152afcfae   3 weeks ago    133MB
mysql        latest    c60d96bd2b77   3 weeks ago    514MB
centos       latest    300e315adb2f   8 months ago   209MB
[root@aris ~]# docker run -d --name "nginx01" -p 9080:80 nginx
27b55924e9433ab531bb4e68dfe9bff2ae3e09df92766d0da1f300f15d86d7ec
[root@aris ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
27b55924e943   nginx     "/docker-entrypoint.…"   4 seconds ago   Up 4 seconds   0.0.0.0:9080->80/tcp, :::9080->80/tcp   nginx01
[root@aris ~]# 

这里指定启动容器名称为nginx01, -d在后台运行,使用的镜像为nginx,并且将宿主机器的9080端口映射到容器内部的80端口(nginx的默认端口)。

 

第四部:测试容器中运行的服务

使用curl http://localhost:9080访问nginx

[root@aris ~]# curl http://localhost:9080
<!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>
[root@aris ~]# 

可以看到容器成功启动运行。

 

第五步:进入容器查看nginx相关配置:

[root@aris ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
27b55924e943   nginx     "/docker-entrypoint.…"   5 minutes ago   Up 5 minutes   0.0.0.0:9080->80/tcp, :::9080->80/tcp   nginx01
[root@aris ~]# docker exec -it nginx01 /bin/bash
root@27b55924e943:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@27b55924e943:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@27b55924e943:/# 

这时候就可以像在本地操作nginx一样修改nginx配置,但这是一种不好的做法,最好的做法是使用卷挂载的方式,将配置文件放到宿主机挂载的目录上,从而在宿主机上修改容器内部nginx服务使用的配置文件。

 

第六步:在宿主机查看容器日志:

[root@aris ~]# docker logs -tf --tail 10 27b55924e943
2021-08-13T10:23:41.573735002Z /docker-entrypoint.sh: Configuration complete; ready for start up
2021-08-13T10:23:41.580405254Z 2021/08/13 10:23:41 [notice] 1#1: using the "epoll" event method
2021-08-13T10:23:41.580426690Z 2021/08/13 10:23:41 [notice] 1#1: nginx/1.21.1
2021-08-13T10:23:41.580429748Z 2021/08/13 10:23:41 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
2021-08-13T10:23:41.580432080Z 2021/08/13 10:23:41 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
2021-08-13T10:23:41.580434272Z 2021/08/13 10:23:41 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021-08-13T10:23:41.580527109Z 2021/08/13 10:23:41 [notice] 1#1: start worker processes
2021-08-13T10:23:41.580572391Z 2021/08/13 10:23:41 [notice] 1#1: start worker process 32
2021-08-13T10:23:41.581302993Z 2021/08/13 10:23:41 [notice] 1#1: start worker process 33
2021-08-13T10:26:09.199705823Z 172.17.0.1 - - [13/Aug/2021:10:26:09 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
2021-08-13T10:36:24.720809637Z 10.211.55.2 - - [13/Aug/2021:10:36:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.1" "-"

可以实时看到日志刷新输出。

这篇关于Docker(二) 基本应用——docker安装nginx的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!