在Docker中,我使用docker network create usta_network
了一个新的networkingdocker network create usta_network
命令,并在docker-compose.yml文件的服务声明中引用此networking。
没关系,我现在可以用容器的名字来ping服务,但是我怎么能在Nginx的上游声明中引用一个容器的名字呢? 在集装箱之间进行访问是最好还是最好的做法?
docker.compose for nginx
version: '2' services: nginx-main: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro - /var/log/nginx:/var/log/nginx container_name: nginx_main networks: default: external: name: usta_network ./nginx.conf
用户nginx;
http { server { listen 80; index index.php index.html index.htm; server_name example.org www.example.org; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://rahmanusta_upstream; } } upstream rahmanusta_upstream { server rahmanusta_wp:6565; } }
docker-compose for mysql + wordpress
version: '2' services: mysql-db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: pass MYSQL_DATABASE: mysql_db MYSQL_USER: root MYSQL_PASSWORD: pass container_name: mysql_db rahmanusta-wp: depends_on: - mysql-db image: wordpress:latest ports: - "6565:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_PASSWORD: pass container_name: rahmanusta_wp volumes: db_data: networks: default: external: name: usta_network
dockernetworking检查usta_network
[ { "Name": "usta_network", "Id": "b971429c1ddcfa791bdfff0a6f9463ab9cfb9ae04ba6a3aa60ce08ca11d0a5ab", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.20.0.0/16", "Gateway": "172.20.0.1/16" } ] }, "Internal": false, "Containers": { "16cc89ca548fe90fb8ed9431d8e9633c341373344304ec3320acee1a81090709": { "Name": "rahmanusta_wp", "EndpointID": "d310cbf9617958226d36d5f9e0d804c113093097669b1d5ae8bed90d31dfca2f", "MacAddress": "02:42:ac:14:00:03", "IPv4Address": "172.20.0.3/16", "IPv6Address": "" }, "5b0f7a7d95b23b6e69b17f85ebff5d652bdbcba305755169c6b9f78199fbf346": { "Name": "mysql_db", "EndpointID": "d2c304b450ce96d309ef51319aa42336bb742d99db095e98e129e547aee820c3", "MacAddress": "02:42:ac:14:00:02", "IPv4Address": "172.20.0.2/16", "IPv6Address": "" }, "addf3c475fcfd5566b3d1c4b4a951f78978c07265211746f79711f38c5cd9649": { "Name": "nginx_main", "EndpointID": "3f4106184dbe805c509011c3dc42c673c85fcf2eb08441a5ecf24b9c5c68e2d4", "MacAddress": "02:42:ac:14:00:04", "IPv4Address": "172.20.0.4/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ]
谢谢。
在你的docker-compose.yml中,你将用相关的名字命名每个服务。 假设我在我的名为jenkins1的nginx docker容器后面创建了一个jenkins实例。
在我的nginx.conf文件的http部分中,我命名上游服务器:
upstream jenkins { server jenkins1:8080; }
在服务器块,然后我可以调用这个如下;
location / { proxy_pass https://jenkins; }
这将引导所有的流量到达jenkins1服务容器上的端口8080,达到你的nginx容器端口。
编辑基于更新的问题
在docker-compose.yml中,使用外部链接将.yml文件外部的两个容器链接起来。 您的nginx服务部分应该阅读;
services: nginx-main: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro - /var/log/nginx:/var/log/nginx external_links: - rahmanusta_wp - mysql_db container_name: nginx_main