Tips Docker Compose Proxy: Cách tích hợp Proxy trong Docker Compose

TricksMMO

Administrator
Staff member
### Cách tích hợp proxy trong Docker Compose

Docker Compose là một công cụ để xác định và chạy các ứng dụng Docker đa phụ thuộc.Nó có thể được sử dụng để tạo và quản lý các ứng dụng phức tạp với nhiều dịch vụ, mỗi dịch vụ chạy trong thùng chứa riêng của nó.

Proxy là một máy chủ nằm giữa máy khách và máy chủ và các yêu cầu chuyển tiếp từ máy khách đến máy chủ.Proxy có thể được sử dụng để cải thiện hiệu suất, bảo mật và/hoặc để cung cấp quyền truy cập vào các tài nguyên không thể truy cập được.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tích hợp proxy vào ứng dụng Docker Compose.Chúng tôi sẽ sử dụng máy chủ proxy nginx, đây là một lựa chọn phổ biến cho các ứng dụng docker ủy quyền.

** Điều kiện tiên quyết **

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Công cụ Docker 18,09 trở lên
* Docker soạn 1,25 hoặc muộn hơn
* Máy tính Linux hoặc MacOS

** Bước 1: Tạo tệp Docker Compose **

Bước đầu tiên là tạo một tệp Docker Compose xác định ứng dụng và dịch vụ của nó.

`` `
Phiên bản: '3,8'
dịch vụ:
Web:
Hình ảnh: Nginx: mới nhất
Cổng:
- 80:80
Ủy quyền:
Hình ảnh: Nginx: mới nhất
Khối lượng:
- ./conf.d:/etc/nginx/conf.d
Cổng:
- 8080: 80
`` `

Docker này soạn thảo tệp xác định hai dịch vụ:

* `Web`: Dịch vụ này là một máy chủ web đơn giản lắng nghe trên cổng 80.
* `Proxy`: Dịch vụ này là máy chủ proxy lắng nghe trên cổng 8080.

Dịch vụ `proxy` gắn thư mục` conf.d` từ máy chủ vào container.Thư mục này chứa các tệp cấu hình cho máy chủ proxy.

** Bước 2: Tạo các tệp cấu hình **

Bước tiếp theo là tạo các tệp cấu hình cho máy chủ proxy.

`` `
phụ trợ ngược dòng {
Web máy chủ: 80;
}

máy chủ {
Nghe 8080;
server_name localhost;

vị trí / {
Proxy_Pass http: // phụ trợ;
}
}
`` `

Tệp cấu hình này xác định một nhóm ngược dòng có tên là `phụ trợ`.Nhóm `phụ trợ` chứa một máy chủ, đó là dịch vụ` web`.

Khối `server` xác định chính máy chủ proxy.Chỉ thị `Listen` chỉ định cổng mà máy chủ proxy lắng nghe.Chỉ thị `server_name` chỉ định tên máy chủ mà máy chủ proxy trả lời.

Khối `vị trí` xác định vị trí mà máy chủ proxy xử lý.Chỉ thị `proxy_pass` chỉ định nhóm ngược dòng mà máy chủ proxy chuyển tiếp yêu cầu.

** Bước 3: Xây dựng và chạy ứng dụng **

Khi bạn đã tạo tệp Docker Compose và các tệp cấu hình, bạn có thể xây dựng và chạy ứng dụng.

`` `
Docker -Compose Up -D
`` `

Lệnh này sẽ xây dựng các hình ảnh cho ứng dụng và bắt đầu các thùng chứa.

Bây giờ bạn có thể truy cập ứng dụng bằng cách truy cập http: // localhost: 8080 trong trình duyệt của bạn.

** Bước 4: Kiểm tra proxy **

Bạn có thể kiểm tra proxy bằng cách thực hiện yêu cầu cho ứng dụng.

`` `
Curl http: // localhost: 8080
`` `

Lệnh này sẽ trả về phản hồi tương tự như khi bạn đã thực hiện yêu cầu trực tiếp dịch vụ 'web`.

**Phần kết luận**

Trong hướng dẫn này, bạn đã học cách tích hợp proxy vào ứng dụng Docker Compose.Bạn đã sử dụng máy chủ proxy nginx cho các yêu cầu proxy từ cổng 8080 đến cổng 80.

Bạn có thể sử dụng kỹ thuật này để ủy quyền bất kỳ loại ứng dụng nào chạy trong thùng chứa Docker.

** hashtags **

* #Docker
* #Docker-Compose
* #Ủy quyền
* #Nginx
* #Docker-Networking
=======================================
### How to Integrate a Proxy in Docker Compose

Docker compose is a tool for defining and running multi-container Docker applications. It can be used to create and manage complex applications with multiple services, each running in its own container.

A proxy is a server that sits between a client and a server and forwards requests from the client to the server. Proxies can be used to improve performance, security, and/or to provide access to resources that are not otherwise accessible.

In this tutorial, we will show you how to integrate a proxy into a Docker compose application. We will use the Nginx proxy server, which is a popular choice for proxying Docker applications.

**Prerequisites**

To follow this tutorial, you will need the following:

* Docker Engine 18.09 or later
* Docker Compose 1.25 or later
* A Linux or macOS computer

**Step 1: Create a Docker compose file**

The first step is to create a Docker compose file that defines the application and its services.

```
version: '3.8'
services:
web:
image: nginx:latest
ports:
- 80:80
proxy:
image: nginx:latest
volumes:
- ./conf.d:/etc/nginx/conf.d
ports:
- 8080:80
```

This Docker compose file defines two services:

* `web`: This service is a simple web server that listens on port 80.
* `proxy`: This service is a proxy server that listens on port 8080.

The `proxy` service mounts the `conf.d` directory from the host machine into the container. This directory contains the configuration files for the proxy server.

**Step 2: Create the configuration files**

The next step is to create the configuration files for the proxy server.

```
upstream backend {
server web:80;
}

server {
listen 8080;
server_name localhost;

location / {
proxy_pass http://backend;
}
}
```

This configuration file defines an upstream group named `backend`. The `backend` group contains a single server, which is the `web` service.

The `server` block defines the proxy server itself. The `listen` directive specifies the port that the proxy server listens on. The `server_name` directive specifies the hostname that the proxy server responds to.

The `location` block defines the location that the proxy server handles. The `proxy_pass` directive specifies the upstream group that the proxy server forwards requests to.

**Step 3: Build and run the application**

Once you have created the Docker compose file and the configuration files, you can build and run the application.

```
docker-compose up -d
```

This command will build the images for the application and start the containers.

You can now access the application by visiting http://localhost:8080 in your browser.

**Step 4: Test the proxy**

You can test the proxy by making a request to the application.

```
curl http://localhost:8080
```

This command should return the same response as if you had made a request to the `web` service directly.

**Conclusion**

In this tutorial, you learned how to integrate a proxy into a Docker compose application. You used the Nginx proxy server to proxy requests from port 8080 to port 80.

You can use this technique to proxy any type of application that runs in a Docker container.

**Hashtags**

* #Docker
* #Docker-compose
* #Proxy
* #Nginx
* #Docker-networking
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top