TNS
VOXPOP
Will JavaScript type annotations kill TypeScript?
The creators of Svelte and Turbo 8 both dropped TS recently saying that "it's not worth it".
Yes: If JavaScript gets type annotations then there's no reason for TypeScript to exist.
0%
No: TypeScript remains the best language for structuring large enterprise applications.
0%
TBD: The existing user base and its corpensource owner means that TypeScript isn’t likely to reach EOL without a putting up a fight.
0%
I hope they both die. I mean, if you really need strong types in the browser then you could leverage WASM and use a real programming language.
0%
I don’t know and I don’t care.
0%
Operations

How to Use the Docker exec Command

exec allows you to execute commands within an already deployed container, or outside of the container. Here is how.
Sep 23rd, 2023 6:00am by
Featued image for: How to Use the Docker exec Command

For those who are just getting started on your journey with Docker containers, there is much to learn. Beyond pulling images and deploying basic containers, one of the first things you’ll want to understand is the exec command.

Essentially, the exec command allows you to run commands within an already deployed container. The exec command allows you to do this in two different ways…from inside or outside the container. I’m going to show you how to do both. In the end, you’ll be better prepared to interact with your running Docker containers.

What You’ll Need

The only thing you’ll need for this is a running instance of the Docker runtime engine installed on a supported platform. I’ll demonstrate on Ubuntu Server 22.04.

In case you don’t have Docker installed, let’s take care of that first. If you already have Docker installed, go ahead and jump to the next section.

Install Docker

Before you can install Docker on Ubuntu Server, you must first add the official Docker GPG key with the command:


You’ll be prompted for your sudo password.

Once the GPG key is successfully added, create the necessary Docker repository, with the following command:

Install a few dependencies with this command:


Update apt with:


Install Docker with the command:


Next, you must add your user to the docker group with the command:


Log out and log back in so the changes take effect.

Congrats, Docker is now ready to go.

Deploy a Test Container

To use the exec command, we first must deploy a simple test container. For that, we’ll use the tried-and-true NGINX and deploy a container with the command:


After running the command, Docker should report back the ID of the container. If you miss that, you can always view it with:


You’ll only need the first four characters of the ID.

Access the Running Container’s Shell

Now, we can access the running container’s shell, which will allow you to run command from inside the container. This is done with the exec command like so:


Where ID is the first four characters of the running container’s ID. You should then find yourself at the running container’s bash prompt. Let’s say you want to upgrade the software. You can do so with the commands:


After the upgrade completes, you can exit the shell with the command:


Let’s simplify the process.

Run a Command from Outside the Container

Thanks to the exec command, you don’t have to first access the container’s shell before running a command. Instead, you can send the command inside the container. Let’s stick with our example of updating and upgrading the running NGINX container. Again, we’ll need the container ID for this command.

To update and upgrade the software for our NGINX container (without first accessing the container), the command would be:


Where ID is the first four characters of the container ID.

The use of the && operator is common in Linux and makes it possible to daisy chain commands together such that they run one after another.

You can use this method to run just about any command. For example, you could view the index.html file used by NGINX with the command:


Where ID is the first four characters of the container ID.

Let’s copy a new index.html file into the running container and then use exec to view it. Create the new file on your host with:


In that file, paste the following contents:

Save and close the file. Next, copy the file to the running NGINX container with the command:


Where ID is the ID of the running container.

Now, we can view the contents of the file with:


The output should simply be:


And that’s how you use the Docker exec command. With this tool, you can better (and more efficiently) manage your running Docker containers.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.