Introduction
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/redisimage.png?resize=1100%2C105&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/containerCreated.png?resize=1071%2C375&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerps-1.png?resize=1085%2C466&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerimglist.png?resize=1101%2C143&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerexecute.png?resize=736%2C166&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/containershell.png?resize=938%2C268&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/rediscli.png?resize=919%2C344&ssl=1)
we can check it’s running by typing ping, and we get PONG back, which is a good sign.
![network ping pong](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/pong.png?resize=934%2C385&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/retrievekey.png?resize=928%2C467&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/counter.png?resize=958%2C639&ssl=1)
Let’s exit from the redis-cli with exit, and now we’re back in our shell on the container.
![exit redis-cli](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/exit-1.png?resize=900%2C627&ssl=1)
So, if we type docker ps and execute it, we can see that the redis1 container.
![docker ps new](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerpsnew.png?resize=1040%2C346&ssl=1)
If now we want to stop the redis1 container, we can do so with the docker stop command like this.
![docker stop command](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerstop.png?resize=1039%2C351&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/bothstate.png?resize=1061%2C578&ssl=1)
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](https://i0.wp.com/monkelite.com/wp-content/uploads/2020/04/dockerremove-1.png?resize=1076%2C565&ssl=1)
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.