构建docker compose时如何使用网络?

构建docker compose时如何使用网络?,docker,docker-compose,docker-networking,Docker,Docker Compose,Docker Networking,我想为我的应用程序建立一个docker映像。构建过程(测试)需要其他服务(db)。我的应用程序似乎无法连接到数据库 我用简单的小图片复制了这个问题 dockerfile(它只是尝试访问web端点) docker compose version: "3.7" services: web: image: tutum/hello-world ports: - "8080:80" app: build: . depends_on: - we

我想为我的应用程序建立一个docker映像。构建过程(测试)需要其他服务(db)。我的应用程序似乎无法连接到数据库

我用简单的小图片复制了这个问题

dockerfile(它只是尝试访问web端点)

docker compose

version: "3.7"
services:
  web:
    image: tutum/hello-world
    ports:
      - "8080:80"
  app:
    build: .
    depends_on:
      - web
在docker撰写期间,我得到了

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

在构建过程中,我如何访问其他容器?

您需要使用
网络作为主机:

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
运行
应用程序

docker-compose up -d --build app
输出:

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

您需要使用
网络
作为撰写中的主机:

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
运行
应用程序

docker-compose up -d --build app
输出:

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