skip to Main Content

I am trying to assign my APP with a static IP. I think I have assigned the IP, but I cannot reach the IP from outside either curl or the browser.

docker-compose.yaml

version: '3.8'
services:
  client:
    container_name: client
    build: ./frontend
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./frontend:/app/
      - /app/node_modules
    networks:
      app_net:
        ipv4_address: 172.16.238.10
    hostname: client
    command: npm run dev
networks:
  app_net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: "172.16.238.0/24"

Terminal message when I run it:

client    | > [email protected] dev
client    | > vite
client    | 
client    | 
client    |   VITE v3.2.2  ready in 710 ms
client    | 
client    |   ➜  Local:   http://localhost:3000/
client    |   ➜  Network: http://172.16.238.10:3000/

I can reach http://localhost:3000/ on the browser just fine. But whenever I try to reach http://172.16.238.10:3000/, I get a timeout. Something this on the browser:

This site can’t be reached
172.16.238.10 took too long to respond.
ERR_CONNECTION_TIMED_OUT

I can curl to the IP I assigned inside the docker container, but I cannot do it from outside. Is it possible to expose the IP to the outside?

3

Answers


  1. You probably want to look at using a macvlan network instead of bridged.

    Login or Signup to reply.
  2. If you manage to assign another IP address (172.16.238.10) to the host, the container can continue having a dynamic IP address. You can map the port 3000 only to the IP address 172.16.238.10.

    Then you can connect to 172.16.238.10:3000 as so:

    version: '3.8'
    services:
      client:
        container_name: client
        build: ./frontend
        ports:
          - 172.16.238.10:3000:3000
        working_dir: /app
        volumes:
          - ./frontend:/app/
          - /app/node_modules
        networks:
          - app_net
        hostname: client
        command: npm run dev
    networks:
      app_net:
        driver: bridge
    
    Login or Signup to reply.
  3. As a general rule, you can’t access the container-private IP addresses at all. It happens to be possible on native Linux, not using Docker Desktop or another VM-based setup, and only if you’re on the same machine as the container, but given all of these constraints it’s usually better to not try it.

    The standard way to access a container from outside is to declare ports: for it, and to use the host’s DNS name or IP address and the published port number. Don’t declare any IP-related setup in your Compose file at all.

    Assuming your Dockerfile is correctly constructed, you should be able to reduce that Compose file to just

    version: '3.8'
    services:
      client:
        build: ./frontend
        ports:
          - 3000:3000
    

    and calling http://host.example.com:3000/ using your host’s DNS name, or http://localhost:3000 if you’re on the same host, will forward to port 3000 in the container. None of the other Compose options are necessary.

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