Docker allows us to create packaged, isolated, and portable environments where we can develop and deploy our applications. It provides complete security and you can run multiple containers on the same operating system without the processes of one container affecting the other. Also, creating a Docker container is as simple as it comes. You can either directly use the Docker run command to create containers directly after pulling the image from Dockerhub or you can also build your own customized images. To do so, simply create a Dockerfile, use instructions to specify base images, and build layers of intermediate images over it. Once you are done, execute the Docker run command to create a container and start developing your application. However, during the development stage, you might come across several instances where you might have to run certain commands inside the Docker containers that have been deployed inside your own local machine or some other server. These commands might be to install packages, or test your application, or simply ping the container. There are several methods you can use to do so. We will discuss three different methods in this tutorial, following which you can run commands inside any Docker container that you have access to. These are -
- Specifying commands inside Dockerfile.
- Running commands by accessing the shell.
- Running commands in background mode.
Please note that before you move ahead with this article, make sure that you have access to a Linux machine with Docker installed in it, and sudo privileges as well. So without any further ado, let’s get started with the tutorial.
1. Using Dockerfile to run commands
Let’s try to create an Ubuntu container by specifying instructions inside Dockerfile. We will try to run the “ apt-get -y update ” command to update the container, and also create a file inside the container, all using Dockerfile. Below are the instructions that you should mention inside your Dockerfile to do so.
FROM ubuntu:latest RUN apt-get -y update RUN echo “Welcome to TGB” > techgeekbuzz.txt
In the above Dockerfile, we have used the FROM instruction to pull the Ubuntu base image from the official Docker registry called Dockerhub. We have used this as a base image and added intermediate image layers by executing RUN instructions after it. We have tried to update the Ubuntu container and create a text file with a simple message using the echo sub-command along with the RUN instruction. Here’s the structure of the directory which happens to be our build context.
Also, this is what our Dockerfile looks like. Now, let’s try to build our Docker image using the Docker build command.
$ docker build -t test .
Now, let’s verify whether the file has been actually created or not by running a container associated with the image. We will use the Docker run command to do so.
$ docker run -it --rm test bash
Here, we have used the remove option because we want the container to be removed automatically once we exit.
You can see that we now have access to the bash of the container. Let’s list all the files.
You can see that our file was successfully created with the content inside it. This is one way of executing commands inside Docker containers. You can simply use the commands along with the RUN instruction. However, this method is not efficient. Here’s the reason why. First, we can’t simply build an image each time we want to run a simple command inside a container. It’s not feasible and it will take up a lot of resources since images are of huge sizes. Moreover, with each RUN instruction being added, an intermediate image layer is built and the cache of all the subsequent layers breaks. This is not a good practice.
2. Running Commands by accessing the shell
Instead of writing instructions inside Dockerfiles, we can simply pull or run an Ubuntu or any other container, access its bash, and start executing commands inside it, the same way we would do in our own local machine. Let’s try to do exactly the same. We will use the Docker run command to access the bash of our container.
$ docker run -it --rm ubuntu bash
We will use the -it options to interact with the container through a terminal driver.
You can see that we now have access to the bash of our container. Let’s try to create a new file and display it.
$ echo “Welcome to TGB” > tgb.txt $ ls $ cat tgb.txt
You can see that our commands have been successfully executed. However, even this method is not efficient. Firstly, because to save the changes, you need to execute the Docker commit command. Moreover, if your container is already running in the background mode on a remote server, you will not be able to execute your commands inside it. For this method to work, you need to have access to the physical machine where your container is running.
3. Running commands in background mode
If you already have a Docker container running in the background or detached mode, you can use the Docker exec command to run commands inside the container. You can even run the commands if the target Docker container is not running on the same machine or server. Let’s first run a container in detached mode.
$ docker run -d --name=myubuntu ubuntu
We have used the detached option along with the Docker run command. To verify whether the container is running or not, we can list all the actively running containers.
$ docker container ps
Now, let’s create a file inside this container using the docker exec command.
$ docker exec myubuntu echo “Welcome to TGB” > tgb.txt
Now, let’s run the myubuntu container using the Docker exec command. As a sub-command, we will use /bin/bash.
$ docker exec -it myubuntu /bin/bash
We now have access to the bash of the container. Let’s list the contents to verify.
$ ls $ cat tgb.txt
To conclude, in this tutorial, we have discussed three different methods that you can leverage to run commands inside Docker containers. You can either specify commands inside Dockerfile along with the RUN instructions, directly access the bash of the container or use the Docker exec command to run commands inside the container running in the background mode. We also discussed the limitations of each method along with some practical examples. We certainly hope that through this article, you will be able to get hands-on with executing commands inside Docker containers and you will be able to figure out the best one that suits your requirements.