使用docker安装nginx提供web服务
- 使用docker安装nginx实现web服务
- 一、拉取镜像
- 二、运行镜像启动容器
- 三、文件映射
- 四、重启容器
- 五、反向代理配置
使用docker安装nginx实现web服务
用例:通过nginx镜像实现一个web服务,在首页输出自己的名字。
一、拉取镜像
docker pull命令用于拉取应用镜像
[root@VM-4-6-centos ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f1f26f570256: Pull complete
84181e80d10e: Pull complete
1ff0f94a8007: Pull complete
d776269cad10: Pull complete
e9427fcfa864: Pull complete
d4ceccbfc269: Pull complete
Digest: sha256:f4e3b6489888647ce1834b601c6c06b9f8c03dee6e097e13ed3e28c01ea3ac8c
Status: Downloaded newer image for nginx:latest
使用docker images命令可以查看当前操作系统中下载了哪些镜像文件。
[root@VM-4-6-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ac232364af84 4 days ago 142MB
二、运行镜像启动容器
启动容器,容器名称为nginx-ying
[root@VM-4-6-centos ~]# docker run -d --name nginx-ying -p 80:80 nginx
3508bb399fd5419220bb7289f4cd9eebdc9f6ad1c48f818ddea7bdf639cd533a
-d表示容器在后台运行 --name为容器起一个名字 -p端口映射,格式为宿主机端口:容器端口,上文中含义是将容器中的端口80映射到宿主机的端口80,对外提供访问服务。 最后一个字段为镜像名称
查看正在运行中的容器。
[root@VM-4-6-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3508bb399fd5 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-ying
三、文件映射
在宿主机中新建文件目录。
[root@VM-4-6-centos ~]# mkdir -p /root/nginx/logs /root/nginx/html /root/nginx/conf;
将nginx配置文件copy到宿主机中
[root@VM-4-6-centos nginx]# docker cp nginx-ying:etc/nginx/nginx.conf /root/nginx/conf;
创建一个index.html文件放入宿主机的/root/nginx/html目录,因为存在映射关系,实际上也是放入了容器的/usr/share/nginx/html目录。
[root@VM-4-6-centos html]# cat index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>使用docker搭建nginx web服务</title>
</head>
<body>
<h1>任务1:通过nginx精选实现一个web服务,在首页输出自己的名字</h1>
<p>My name is hhhhha</p>
</body>
</html>
四、重启容器
启动一个新的容器,容器名字还是不变。
[root@VM-4-6-centos html]# docker run -d -p 80:80 \
> --name nginx-prod \
> -v /root/nginx/html:/usr/share/nginx/html \
> -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
> -v /root/nginx/logs:/var/log/nginx nginx
797f02efb554d9f4d2d03bf8844fb89bdb803f8c6f7cdb5662257efa5a07304b
-v参数表达了宿主机文件与容器中文件的映射关系,格式为-v 宿主机目录:容器文件系统目录。
在启动新容器之前,将nginx-ying旧的容器删掉。如果不删除旧的容器,新容器的端口与旧容器端口会发生冲突。使用如下命令删除容器:
[root@VM-4-6-centos html]# docker stop nginx-ying;
nginx-ying
[root@VM-4-6-centos html]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-4-6-centos html]# docker rm nginx-ying
nginx-ying
容器启动后,可浏览器访问宿主机80端口,响应如下则为正常。
五、反向代理配置
在nginx.conf 配置文件添加如下参数。
server{
listen 80;
charset utf-8;
server_name 124.221.152.49;//宿主机地址
location / {
proxy_pass https://liumou.site;//访问的服务器地址
proxy_redirect default;
}
}
全文内容如下:
[root@VM-4-6-centos conf]# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server{
listen 80;
charset utf-8;
server_name 124.221.152.49;
location / {
proxy_pass https://liumou.site;
proxy_redirect default;
}
}
}
重启容器
[root@VM-4-6-centos conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
797f02efb554 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp nginx-prod
[root@VM-4-6-centos conf]# docker restart 797f02efb554
797f02efb554
最终效果: