Centos7系统Docker环境下Nginx反向代理
目录
1.拉取nginx镜像
1.拉取nginx镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
27833a3ba0a5: Pull complete
ea005e36e544: Pull complete
d172c7f0578d: Pull complete
Digest: sha256:e71b1bf4281f25533cf15e6e5f9be4dac74d2328152edf7ecde23abc54e16c1c
Status: Downloaded newer image for nginx:latest
2.可以直接运行测试一下
[root@localhost ~]# docker run -d -p 8080:80 nginx
[root@localhost ~]# curl -I http://localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Thu, 25 Apr 2019 14:39:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
[root@localhost ~]#
2.Docker Compose
Docker Compose是一个用来定义和运行复杂应用的Docker工具。使用Compose,你可以在一个文件中定义一个多容器应用,然后使用一条命令来启动你的应用,完成一切准备工作。
1.安装Docker Compose
官网的安装教程:安装Docker Compose
可以从官网获取最新的版本
[root@localhost ~]# sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 752 0 --:--:-- --:--:-- --:--:-- 752
100 15.4M 100 15.4M 0 0 1731k 0 0:00:09 0:00:09 --:--:-- 2687k
[root@localhost ~]#
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# docker-compose --version
docker-compose version 1.24.0, build 0aa59064
2.创建docker-compose.yml
Dcoker Compse的执行文件是docker-compose.yml,所以我们在测试项目下创建一个docker-compose.yml文件。
[root@localhost ~]# cd Documents/DockerWeb
[root@localhost DockerWeb]# touch docker-compose.yml
编辑该文件,写入以下内容,保存退出。
version: '2'
services:
docker-web:
container_name: docker-web-compose
build: .
reverse-proxy:
container_name: reverse-proxy
image: nginx
ports:
- "9090:8080"
volumes:
- ./proxy.conf:/etc/nginx/conf.d/default.conf
#简单介绍下上面的配置文件,其中定义了两个服务:
#一个是docker-web,即以我们当前项目目录来构建镜像并启动一个叫docker-web-compose的容器。
#一个是reverse-proxy,用来使用nginx镜像进行反向代理,其中又通过指定volumes来使用挂载的方式进行配置,配置文件是当前路径下的proxy.conf。
创建proxy.conf配置文件
[root@localhost DockerWeb]# touch proxy.conf
编辑该文件,写入以下内容,保存退出。
server {
listen 8080;
location / {
proxy_pass http://docker-web:5000;
}
}
#其中要注意反向代理的配置:proxy_pass http://docker-web:5000;,其中ip部分直接指定的是docker-compose.yml中定义的第一个服务的名称docker-web。
创建完后我们看一下该目录的内容
[root@localhost DockerWeb]# ll
total 356
-rw-r--r--. 1 root root 146 Apr 24 14:04 appsettings.Development.json
-rw-r--r--. 1 root root 105 Apr 24 14:04 appsettings.json
-rw-r--r--. 1 root root 287 Apr 25 14:55 docker-compose.yml
-rw-r--r--. 1 root root 137 Apr 24 14:51 Dockerfile
-rw-r--r--. 1 root root 228338 Apr 24 14:41 DockerWeb.deps.json
-rw-r--r--. 1 root root 9216 Apr 24 14:35 DockerWeb.dll
-rw-r--r--. 1 root root 1924 Apr 24 14:35 DockerWeb.pdb
-rw-r--r--. 1 root root 224 Apr 24 14:41 DockerWeb.runtimeconfig.json
-rw-r--r--. 1 root root 78848 Apr 24 14:35 DockerWeb.Views.dll
-rw-r--r--. 1 root root 5496 Apr 24 14:35 DockerWeb.Views.pdb
-rw-r--r--. 1 root root 92 Apr 25 14:54 proxy.conf
-rw-r--r--. 1 root root 526 Apr 24 14:41 web.config
drwxr-xr-x. 6 root root 71 Apr 24 22:48 wwwroot
3.运行Compose
[root@localhost DockerWeb]# docker-compose up -d
Creating network "dockerweb_default" with the default driver
Building docker-web
Step 1/6 : FROM microsoft/dotnet:latest
---> e268893be733
Step 2/6 : WORKDIR /app
---> Using cache
---> 636e51b35e60
Step 3/6 : COPY . /app
---> 26c5b7a83367
Step 4/6 : EXPOSE 5000
---> Running in 54796fcb8c07
Removing intermediate container 54796fcb8c07
---> 6663d8b3df8f
Step 5/6 : ENV ASPNETCORE_URLS http://*:5000
---> Running in a6d66985a77e
Removing intermediate container a6d66985a77e
---> 586c4cc1cf75
Step 6/6 : ENTRYPOINT ["dotnet","DockerWeb.dll"]
---> Running in 812c66d16d72
Removing intermediate container 812c66d16d72
---> 8da5e977ec16
Successfully built 8da5e977ec16
Successfully tagged dockerweb_docker-web:latest
WARNING: Image for service docker-web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker-web-compose ... done
Creating reverse-proxy ... done
[root@localhost DockerWeb]#
运行完后我们可以看下镜像跟容器是否存在相应的内容,最后可以再用curl测试下
[root@localhost DockerWeb]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerweb_docker-web latest 8da5e977ec16 About a minute ago 1.74GB
nginx latest 27a188018e18 8 days ago 109MB
microsoft/dotnet latest e268893be733 2 weeks ago 1.74GB
[root@localhost DockerWeb]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72584de2c725 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp, 0.0.0.0:9090->8080/tcp reverse-proxy
1b3adbbe8ec0 dockerweb_docker-web "dotnet DockerWeb.dll" About a minute ago Up About a minute 5000/tcp docker-web-compose
dd45358d8c37 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp friendly_bhaskara
[root@localhost DockerWeb]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------
docker-web-compose dotnet DockerWeb.dll Up 5000/tcp
reverse-proxy nginx -g daemon off; Up 80/tcp, 0.0.0.0:9090->8080/tcp
[root@localhost DockerWeb]# curl -I http://localhost:9090
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Thu, 25 Apr 2019 15:01:41 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
[root@localhost DockerWeb]#
参考博客: