Executing Commands
Part of the Introduction to Docker section of Coddy's Terminal journey — lesson 19 of 40.
Once a container is running in the background, you can run extra commands inside it with docker exec:
docker run -d --name live nginx
docker exec live echo "hello from inside"The first command starts live in the background. The second runs echo inside that already running container and prints its output.
This is the everyday way to look around or run a quick task in a container that is already up, without stopping and restarting it. Note that exec needs a running container, so the image has to be one that stays up (like nginx).
Challenge
EasyStart a detached nginx container named server, then use docker exec to run echo inside the container within it.
Cheat sheet
Use docker exec to run a command inside an already running container:
docker run -d --name live nginx
docker exec live echo "hello from inside"exec requires the container to already be running.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.