Skip to content

Get container name by service name

Maksym Zaporozhets edited this page Jan 8, 2024 · 4 revisions

composition:get-container-name

Description

Get Docker container name by service name in the docker-compose*.yaml.

Usage

php "${DOCKERIZER_PROJECTS_ROOT_DIR}dockerizer_for_php/bin/dockerizer" composition:get-container-name mysql

This is especially useful for Bash aliases when many compositions have a php or mysql service, but you need to get a container name for docker exec or other commands.

For example, here is an alias for the composer install command:

# Export a global variable containing the path to your projects root directory
export DOCKERIZER_PROJECTS_ROOT_DIR=${HOME}/misc/apps/
# Alias to run Dockerizer from any directory
alias DOCKERIZER='php -d xdebug.start_with_request=trigger ${DOCKERIZER_PROJECTS_ROOT_DIR}dockerizer_for_php/bin/dockerizer'
# Run `CI` inside a container of a `php` service without entering the container.
# You should be in the directory containing `docker-compose.yaml` file.
alias CI='docker exec -it $(DOCKERIZER composition:get-container-name php) composer install'

Command arguments and options

  • service-name (first argument, required): Service name in the docker-compose*.yaml.
  • --path (optional): Path to docker-compose files. Default: getcwd().
Clone this wiki locally