簡體   English   中英

構建docker-compose時如何使用網絡?

[英]how to use networking when building docker-compose?

我想構建我的應用程序的 docker 映像。 構建過程(測試)需要其他服務(db)。 好像我的應用無法連接到數據庫

我設法使用簡單的小圖像復制了這個問題。

dockerfile(它只是試圖到達 web 端點)

from alpine:3.10.2
run wget web:8080
cmd ["sh"]

docker-compose

version: "3.7"
services:
  web:
    image: tutum/hello-world
    ports:
      - "8080:80"
  app:
    build: .
    depends_on:
      - web

docker-compose up期間我得到了

wget: bad address 'web:8080'
ERROR: Service 'app' failed to build: The command '/bin/sh -c wget web:8080' returned a non-zero code: 1

在構建過程中如何訪問其他容器?

您需要在撰寫中使用network作為主機:

build:
     network: host

並使用localhost:port訪問它

例子:

docker-compose:

version: "3.7"
services:
  web:
    image: tutum/hello-world
    ports:
      - "8080:80"
  app:
    build:
     context: .
     network: host
    depends_on:
      - web

Dockerfile:

FROM alpine:3.10.2
RUN wget localhost:8080
CMD ["sh"]

運行web

docker-compose up -d --build web

運行app

docker-compose up -d --build app

output:

Building app
Step 1/3 : FROM alpine:3.10.2
3.10.2: Pulling from library/alpine
9d48c3bd43c5: Pull complete
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:3.10.2
 ---> 961769676411
Step 2/3 : RUN wget localhost:8080
 ---> Running in f021283ab323
Connecting to localhost:8080 (127.0.0.1:8080)
index.html           100% |********************************|   478  0:00:00 ETA
Removing intermediate container f021283ab323
 ---> aae07c08a119
Step 3/3 : CMD ["sh"]
 ---> Running in e81d9401db71
Removing intermediate container e81d9401db71
 ---> 6c217a535c7d

Successfully built 6c217a535c7d
Successfully tagged docker-test_app:latest
docker-test_web_1 is up-to-date
Creating docker-test_app_1 ... done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM