Executing arbitrary commands with exec

It's maybe the most powerful all--around tool when working with Docker images. The exec command allows you to run arbitrary commands inside a running container.

In opposite of the overriding the CMD or ENTRYPOINT, the command started using docker exec will only run while the container's primary process (PID 1) is running.

Tip

If the container is paused, then the docker exec command will fail with an error.

Also, the syntax of the exec command is different. It's not a part of the Docker run command, it's rather a new separate command, executed after you run your container with the docker run. Consider the following example and run the following in one shell terminal:

docker run -it ubuntu bash --name myUbuntuBash ...

Get Developing with Docker now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.