22

How can I get VSCode to use host networking during the build of the container? In devcontainer.json, I can set

"runArgs": ["--network=host"]

but that only applies to running the container. How can I get VSCode to use host networking during the build of the container?

6
  • I am confused about what this question is asking. What does it mean to use "host networking mode" before the container has been created? Unless you have a docker in docker configuration, I don't see why anything else other than "host" networking would be used during the build process. Commented Feb 9 at 13:26
  • @mallwright The container has been created from the base image, but it's adding additional layers. RUN commands are run within the container during the build. Most relevant for this question is RUN commands that fetch packages. Commented Feb 10 at 14:35
  • Right, but then what is the alternative from host networking given that you are building on the host? Is the idea to tell Docker to use a specific proxy or perhaps secure DNS while building the container? Commented Feb 11 at 15:07
  • @mallwright Bridge networking. See docs.docker.com/engine/network/… Commented Feb 12 at 17:13
  • 1
    Not at all. You can't assign a networking configuration to an image. It's a property of a container. The original motivation for this was that my corporate network had firewall rules that blocked packets from the bridge network NAT. Count yourself lucky you've never had to deal with this. It was a pain to root cause. Commented Feb 13 at 17:10

5 Answers 5

9

UPDATE 2023

The solution below no longer works. According to a GitHub issue you can use this instead:

"runArgs": [
    "--network=host",
],

Original Solution (no longer works)

You need to add the following in your devcontainer.json file:

    "build": {
        "args": {
            "network": "host"
        }
    }

This property will allow you to use the host network when building.

Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't work anymore, it adds --build-arg network=host instead of --network=host
Sorry for downvoting, but it does not work anymore as stated by @madhur4127 I try to provide the answer below.
Thanks for the update, I have updated the answer to include a working solution.
@Oren_C As far as I understood, runArgs are only for running, and the question is about building
4

As of now, proper solution does not exist, correspondent PR is open: https://github.com/microsoft/vscode-remote-release/issues/3545

A workaround mentioned in this PR is using initializeCommand in combination with image:

 "initializeCommand": "docker build --network host --tag my-image .",
  "image": "my-image"

Comments

4

@Dan Hook

You need the following in devcontainer.json

"build": {
    "options": ["--network=host"]
}

1 Comment

This is actual solution for build side network config. For running the docker image, it's "runArgs"
1

I have met a similar problem that I want to use host proxy configuration to build.

I add this in devcontainer.json:

    "build": {
        "args": {
            "HTTP_PROXY": "your_proxy_ip:port",
        }
    }

For other config, you could do the same.

Hope it will be helpful to you.

see more details here: https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg

Comments

-1

updated solution for 2024 (in case someone come across this thread)
place inside devcontainer.json the following line:

"initializeCommand": [
    "docker", "build", "--network=host",".devcontainer"
],

worked for me

1 Comment

Why use this and not runArgs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.