skip to Main Content

I would like to see IP addresses of the running nginx containers in its content (web pages). I tried to modify the original nginx image (as seen below) but the created containers showed only one (172.17.0.2) obtained apparently from the modified image not respective containers.

Dockerfile:

FROM nginx:latest
WORKDIR / 
COPY a /a
RUN chmod +x /a
RUN ./a

‘a’ file:

echo $(hostname -i) >> /usr/share/nginx/html/index.html

Then started 3 containers but get only one address: 172.17.0.2 instead of real IPs from the 3 servers.

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
e30be0255521   ng        "/docker-entrypoint.…"   4 seconds ago    Up 3 seconds    0.0.0.0:8887->80/tcp, :::8887->80/tcp   web2
cc1893432ca8   ng        "/docker-entrypoint.…"   14 seconds ago   Up 13 seconds   0.0.0.0:8889->80/tcp, :::8889->80/tcp   web1
16ac880a4e7a   ng        "/docker-entrypoint.…"   47 seconds ago   Up 46 seconds   0.0.0.0:8888->80/tcp, :::8888->80/tcp   web

How to modify the nginx image (or enything else) to get real containers IPs in nginx content?

I tried to find the solution in here (the were several posts but no one get the solution), in the web and by modifying the Dockerfile.

2

Answers


  1. Chosen as BEST ANSWER

    I need to answer my own question, anyway it might be useful for anyone learning docker. To implement a Docker configuration that serves the host's IP address through a default Nginx location, follow these steps:

    1. Create a Dockerfile:

      FROM nginx:latest

      CMD ["sh", "-c", "hostname -i > /usr/share/nginx/html/index.html & nginx -g 'daemon off;'"]

    This Dockerfile utilizes the latest Nginx image, configuring it to execute a command that captures the host's IP address using $hostname -i and writes it to the default Nginx location (/usr/share/nginx/html/index.html). Build the Docker image:

    1. Build the Docker image: Execute the following command to build the Docker image, naming it accordingly (e.g., "selfweb"):

      docker build -t selfweb .

    2. Run the Docker container: Start the container while mapping port 80 within the container to a port on your host machine, for example, port 8080:

      docker run -d -p 8080:80 selfweb

    3. Retrieve the hostname served by Nginx: Use curl or your preferred web browser to access the hostname served by Nginx: curl http://localhost:8080

    browser


  2. There is another clever way to retrieve the IP address of our servers served by Nginx:

    We just need to make a few adjustments to the configuration and utilize the $server_addr internal variable while removing unnecessary elements:

    docker run -dt –name nnn nginx:latest # (Consider using the latest version of Nginx)

    docker exec -it nnn sh -c 'sed -i "s|root   /usr/share/nginx/html|return 200 $server_addr|" /etc/nginx/conf.d/default.conf'
    docker exec -it nnn sh -c 'sed -i "s|index  index.html index.htm|default_type text/plain|" /etc/nginx/conf.d/default.conf'
    docker exec -it nnn sh -c 'nginx -t && service nginx reload'  # Final Nginx reload
    

    After that, we can create an image from our container by running (ensure the container is running):

    docker commit nnn nginx_with_ip:v1  # Or use any name you prefer
    

    Then you can run your containers from the customized Nginx image – nginx_with_ip or any other name you’ve chosen.

    docker run -dt -p 8888:80 nginx_with_ip
    

    curl localhost:8888 – or open your favorite browser with that address and check it.
    Voila! You will see the container’s internal IP address.

    enter image description here

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