How to Easily Run Commands Inside a Running Docker Container


#Easily #Run #Commands #Running #Docker #Container

In Jack Wallen’s How to Make Tech Work tutorial, he shows how using the Docker exec command provides you with more flexibility.

Did you know you can issue commands within a running Docker container? It’s a very handy way to do things like update software, restart services and much more. Even better, Docker makes this pretty easy, thanks to the exec command.

There are two ways you can use exec. The first is to gain access to a running container’s shell, where you can then run any command as if you’re at the Linux terminal. To do this, you might first locate the running container’s ID, which can be done with the command

docker ps

With that ID in hand, you can access the container with a command like

docker exec -it ID /bin/bash

where ID is the first four characters of the container ID in question.

Once at the bash prompt, you can issue the commands you require, such as

apt-get update && apt-get upgrade

Once you’re done, you can exit the container with this command


But what if you want to skip the first step and execute the same commands from outside the container? This is also possible using the exec command.

Let’s say you want to run the same commands for updating and upgrading the software in your container. You’ll still need the container ID and can run a command like

docker exec ID apt-get update && sudo apt-get upgrade -y

where ID is the first four characters of the container ID in question.

You’re not limited to just updating and upgrading your containers; any command that can be run from the bash prompt can be run from outside the container.

Enjoy this new flexibility that comes with the Docker exec command.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.