这是indexloc提供的服务,不要输入任何密码
Skip to content

Make a way for containers to have dependencies during the build #6093

@Ajedi32

Description

@Ajedi32

Description of the issue

Docker Compose 1.17.0 introduced support for specifying a network for containers to attach themselves to during the build process. Unfortunately, since docker-compose doesn't start a container's dependencies until after the build, you can't use this option to allow containers to communicate with each other.

I'd like to solve that problem, whether by changing the default behavior to allow communication with dependencies during the build, or by introducing a new option in the compose file that specifies which dependencies need to be started during the build.

Steps to reproduce the issue

  1. Create a project with the following files:

docker-compose.yml:

version: '3.5'

services:
  some_dependency:
    image: httpd
  myapp:
    build:
      context: .
      network: myapp_default
    depends_on:
      - some_dependency

networks:
  default:
    name: myapp_default

Dockerfile:

FROM alpine

RUN apk add --no-cache curl

RUN curl http://some_dependency/ > file
  1. Run docker-compose build --no-cache

Observed result

some_dependency uses an image, skipping
Building myapp
Step 1/3 : FROM alpine
 ---> 11cd0b38bc3c
Step 2/3 : RUN apk add --no-cache curl
 ---> Running in b7efd52675f0
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
(1/5) Installing ca-certificates (20171114-r3)
(2/5) Installing nghttp2-libs (1.32.0-r0)
(3/5) Installing libssh2 (1.8.0-r3)
(4/5) Installing libcurl (7.61.0-r0)
(5/5) Installing curl (7.61.0-r0)
Executing busybox-1.28.4-r0.trigger
Executing ca-certificates-20171114-r3.trigger
OK: 6 MiB in 18 packages
Removing intermediate container b7efd52675f0
 ---> e639a58808ca
Step 3/3 : RUN curl http://some_dependency/ > file
 ---> Running in 7ba3dfaae446
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:04 --:--:--     0curl: (6) Could not resolve host: some_dependency
Service 'myapp' failed to build: The command '/bin/sh -c curl http://some_dependency/ > file' returned a non-zero code: **6**

However, if you manually start some_dependency with docker-compose up --detach some_dependency before running the build, everything works fine:

some_dependency uses an image, skipping
Building myapp
Step 1/3 : FROM alpine
 ---> 11cd0b38bc3c
Step 2/3 : RUN apk add --no-cache curl
 ---> Running in 1f4dead4514b
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
(1/5) Installing ca-certificates (20171114-r3)
(2/5) Installing nghttp2-libs (1.32.0-r0)
(3/5) Installing libssh2 (1.8.0-r3)
(4/5) Installing libcurl (7.61.0-r0)
(5/5) Installing curl (7.61.0-r0)
Executing busybox-1.28.4-r0.trigger
Executing ca-certificates-20171114-r3.trigger
OK: 6 MiB in 18 packages
Removing intermediate container 1f4dead4514b
 ---> c14e434d64e1
Step 3/3 : RUN curl http://some_dependency/ > file
 ---> Running in 17c25eaed969
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    45  100    45    0     0   6428      0 --:--:-- --:--:-- --:--:--  6428
Removing intermediate container 17c25eaed969
 ---> 07c9c20086cd

Successfully built 07c9c20086cd
Successfully tagged dockercomposebuilddependencies_myapp:latest

Expected result

There should be some way to ensure some_dependency is started during the build process for myapp.

Either that should just be the default behavior of depends_on, or perhaps there could be a depends_on option within the build configuration that ensures network access is properly set up during the build.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions