How To Easily Run Commands Inside A Running Docker Container

how-to-easily-run-commands-inside-a-running-docker-container

How To Easily Run Commands Inside A Running Docker Container

on

How to Easily Run Commands Inside a 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

exit

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.

Also See

  • How to Create and Use a Docker Secret From a File (+Video) (TechRepublic)
  • How to Stop and Remove All Docker Containers with 2 Simple Commands (TechRepublic)
  • How to build a Docker image from a Dockerfile (TechRepublic)
  • How to Add the Docker Scout Feature to the Docker CLI (TechRepublic)
  • Programming languages and developer career resources (TechRepublic on Flipboard)
  • Cloud
  • Developer
  • Open source
  • Software

on October 12, 2023, 5:59 PM EDT How to Easily Run Commands Inside a 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…