Tutorials

How To Set Hostname In Docker Compose | Docker Compose Tutorial

How To Set Hostname In Docker Compose | Docker Compose Tutorial
Written by itsubuntu

Last modified on March 12th, 2022 at 11:14 am

In this docker-compose tutorial, we will show you how to set a hostname in Docker Compose.

Docker is a  platform as a service (PaaS) product that delivers software in packages called containers. A container is a standard unit of software that contains code and all its dependencies. Containers help the application to run in any environment without any issues. . A Docker container image is a package of software that includes everything needed to run an application.

How To Set Hostname In Docker Compose | Docker Compose Tutorial

The correct way of assigning a hostname in docker-compose is to define it under the service like this

services:
  some-service:
    networks:
      some-network:
        aliases:
          - alias1
          - alias2

But some users are experiencing issues with this method. If this is not working for you with docker run then assign the container a name like below:

docker-compose run --name alias1 some-service

Another simplest way  is to just set the container name in the docker-compose.yml

services:
  dns:
    image: phensley/docker-dns
    container_name: affy

About the author

itsubuntu

Leave a Comment