Entering a Docker Container via Docker Compose CLI

by Dec 25, 2025IT Tips0 comments

TIL that you can enter a container straight from docker compose

Run bash as the containers default user

docker compose -f docker-compose.yaml exec php bash

Run bash as the root user

docker compose -f docker-compose.yaml exec -u 0 php bash

Where docker-compose.yaml is your yaml file e.g. it could be something like compose.dev.yaml or compose.prod.yaml

php is the name of your container under the services property in the docker yaml file (see example yaml below)

bash is your process to execute (e.g. a bash shell)

-u 0 is the UID (User ID) of the user you want bash to run as

services:
    php:
        restart: unless-stopped
        env_file: 
            -   path: ./php.env
                required: false
        build:
            context: ./docker/config/php/
            dockerfile: Dockerfile
            network: host
            args:
                CONTAINER_GID: '${CONTAINER_GID:-1000}'
                CONTAINER_UID: '${CONTAINER_UID:-1000}'
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        networks:
            - wms-dev-net
        volumes:
            - '${HOME}/.ssh:/home/wms/.ssh'
            - '.:/var/www/wms'
            - './bin/bash_completion.sh:/etc/bash_completion.d/cake'
            - '~/.composer/docker-cache/:/root/.composer:cached'
            - '~/.config/composer/:/root/.config/composer:cached'
            - 'vscode-extensions:/root/.vscode-server/extensions'
        links:
            - postgres
            - cups
            - mailpit
networks:
    wms-dev-net:
        driver: bridge
        enable_ipv6: false
    host:
        enable_ipv6: false
volumes:
    tgnpostgres:
        driver: local
    vscode-extensions:
        driver: local

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.