Execute a shell command inside a Docker container (3 methods)
Running projects inside containers is a bread and butter task of a developers life. It’s not rare to need to execute a command against a container and Docker gives us multiple options to do so.
- Execute a command inside a new container
- Execute a command inside a stopped container
- Execute a command inside a running container

Execute a command inside a new container
Often we want to run a command in an interactive mode inside a new, just instantiated container. Let’s create a new server
container based on the nginx
image, and run a bash
command after the container creation.
docker container run -it --name server nginx bash
Execute a command inside a stopped container
Same thing but for the stopped container that already exists.
docker container start server -at bash
Execute a command inside a running container
What if the container is already running, we don’t want to interrupt its internal processes, but we really have to get into it to add a plugin or check some logs? The exec
command comes handy then.
docker container exec -it server bash
Quick copy/paste kinda article today. Hopefully, you found it helpful. Ciao!