Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-Compose container ip address with container name

My docker-compose have a two service, and docker-compose.yml define enviroment variable ip address with container name,

 version: '2'

services:
  api:
    build: ./api/
    command: python3 manage.py runserver
    volumes:
      - ./api:/code
    ports:
      - "8000:80"
    networks:
      - dock_net
    container_name: con_api

  web:
    build: ./web/
    command: python3 manage.py runserver
    volumes:
      - ./web:/code
    ports:
      - "8001:80"
    networks:
      - dock_net
    container_name: con_web
    environment:
        Ip:con_ip

networks:
  dock_net:
      driver: bridge

But variable see "con_ip" not 127.0.0.3

like image 744
onuryartasi Avatar asked Jul 31 '17 10:07

onuryartasi


People also ask

How to get a docker container IP address?

How to Get A Docker Container IP Address - examples 1 Using Docker Inspect#N#Docker inspect is a great way to retrieve low-level information on Docker objects. You can pick... 2 Using Docker exec#N#In the following example we will work with the dockerhive_namenode.#N#$ docker exec... 3 Inside the Docker Container More ...

Is it possible to add custom domain names to Docker Compose?

You'll be pleased to know that Docker Compose already adds a mysqlservice domain name (in the web container /etc/hosts) which point to the mysql container. Instead of looking for the mysql container IP address, you can just use the mysqlservice domain name. If you want to add custom domain names, it's also possible with the extra_hosts parameter.

How do Docker containers communicate with each other?

Think of docker network as a pool of available IP addresses. If two containers take on IP addresses from the same pool, they're going to be able to communicate with each other. There are mainly two types of networks, the default or predefined networks and the user-defined networks. Ignore the last two and focus on the first network.

What is a static IP address in Docker?

Static IP addresses don’t change when containers or services are stopped and started, making them useful for permanent networking. Assigning Docker containers static IP addresses is an easy way to make them more accessible. Why Use a Static IP?


1 Answers

I don't think that you are properly using environment variables. Please refer environment variables in compose.

You can access one container from other container simply by using service name of that container. And this is the recommended way.

But if you prefer IP addresses for your own reasons, I am telling you how to set the static ip address of container, i would not recommend it though.

version: '2'
services:
  api:
    build: ./api/
    command: python3 manage.py runserver
    volumes:
      - ./api:/code
    ports:
      - "8000:80"
    networks:
      - dock_net:
          ipv4_address: 127.0.0.3
    container_name: con_api

  web:
    build: ./web/
    command: python3 manage.py runserver
    volumes:
      - ./web:/code
    ports:
      - "8001:80"
    networks:
      - dock_net:
          ipv4_address: 127.0.0.4
    container_name: con_web

networks:
  dock_net:
    driver: bridge
    ipam:
     config:
       - subnet: 127.0.0.0/8
         gateway: 127.0.0.1

This will assign the required IP-addresses to your containers. API will be at 127.0.0.3 and web will be at 127.0.0.4

EDIT: If you want to access service named api from inside the web container then you can use its ip address as we have allocated here. http://127.0.0.3:80/ or you can also use http://api:80/

The api is used in place of IP address because its the service name and when no hostname is given, service name is taken as default hostname. If you want to know about hostnames refer to this question.

like image 71
Ayushya Avatar answered Nov 10 '22 00:11

Ayushya