Rpj
Rpj

Reputation: 6080

How to customize container name in docker-compose

version: '3.8'

services:
  rocketchat:
    image: foo/foo:3.9.1
    container_name: $FOO_CONTAINER_NAME


.env file contains
    FOO_CONTAINER_NAME=foo_container

I want the container name to reflect lifecycle environment (say dev, staging, prod), how to customize this. I tried the following, but it didn't help

container_name: $FOO_CONTAINER_NAME_$ENV
container_name: $FOO_CONTAINER_NAME_{$ENV}"

ENV is declared in .env file as dev, staging ..

Upvotes: 0

Views: 898

Answers (1)

ItayB
ItayB

Reputation: 11337

docker-compose.yaml content:

version: '3.8'

services:
  rocketchat:
    image: foo/foo:3.9.1
    container_name: ${FOO_CONTAINER_NAME}_${ENV}

.env content:

FOO_CONTAINER_NAME=foo_container
ENV=staging

running docker-compose config output:

services:
  rocketchat:
    container_name: foo_container_staging
    image: foo/foo:3.9.1
version: '3.8'

Upvotes: 1

Related Questions