-1

I want to connect to my Docker containers from my Docker host using hostnames.

I already know how to connect to containers by mapping their ports using docker run -p <host-port>:<container-port> ... and then access them through localhost.

Also, I can connect to containers using their IP-addresses given by docker inspect <container>. But these IP-adresses are not static.

How can I give containers hostnames, so that I can connect to them through exposed ports without having to think about non-static IPs?

Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • (In many contexts you _can’t_ use the `docker inspect` IP addresses; they only work from a Linux-native host that’s the exact same host running the Docker daemon. You should use `localhost` and the published port, which works from the same host whenever Docker isn’t in a VM.) – David Maze Dec 10 '19 at 13:41
  • @DavidMaze - "You should use localhost and the published port..." - don't you mean that this also works, when Docker is in a VM, like Docker for Mac? – Shuzheng Dec 10 '19 at 14:03
  • If you're using Docker Toolbox (say, on a Windows 7 host) you need the VM's IP address. Docker for Mac does internally use a VM, but it knows how to republish ports so that `localhost` works from the physical host. – David Maze Dec 11 '19 at 00:21

1 Answers1

0

Use docker-compose and make services in them. Each container will be a part of a Service and one container can talk to other container using the service name the container belongs to.

Ex:

 $ cat docker-compose.yml
version: '3.1'

services:
    server:
        image: redis
        command: [ "redis-server" ]
    client:
        image: redis
        command: [ "redis-cli", "-h", "server", "ping" ]
        links:
            - server
  $
  $
  $ docker-compose up
Starting server_1 ... done
Starting client_1 ... done
Attaching to server_1, client_1
client_1  | PONG
server_1  | 1:C 10 Dec 2019 12:59:20.161 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
server_1  | 1:C 10 Dec 2019 12:59:20.161 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
server_1  | 1:C 10 Dec 2019 12:59:20.161 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
server_1  | 1:M 10 Dec 2019 12:59:20.162 * Running mode=standalone, port=6379.
server_1  | 1:M 10 Dec 2019 12:59:20.162 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
server_1  | 1:M 10 Dec 2019 12:59:20.162 # Server initialized
server_1  | 1:M 10 Dec 2019 12:59:20.162 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
server_1  | 1:M 10 Dec 2019 12:59:20.162 * Ready to accept connections
client_1 exited with code 0

Here, I created two services, server and client. server starts a redis-server and client tries to connect to the server. Also, note that I haven't exposed ports here, so the client container is talking to the server container using server (service-name)

client:
    image: redis
    command: [ "redis-cli", "-h", "server", "ping" ]
Shubham
  • 2,847
  • 4
  • 24
  • 37