Custom container name for docker-compose

$ cd ~/test

$ cat docker-compose.yml
version: '3.3’
services:
db:
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
dbdata:

$ docker-compose up -d
Starting test_db_1 … done
Starting test_wordpress_1 … done

Instead of test_wordpress_1 could I set a name of my choice?

No reply…?

Hey tech687,

To get faster answers, maybe you should put a bit of efforts into explaining your problem. Like, you know, formatting and such :wink:

Now, for what I understand, you just want to change the name of your container. In docker-compose, you can do this by setting the “container_name” property on any of your containers.

For example:

db:
   image: mysql:5.7
   container_name: db

If you don’t want to change the container name, but needs ‘user-friendly’ names for discovery, you can as well set the ‘hostname’ property, or you can add network aliases, like such:

  db:
       image: mysql:5.7
       container_name: mycontainername
       hostname: myhostname
       networks:
           default:
                aliases:
                   myalias

I hope this helps

Regards

P.S: Feel free to consult docker-compose documentation for information, it will save you 6 months sitting on your hands instead of a simple… google search ^^

8 Likes

I am a person starting to learn docker.

If you run into the same problem as me:

ERROR: The Compose file './docker-compose.yml' is invalid because:
services.dynamodb-local.networks.default contains "ALIAS_NAME", which is an invalid type, it should be an array

be sure to add a hyphen when writing your alias in docker compose. Using the same example as above:

  db:
       image: mysql:5.7
       container_name: mycontainername
       hostname: myhostname
       networks:
           default:
                aliases:
                   - myalias

This worked for me, i hope it might be useful.

1 Like

Thanks for reporting back! Without reading any documentation, the plural form of aliases may already reveal it’s a list, a.k.a. an array. If you need any help on YAML, see, e.g.:

Following article would help

Custom Container Names In Docker Compose

1 Like

To set custom container names in Docker Compose, you can utilize the container_name field within your service definition. Here’s a brief guide:

Open your docker-compose.yml file.

Under the desired service, add the container_name field followed by the desired custom name. For example:

services:
  myservice:
    image: myimage
    container_name: custom-container-name

Replace myservice with the name of your service and custom-container-name with your preferred container name.

Save the changes to your docker-compose.yml file.

When you bring up your Docker Compose environment using docker-compose up, the container associated with the service will have the custom name specified in the container_name field.

Keep in mind that container names must be unique within your Docker environment. If you have multiple instances of the same service, you may need to use some form of dynamic naming or include additional identifiers to ensure uniqueness.To get comprehensive guide check out this article Custom Container Names In Docker Compose

1 Like

Hello,

How can I make a mix of default container name with a custom postfix?

I like that if I drop container_name then it’s auto-generated as [project]-[service]-[index] because I can change “name” of my project and it will be propagated to the container name. But in one case I’d like to have my custom postfix in container name with version of DB engine and don’t loose the autonaming feature. So I’d like to have something like this:

name: my-project
services:
  service1:
    container_name: ${autogenerated-container-name}-${dbVersionSuffix}

Is this possible?
I’ve tried to do

services:
  service1-${dbVersionSuffix}:

But it throws an error.

Cheers!