Docker Community Forums

Share and learn in the Docker community.

Custom container name for docker-compose


(Tech687) #1

$ 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?


(Tech687) #2

No reply…?


(Neuroglia) #3

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 ^^