Hadrien Delphin
Hadrien Delphin

Reputation: 620

call a docker container by it name

I would like to know if it's possible to use my docker container name as host instead of the IP.

Let me explain, here's my docker-compose file :

version : "3"
  services:
    pandacola-logger:
        build: ./
        networks:
          - logger_db
        volumes: 
          - "./:/app"
        ports:
          - 8060:8060
          - 10060:10060
        command: npm run dev

      logger-mysql:
        image: mysql
        networks:
          - logger_db
        command: --default-authentication-plugin=mysql_native_password
        environment:
          MYSQL_ROOT_PASSWORD: Carotte1988-
          MYSQL_DATABASE: logger
          MYSQL_USER: logger-user
          MYSQL_PASSWORD: PandaCola-
        ports:
          - 3306:3306

      adminer:
        networks:
          - logger_db
        image: adminer
        restart: always
        ports:
          - 8090:8090

    networks:
      logger_db: {}

Sorry the intentation is a bit messy

I would like to set the name of my logger-mysql in a the .env file of my webservice (the pandacola-logger) instead of his IP adress

here's the .env file

HOST=0.0.0.0
PORT=8060
NODE_ENV=development
APP_NAME=AdonisJs
APP_URL=http://${HOST}:${PORT}
CACHE_VIEWS=false
APP_KEY=Qs1GxZrmQf18YZ9V42FWUUnnxLfPetca
DB_CONNECTION=mysql
DB_HOST=0.0.0.0  <---- here's where I want to use my container's name
DB_PORT=3306
DB_USER=logger-user
DB_PASSWORD=PandaCola-
DB_DATABASE=logger
HASH_DRIVER=bcrypt

If you can tell me first, if it's possible, and then, how to do it, it would be lovely.

Upvotes: 0

Views: 799

Answers (1)

heyitsvajid
heyitsvajid

Reputation: 1121

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

Reference

For Example:

version: '2.2'
services:
  redis:
    image: redis
    container_name: cache
    expose:
      - 6379
  app:
    build: ./
    volumes:
      - ./:/var/www/app
    ports:
      - 7731:80
    environment:
      - REDIS_URL=redis://cache
      - NODE_ENV=development
      - PORT=80
    command:
      sh -c 'npm i && node server.js'

networks:
  default:
    external:
      name: "tools"

Upvotes: 1

Related Questions