docker exec: run a command in a running container - Rocketeers

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com) 

 On this page

 Knowledge
---------

docker exec: run a command in a running container
=================================================

### [\#Development](https://rocketee.rs/index.php/development)

docker exec runs a command inside a container that is already running. The classic use is opening an interactive shell with docker exec -it, but it is just as useful for one-off commands.

 Published by [Mark van Eijk](https://rocketee.rs/index.php/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [Open a shell inside a container](#content-open-a-shell-inside-a-container)
2. [Run a one-off command](#content-run-a-one-off-command)
3. [Useful flags](#content-useful-flags)
4. [exec vs run vs attach](#content-exec-vs-run-vs-attach)

[\#](#content-open-a-shell-inside-a-container "Permalink")Open a shell inside a container
-----------------------------------------------------------------------------------------

The most common use of `docker exec` is getting a shell in a running container. The `-it` flags give you an interactive terminal:

 ```
docker exec -it my-container bash

```

If the image is minimal and has no `bash`, fall back to `sh`:

 ```
docker exec -it my-container sh

```

The `-i` keeps STDIN open and `-t` allocates a pseudo-TTY; together they make the shell behave like a normal terminal.

[\#](#content-run-a-one-off-command "Permalink")Run a one-off command
---------------------------------------------------------------------

You do not need a shell for a single command. Anything after the container name is run inside it:

 ```
docker exec my-container ls -la /app
docker exec my-container cat /etc/hostname

```

For a Laravel container you might run Artisan, or open a database client:

 ```
docker exec -it my-app php artisan migrate
docker exec -it my-db mysql -u root -p

```

[\#](#content-useful-flags "Permalink")Useful flags
---------------------------------------------------

 ```
docker exec -u www-data my-container whoami   # run as a specific user
docker exec -w /app my-container pwd           # set the working directory
docker exec -e DEBUG=1 my-container env        # pass an environment variable

```

[\#](#content-exec-vs-run-vs-attach "Permalink")exec vs run vs attach
---------------------------------------------------------------------

These look similar but do different things:

- `docker exec` runs a command in a container that is **already running**.
- `docker run` creates a **new** container from an image.
- `docker attach` connects to the container's main process (its PID 1), so `Ctrl+C` there can stop the container, which is usually not what you want.

To find the container name or ID to pass to `exec`, list running containers with `docker ps`. If you started everything with Compose, see [docker compose up](/docker-compose-up).

### Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#Development](https://rocketee.rs/index.php/development)

- [Install PHP memcached extension on macOS](https://rocketee.rs/index.php/install-php-memcached-extension-on-macos)
- [How to delete a local (and remote) Git branch](https://rocketee.rs/index.php/git-delete-local-branch)
- [Fix: Cannot connect to the Docker daemon at unix:///var/run/docker.sock](https://rocketee.rs/index.php/cannot-connect-to-docker-daemon)
- [git stash pop vs apply: save and restore changes](https://rocketee.rs/index.php/git-stash-pop)
- [How to clean up Docker with prune (images, volumes, system)](https://rocketee.rs/index.php/docker-prune)
- [What port does SSH use (and how to change it)](https://rocketee.rs/index.php/ssh-port)

 [View all 12 articles →](https://rocketee.rs/index.php/development)
