✦ For everyone, free.

Practical knowledge for real and everyday life

Home

9.4.4.1 Exec Service Commands

A focused guide to Exec Service Commands, connecting core concepts with practical Docker and container operations.

Exec service commands are the specific commands run inside a running service's container through docker compose exec, covering everything from simple inspection commands to running application-specific scripts, all executed within that container's existing, already-running environment.

Running a Simple Inspection Command

A straightforward command checks some aspect of the running container's current state.

docker compose exec api ls -la /app
docker compose exec api cat /app/config.yaml
Running an Application-Specific Command

Many applications provide their own command-line utilities, runnable through exec exactly as they would be run directly on a non-containerized system.

docker compose exec api npm run db:migrate
docker compose exec api python manage.py shell
Running a Command With Specific Environment Variables

Additional environment variables can be set specifically for this one executed command, without affecting the container's actual, ongoing environment.

docker compose exec -e DEBUG=true api npm run diagnose
Running a Command as a Specific User

For a container that runs as a particular user by default, exec can override this to run the specific command as a different user, useful for tasks requiring different permissions.

docker compose exec -u root api chmod 755 /app/scripts/deploy.sh
Why Exec Service Commands Matter

The flexibility to run any arbitrary command within a service's already-running environment, with options for custom environment variables and user context, makes exec an essential tool for everything from quick inspection to running application-specific administrative tasks against a live, running service.