Docker安装并部署nginx

1.搜索nginx镜像

1
2
3
4
5
~$ sudo docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13374 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1828 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 778 [OK]

2.下载nginx镜像

查看本地所有镜像

1
2
3
4
~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 74435f89ab78 7 days ago 73.9MB
ubuntu 18.04 8e4ce0a6ce69 7 days ago 64.2MB

从远程仓库中下载 nginx 镜像

1
2
3
4
5
6
7
8
9
10
~$ sudo docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8d69e59170f7: Pull complete
3f9f1ec1d262: Pull complete
d1f5ff4f210d: Pull complete
1e22bfa8652e: Pull complete
Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

再次查看本地所有镜像,验证是否下载成功~$ sudo docker images

1
2
3
4
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu latest 74435f89ab78 7 days ago 73.9MB
ubuntu 18.04 8e4ce0a6ce69 7 days ago 64.2MB
nginx latest 2622e6cca7eb 2 weeks ago 132MB

3. 启动nginx

1
2
3
4
5
6
# 以后台运行的方式启动nginx : docker run -d <image name>
# -d : 后台运行,
# --name : 为容器起别名
# -p 8888:88 : 暴露端口,-p 宿主机端口:容器内部端口
~$ sudo docker run -d --name MyNginx -p 8888:80 nginx
8438eaf75dbdcae96602fd91035cf550e8a3a4634bfc17a92e63f2325fc15064

查看当前正在运行的容器,验证 nginx 服务是否已启动

1
2
3
~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8438eaf75dbd nginx "/docker-entrypoint.…" 9 seconds ago Up 7 seconds 0.0.0.0:8888->80/tcp MyNginx

使用 curl ip:port 进行本地测试,从返回的结果发现 nginx 部署成功

也可以通过外网访问连接 http://ip:8888 来测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
~$ sudo curl localhost:8888
<!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>

通过外网访问连接 http://ip:8888 来测试 nginx 是否部署成功 服务器记得放行端口

4. 修改nginx配置

查看当前正在运行的容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
~$ sudo docker ps                                                                               
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8438eaf75dbd nginx "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 0.0.0.0:8888 80/tcp MyNginx

# 根据容器 NAME 进入当前正在运行的 nginx 容器( docker exec -it : 特征为进入容器后开启一个新的终端 )
```shell
~$ sudo docker exec -it MyNginx /bin/bash

# 查找 nginx 的配置文件目录
```shell
whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx

# 进入 nginx 中用于存储 html 的文件目录
```shell
cd /usr/share/nginx/
cd html/
ls
50x.html index.html

# 查看 index.html 文件中的内容
```shell
cat index.html
<!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>

重写 index.html 中内容

1
echo "<h1 align="center">hey guy welcome back,coffee time</h1>" > index.html          

再次查看 index.html 中的内容

1
2
cat index.html
<h1 align=center>hey guy welcome back,coffee time</h1>

使用 curl ip:port 进行本地测试,从返回的结果发现修改 index.html 后 nginx 依旧部署成功( 配置文件可动态修改 )

也可以通过外网访问连接 http://ip:8888 来测试,如下图所示

1
2
curl http://123.352.21.23:8888
<h1 align=center>hey guy welcome back,coffee time</h1>

通过外网访问连接 http://ip:8888 来测试修改 index.html 后 nginx 是否部署成功( 配置文件可动态修改 )

5. 停止nginx运行

查看当前正在运行的容器

1
2
3
~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8438eaf75dbd nginx "/docker-entrypoint.…" 38 minutes ago Up 38 minutes 0.0.0.0:8888->80/tcp MyNginx

根据id停止指定容器的运行

1
2
~$ sudo docker stop 8438eaf75dbd
8438eaf75dbd

再次查看当前正在运行的容器,验证 nginx 是否被关闭

1
2
~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

nginx 被关闭后则不能访问 http://ip:8888

思考

会有比docker更好的容器方案吗?