Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does docker use service name or container name to resolve service reference in docker compose?

redis:
    image: redis
    container_name: my-redis

If I had the above docker compose.yml file, then somewhere else where I need to, for example, reference the redis host, should I use redis or my-redis in the client connection string?

like image 611
Nico Avatar asked Oct 29 '25 18:10

Nico


1 Answers

You can use both to address redis, given the assumption you are in the same docker network.

services:
  redis:
    image: redis
    container_name: my-redis

The container name will be my-redis, but it is also registered with the network alias redis.

When you have only a single container, the alias may not be useful, or it may not be apparent why it's useful. It starts to become useful when you deploy something with more than 1 replica.

You can see this by inspecting the container.

docker inspect my-redis

You can also leave the container name away to let compose use the default container name <project>-<service>-<replica> i.e. myproject-redis-1. In fact, this is required when you want to deploy multiple replicas because you cannot deploy multiple containers with the same name.

name: myproject
services:
  redis:
    image: redis
    deploy:
      replicas: 2

Typically, people use the service name over the container name.

like image 129
The Fool Avatar answered Nov 01 '25 11:11

The Fool



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!