How to execute a command inside the container


In this article, you’ll learn how to execute a command inside the container and also shows the directory list inside the container. Perform some experiments with the Redis image. Please, read our previous article before proceeding to this article where we Understand the Docker Container Commands.

Create a Container with Redis image

Let’s see how easy Docker makes it to run up an instance of Redis and experiment with it. So here we’re on the command prompt on a machine running Docker for Windows, and we’re currently in the Linux Mode.

docker redis image

We’re running it in the background, so with a -d flag, and we’re going to give this container the name redis1. then, we’re going to expose the container’s port 6379, which is the Redis default, and we’re going to expose that on port 6379 on my local machine. And that means that we could actually connect to this Docker container locally.

Now, when we run this for the first time, it will need to download the Redis image from the Docker Hub container registry to our machine. And this is a one-time action though. Once a container image is available locally, starting up new containers based on the image will be much quicker. And once the Docker container has started up, it will print out the ID of the newly created container.

Create new container

So, we can use the docker ps command to see what containers are running. And so when we issue this command, we can see the container ID, the image name, the startup command that the container is running, when it was created, whether it’s running or not, what ports we’ve published, and what the container name is.

docker ps command

We can also see the container image we downloaded with the docker image ls command execute. This shows me all the images we have locally.

docker image list

Docker Exec command

In our previous example, we started a Docker container running Redis, and we exposed port 6379 on it. And that means we could use the redis-cli and connect to our container on localhost port 6379.

But let’s suppose that we don’t have the redis-cli installed locally. we actually don’t on this computer and we use Docker to run that as well. We can use docker exec to run a command inside a container, and our redis1 container actually already has the redis-cli installed.

So, let’s see how we can run another command on the container that we’re already running. here, is we want to execute the sh command. So we’re just opening a shell, and we’re going to do this against the redis1 container. The -it flag means that we want to run this in interactive mode, where we essentially attach our console to this process in the container, and so we can see its output and type commands in.

docker execute

The -it flags are often used in the Docker run command instead of -d when we don’t want to be detached and run in the background.

After executing this, as you can see, we’re now inside a shell in our container. And so if we do something like ls -al, we can see the contents of the file system in the container.

container exec shell

So, let’s execute the redis-cli with redis-cli, and this will just attach me to the locally running Redis inside this container.

redis cli command

we can check it’s running by typing ping, and we get PONG back, which is a good sign.

network ping pong

Now, Let’s run the few basic Redis commands. First, we’ll set a key in the Redis cache with set name mark, and we can retrieve that key with getting the name.

retrieve key from redis

we can also increment a new counter with Incr Counter. And let’s do that twice. And now if we say get a counter, it will show us that the counter has a value of 2.

docker exec redis image

Let’s exit from the redis-cli with exit, and now we’re back in our shell on the container.

exit redis-cli

So, if we type docker ps and execute it, we can see that the redis1 container.

docker ps new

If now we want to stop the redis1 container, we can do so with the docker stop command like this.

docker stop command

Now, when a container is stopped, if we run the docker ps command, it doesn’t show up, but we can execute docker ps -a, and that will show that our redis1 container is still there, but it’s in a stopped state.

docker state

Now we could restart it if we wanted to, although since Redis is an in-memory cache, it will have forgotten those keys that we set. But let’s clean up after ourselves. we’re going to delete this container now with docker rm redis1, and that removes this container. And if we want to delete the image, we can execute docker image rm redis.

docker image remove

I hope you have been enjoyed this article. We’ll get into more details about this when we start Mount Docker Volume into a Container in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *