Interactive Shell Using Docker Compose

TLDR
To start an interactive shell in a Docker Compose service, use the docker-compose exec command. This allows you to debug and interact with running containers.
Docker Compose simplifies the management of multi-container applications. Sometimes, you need to access a running container to debug or interact with it. This guide explains how to start an interactive shell in a Docker Compose service.
Step 1: Define Your Services
Ensure your docker-compose.yml file defines the services you want to run.
Example docker-compose.yml
version: '3.8'
services:
app:
image: node:18
working_dir: /app
volumes:
- .:/app
command: npm start
db:
image: postgres:15
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
Explanation
app: Defines a Node.js application service.db: Defines a PostgreSQL database service.volumes: Mounts the current directory into the container.environment: Sets environment variables for the database.
Step 2: Start the Services
Start the services using the docker-compose up command.
Command
docker-compose up -d
Explanation
-d: Runs the services in detached mode.
Step 3: Access an Interactive Shell
Use the docker-compose exec command to start an interactive shell in a running service.
Command
docker-compose exec app sh
Explanation
exec: Executes a command in a running container.app: Specifies the service name.sh: Starts a shell session.
Step 4: Debug and Interact
Once inside the container, you can run commands, inspect files, and debug your application.
Example
ls
cat app.js
npm install
Explanation
ls: Lists files in the current directory.cat app.js: Displays the contents ofapp.js.npm install: Installs dependencies.
Best Practices
- Use Descriptive Service Names: Make it easy to identify services.
- Limit Access: Avoid running commands in production containers unless necessary.
- Automate Debugging: Use scripts to automate common debugging tasks.
By following these steps, you can effectively use Docker Compose to start an interactive shell, making debugging and development more efficient.
Related Resources
- How to Access Docker Container Shell: more shell methods
- Docker Compose: Running Multiple Commands: Compose commands
- Enter Docker Container with New TTY: TTY sessions
- Introduction to Docker: Compose: Compose guide
Try it hands-on
Run the commands from this article in the browser. Nothing to install.
Docker Terminal Simulator
Practice Docker commands in an interactive browser terminal. Learn images, containers, logs, exec, Dockerfile builds, volumes, networks, and Docker Compose with a live Docker daemon visualization.
How Docker Works Under the Hood
Watch what really happens when you run docker run -p 8080:80 nginx, one layer at a time. Step down the whole stack: the CLI, the daemon, the registry pull, containerd, the OCI runtime bundle, runc, the running container, and the shared Linux kernel. Every stage shows the real low-level command you can run yourself, so it doubles as a tour of the primitives that make a container: namespaces, cgroups, and runc. A container is not a small VM, and this shows you why.
We earn commissions when you shop through the links below.
Svix
Webhooks as a service
Svix Dispatch sends your webhooks for you: retries with exponential backoff, signed payloads, idempotency keys, and a delivery log your customers can see.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorTags
Found an issue?
Related Posts
Also worth your time on this topic
Communication Between Multiple Docker-Compose Projects
Learn how to enable communication between multiple Docker-Compose projects using shared networks and environment configurations.
Running Docker Containers on Your Linux Server
Install Docker and Docker Compose on Ubuntu, run your first container, deploy a WordPress stack with docker-compose, and set up Nginx as a reverse proxy in front of your containers.
60 minutes
Docker Security Hardening Checklist
Comprehensive security checklist for hardening Docker containers, images, and runtime environments.
60-90 minutes