skip to Main Content

I want to use the host ip address in container, how should I achieve this in dockerfile way?

FROM my_image
ENV http_proxy <the_host_ip_address:port>
ENV https_proxy <the_host_ip_address:port>

2

Answers


  1. You can pass any argument variables while building a docker-image and use them in the Dockerfile. For example:

    FROM my_image
    ARG host
    ARG port
    ENV http_proxy $host:$port
    ENV https_proxy $host:$port
    

    And the command for building a docker-image will look like this:

    docker build --build-arg host=<the_host_ip_address> --build-arg port=<port> .
    

    There is a useful article on the baeldung.com

    UPD

    I’ve already found a more detailed answer. Look at this.

    Login or Signup to reply.
  2. Here is one way to set the host IP address in the Dockerfile:

    FROM my_image
    
    ARG HOST_IP
    ENV http_proxy=$HOST_IP:port
    ENV https_proxy=$HOST_IP:port
    
    # Rest of the Dockerfile goes here
    

    And then when building the image, you would pass the –build-arg flag to set the HOST_IP arg:

    docker build --build-arg HOST_IP=$(hostname -i) -t myimage .
    

    This will set the HOST_IP arg to the IP address of the host machine during build time.

    The advantage of this method is that the host IP doesn’t need to be hardcoded in the Dockerfile, it is set at build time.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search