Selecting "host" network for docker-compose build

I have Dockerfiles based on PHP docker.io images (based on Debian buster) which need to install libraries via apt. When using docker-compose to build, apt-get commands fail when accessing the Debian stores. If I run build “manually” using docker build --network=host then the apt commands work.

My question is if there is a way to specify which network docker-compose uses during build?

Versions:

  • docker-compose: 2.18.1
  • docker engine: 24.0.2 (build cb74dfc)

Here is a minimal example:

Dockerfile:

FROM php:7.1-apache
RUN apt-get update

docker-compose.yaml:

version: '3'
services:
  base:
    image: docker_base
    build:
      context: .
      dockerfile: build/Dockerfile.base
    network_mode: host  #Makes no difference if this is in there or not

When running docker-compose build, I get “stuck” on RUN apt-get update

docker-compose build --no-cache base
[+] Building 164.4s (4/5)                                                                                                                                                                             
 => [base internal] load build definition from Dockerfile.base                                                                                                                                   0.1s
 => => transferring dockerfile: 1.43kB                                                                                                                                                           0.0s
 => [base internal] load .dockerignore                                                                                                                                                           0.1s
 => => transferring context: 2B                                                                                                                                                                  0.0s
 => [base internal] load metadata for docker.io/library/php:7.1-apache                                                                                                                           0.0s
 => CACHED [base 1/2] FROM docker.io/library/php:7.1-apache                                                                                                                                      0.0s
[+] Building 164.6s (4/5)                                                                                                                                                                             
 => [base internal] load build definition from Dockerfile.base                                                                                                                                   0.1s
 => => transferring dockerfile: 1.43kB                                                                                                                                                           0.0s
 => [base internal] load .dockerignore                                                                                                                                                           0.1s
 => => transferring context: 2B                                                                                                                                                                  0.0s
 => [base internal] load metadata for docker.io/library/php:7.1-apache                                                                                                                           0.0s
 => CACHED [base 1/2] FROM docker.io/library/php:7.1-apache                                                                                                                                      0.0s
[+] Building 164.7s (4/5)                                                                                                                                                                             
 => [base internal] load build definition from Dockerfile.base                                                                                                                                   0.1s
 => => transferring dockerfile: 1.43kB                                                                                                                                                           0.0s
 => [base internal] load .dockerignore                                                                                                                                                           0.1s
 => => transferring context: 2B                                                                                                                                                                  0.0s
 => [base internal] load metadata for docker.io/library/php:7.1-apache                                                                                                                           0.0s
 => CACHED [base 1/2] FROM docker.io/library/php:7.1-apache                                                                                                                                      0.0s
[+] Building 390.1s (4/5)                                                                                                                                                              
 => [base internal] load build definition from Dockerfile.base                                                                                                                    0.1s
 => => transferring dockerfile: 1.43kB                                                                                                                                            0.0s 
 => [base internal] load .dockerignore                                                                                                                                            0.1s
 => => transferring context: 2B                                                                                                                                                   0.0s 
 => [base internal] load metadata for docker.io/library/php:7.1-apache                                                                                                            0.0s
 => CACHED [base 1/2] FROM docker.io/library/php:7.1-apache                                                                                                                       0.0s 
 => [base 2/2] RUN apt-get update                                                                                                                                               390.0s
 => => # Get:1 http://security.debian.org/debian-security buster/updates InRelease [34.8 kB]                                                                                           
 => => # Get:2 http://deb.debian.org/debian buster InRelease [122 kB]                                                                                                                 
 => => # Get:1 http://security.debian.org/debian-security buster/updates InRelease [34.8 kB]                                                                                           
 => => # Get:2 http://deb.debian.org/debian buster InRelease [122 kB]                                                                                                                 
 => => # Get:1 http://security.debian.org/debian-security buster/updates InRelease [34.8 kB]                                                                                           
 => => # Get:2 http://deb.debian.org/debian buster InRelease [122 kB]                                                                                                                 

If I manually build, it runs fine:

:$ docker build --no-cache --network host --file ./build/Dockerfile.base .                                             
[+] Building 3.4s (4/5)                                                                                                                                                                
 => [internal] load .dockerignore                                                                                                                                                 0.0s 
 => => transferring context: 2B                                                                                                                                                   0.0s 
 => [internal] load build definition from Dockerfile.base                                                                                                                         0.0s 
 => => transferring dockerfile: 1.43kB                                                                                                                                            0.0s
 => [internal] load metadata for docker.io/library/php:7.1-apache                                                                                                                 0.0s
 => CACHED [1/2] FROM docker.io/library/php:7.1-apache                                                                                                                            0.0s
 => [2/2] RUN apt-get update                                                                                                                                                      3.3s
 => => # Get:1 http://security.debian.org/debian-security buster/updates InRelease [34.8 kB]                                                                                          
 => => # Get:2 http://deb.debian.org/debian buster InRelease [122 kB]                                                                                                                 
 => => # Get:3 http://security.debian.org/debian-security buster/updates/main amd64 Packages [506 kB]                                                                                 
 => => # Get:4 http://deb.debian.org/debian buster-updates InRelease [56.6 kB]                                                                                                        
 => => # Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7909 kB]                                                                                                      
 => => # Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [8788 B]                                                                                               

edit: added version info