60

What is the use of container_name in docker-compose.yml file? Can I use it as hostname which is nothing but the service name in docker-compose.yml file.

Also when I explicitly write hostname under services does it override the hostname represented by service name?

1

3 Answers 3

92

hostname: just sets what the container believes its own hostname is. In the unusual event you got a shell inside the container, it might show up in the prompt. It has no effect on anything outside, and there’s usually no point in setting it. (It has basically the same effect as hostname(1): that command doesn’t cause anything outside your host to know the name you set.)

container_name: sets the actual name of the container when it runs, rather than letting Docker Compose generate it. If this name is different from the name of the block in services:, both names will be usable as DNS names for inter-container communication. Unless you need to use docker to manage a container that Compose started, you usually don’t need to set this either.

If you omit both of these settings, one container can reach another (provided they’re in the same Docker Compose file and have compatible networks: settings) using the name of the services: block and the port the service inside the container is listening in.

version: '3'
services:
  redis:
    image: redis
  db:
    image: mysql
    ports: [6033:3306]
  app:
    build: .
    ports: [12345:8990]
    env:
      REDIS_HOST: redis
      REDIS_PORT: 6379
      MYSQL_HOST: db
      MYSQL_PORT: 3306
8
  • 5
    container_name: <mycontainer> has the benefit that you have nice container names when doing docker ps or listing containers or other operations. Otherwise container_name is automatically assigned a name like <docker_compose_folder>_<service_name>_<incrementing_number_of_containers_with_same_name>. Oct 31, 2021 at 7:08
  • 2
    It means one has to be carefull of not overriding container_name with generic names like db, mysql, app, etc. I usually define a PREFIX variabile in .env and use it like this: container_name: ${PREFIX}-db. Oct 31, 2021 at 7:19
  • 2
    There is a point to setting the hostname, directly or indirectly, when your container expects the same name between first boot and future boots, such as when creating a replica-set in mongodb. It hardcodes the hostname in its configuration, and if it is random, it causes issues. Mar 25, 2022 at 1:47
  • 1
    Setting hostname also configures an alias. If you run getent host <hostname> from another container on the same network, it should resolve to the other container's IP. Jul 23, 2023 at 22:51
  • @JohnChristopher Instead of manually defining a prefix, you could also use the project name, for example: container_name: "${COMPOSE_PROJECT_NAME}-webserver"
    – Martin
    Mar 12 at 20:40
32

The easiest answer is the following:

container_name: This is the container name that you see from the host machine when listing the running containers with the docker container ls command.

hostname: The hostname of the container. Actually, the name that you define here is going to the /etc/hosts file:

$ exec -it myserver /bin/bash

bash-4.2# cat /etc/hosts
127.0.0.1   localhost
172.18.0.2  myserver

That means you can ping machines by that names within a Docker network.

I highly suggest set these two parameters the same to avoid confusion.

An example docker-compose.yml file:

version: '3'
services:
    database-server:
        image: ...
        container_name: database-server
        hostname: database-server
        ports:
            - "xxxx:yyyy"

    web-server:
        image: ...
        container_name: web-server
        hostname: web-server
        ports:
            - "xxxx:xxxx"
            - "5101:4001" # debug port
4
  • 4
    hostname is written only to the host file of one container, the one run by the service. It's not written to the host file of every services run with docker compose. You ping machines by container_name, not hostname. Oct 31, 2021 at 7:23
  • For those wondering, you do not need to set either of container_name or hostname to be able to reference your containers for example in URLs. You can simply use the block name of the service in your docker-compose.yml file, even though the container's actual name (shown for example with docker ps -a) will be an auto-generated name. Jan 13, 2022 at 2:28
  • Can you complement the answer with domainname, to differentiate them?
    – jcarlosweb
    Dec 31, 2022 at 17:11
  • @JohnChristopher Setting hostname also configures an alias. If you run getent host <hostname> from another container on the same network, it should resolve to the other container's IP. Jul 23, 2023 at 22:51
3

you can customize the image name to build & container name during docker-compose up for this, you need to mention like below in docker-compose.yml file. It will create an image & container with custom names.

version: '3'
services:
  frontend_dev:
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    build:
      context: .
      dockerfile: Dockerfile.dev
    image: "mycustomname/sample:v1"
    container_name: mycustomname_sample_v1
    ports:
      - '3000:3000'
    volumes:
      - /app/node_modules
      - .:/app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.