Search code examples
dockernetwork-programmingdocker-composeipv4

Docker static ip connection timeout from outside


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    | > frontend@0.0.0 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?


Solution

  • 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