docker 容器从一个容器卷曲到另一个容器

发布于 2025-01-09 12:59:38 字数 2924 浏览 1 评论 0原文

我有四个容器 A、B、C 和 D。容器 C 是一个 nginx 容器,配置为服务容器 A、B 和 D。我在容器 D 中,想要卷曲容器 B,但无法通过。

在我的 docker-compose 文件中,我在容器 B 上公开端口 1000:1000。在容器 C 中,ngnix default.conf 文件配置为接受容器 B 端口 70 上的连接。我可以从 http://localhost:70 访问 container_B 。但我尝试从容器 D 中获取curl -v container_B:70 并收到错误
卷曲:(7)无法连接到容器_B端口70:

当我尝试卷曲-v容器_B:9000时 连接被拒绝 我得到:

*   Trying 172.27.0.4:9000...
* Connected to container_B (172.27.0.4) port 9000 (#0)
> GET / HTTP/1.1
> Host: auth-api:9000
> User-Agent: curl/7.74.0
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

nginx 配置

server {
    listen 70;
    index index.php index.html;
    server_name localhost;
    root /var/www/html/container_B/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass  container_B:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

撰写文件

container_A:
    image: test/container_A:1.0
    container_name:  container_A
    restart: unless-stopped
    depends_on:
      - main_db
      
    tty: true
    environment:
      SERVICE_NAME:  container_A
    working_dir: /var/www/html/
    ports:
      - 9000:9000
    volumes:
      - ./container_A:/var/www/html

    networks:
      - back-tier
      - front-tier
 
  container_B:
    image: test/container_B:1.0
    container_name:  container_B
    restart: unless-stopped
    depends_on:
      - auth_db
      - backend_api
    tty: true
    environment:
      SERVICE_NAME:container_B
    ports:
      - 1000:1000
    volumes:
      - ./container_B:/var/www/html/container_B
    networks:
      - back-tier
      - front-tier
      
      
  
  container_D:
    image: test/container_d:1.0
    container_name:  container_D
    restart: unless-stopped
    depends_on:
      - backend_api
      - container_B
    tty: true
    environment:
      SERVICE_NAME:  container_D
    ports:
      - 6000:6000
    volumes:
      - ./ container_D:/var/www/html/container_D
    
    networks:
      - front-tier
      
      
      
    container_c:
    image: test/nginx:1.0
    depends_on:
      - backend_api
      - container_B
      - main_db
    container_name: container_c
    restart: always
    tty: true
    ports:
      - "70:70"
      - "60:60"
      - "50:50"
    networks:
      - front-tier
      - back-tier

I have four containers A,B,C and D. container C is an nginx container which is configure to serve container A and B and D. Iam in container D and want to curl contaner B but i can't get through.

on my docker-compose file, I expose port 1000:1000 on container B. In container C the ngnix default.conf file is configure to accept connection on port 70 for container B. I can access container_B from http://localhost:70. But i try curl -v container_B:70 from container D and i get the error
curl: (7) Failed to connect to container_B port 70: Connection refused

when i try curl -v container_B:9000
I get:

*   Trying 172.27.0.4:9000...
* Connected to container_B (172.27.0.4) port 9000 (#0)
> GET / HTTP/1.1
> Host: auth-api:9000
> User-Agent: curl/7.74.0
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

nginx config

server {
    listen 70;
    index index.php index.html;
    server_name localhost;
    root /var/www/html/container_B/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass  container_B:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

compose file

container_A:
    image: test/container_A:1.0
    container_name:  container_A
    restart: unless-stopped
    depends_on:
      - main_db
      
    tty: true
    environment:
      SERVICE_NAME:  container_A
    working_dir: /var/www/html/
    ports:
      - 9000:9000
    volumes:
      - ./container_A:/var/www/html

    networks:
      - back-tier
      - front-tier
 
  container_B:
    image: test/container_B:1.0
    container_name:  container_B
    restart: unless-stopped
    depends_on:
      - auth_db
      - backend_api
    tty: true
    environment:
      SERVICE_NAME:container_B
    ports:
      - 1000:1000
    volumes:
      - ./container_B:/var/www/html/container_B
    networks:
      - back-tier
      - front-tier
      
      
  
  container_D:
    image: test/container_d:1.0
    container_name:  container_D
    restart: unless-stopped
    depends_on:
      - backend_api
      - container_B
    tty: true
    environment:
      SERVICE_NAME:  container_D
    ports:
      - 6000:6000
    volumes:
      - ./ container_D:/var/www/html/container_D
    
    networks:
      - front-tier
      
      
      
    container_c:
    image: test/nginx:1.0
    depends_on:
      - backend_api
      - container_B
      - main_db
    container_name: container_c
    restart: always
    tty: true
    ports:
      - "70:70"
      - "60:60"
      - "50:50"
    networks:
      - front-tier
      - back-tier

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2025-01-16 12:59:38

我花了一段时间才弄清楚我做错了什么。由于我想要在容器 B 中卷曲的服务正在容器 C 的实例上运行,容器 C 是一个为三个容器提供服务的 nginx 容器。我本来应该curl -v container_C:70。端口 70 是在 default.conf nginx 文件中配置的端口,该文件正在侦听容器 B。

我做错的是尝试直接卷曲到容器 B,这就是为什么我遇到错误curl: (7) 无法连接到container_B 端口 70:连接被拒绝。因为我尝试连接的实例没有在容器 B 上运行。

It took me a while to figure out what i was doing wrong. Since the service that i want to curl in container B is running on an instance of container C which is an nginx container serving the three containers. I was suppose to curl -v container_C:70. Port 70 is the port that is configure in default.conf nginx file that is listening on container B.

What i did wrong was trying to curl to container B directly that is why i was encounter the error curl: (7) Failed to connect to container_B port 70: Connection refused. Because the instance that i was trying to connect to wasn't running on container B.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文