7. Docker安装Nginx
7.1. 在DockerHub上搜索nginx
7.2. 下载nginx镜像
[root@iZ8vb6lqqya4rvreq9ra6gZ /]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@iZ8vb6lqqya4rvreq9ra6gZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 weeks ago 141MB
centos latest 5d0da3dc9764 4 months ago 231MB
[root@iZ8vb6lqqya4rvreq9ra6gZ /]#
7.3. 启动容器
# 命令详解
-d 后台运行
--name 给容器命名
-p 暴露端口,3031是宿主机暴露给外网访问的端口,80是nginx在容器中的端口
[root@iZ8vb6lqqya4rvreq9ra6gZ /]# docker run -d --name nginx01 -p 3031:80 nginx
040788c26e85114b9ad1aa6bc3651e9c25d82b2b900df5d12bee8dcc5d850b7b
[root@iZ8vb6lqqya4rvreq9ra6gZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
040788c26e85 nginx "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:3031->80/tcp, :::3031->80/tcp nginx01
[root@iZ8vb6lqqya4rvreq9ra6gZ /]#
7.4. 测试
[root@iZ8vb6lqqya4rvreq9ra6gZ /]# curl localhost:3031
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
Welcome to nginx!
<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 rel="nofollow" href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a rel="nofollow" href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
在浏览器访问:
7.5. 查看nginx配置信息
[root@iZ8vb6lqqya4rvreq9ra6gZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
040788c26e85 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:3031->80/tcp, :::3031->80/tcp nginx01
[root@iZ8vb6lqqya4rvreq9ra6gZ /]# docker exec -it 040788c26e85 /bin/bash
root@040788c26e85:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@040788c26e85:/# cd /etc/nginx/
root@040788c26e85:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@040788c26e85:/etc/nginx#
思考一个问题:
nginx.conf 配置文件是在容器内部的,如果每次都去容器中去修改nginx.conf 配置文件,会不会太麻烦了?
因此,我们后面会使用数据卷技术来解决这个问题,让我们在宿主机上改了nginx.conf 配置文件,容器中的nginx可以直接读取到。
7.6. docker的端口暴露