After you build a docker image and you have created a running instance of it or in other words, you have created a docker container, you might want to execute some commands inside the docker container to either install a package or print something or browse through the directories inside the container. Docker provides you with several ways to do exactly the same. You can easily access the container environment, execute commands inside the environment and access or create files and directories there too. In this article, we will discuss some ways to run or execute commands inside the docker container.
Using Docker exec command
You can run a command inside a container using the docker exec command through the command line of your local machine. To do this, you need to have the container Id of the container inside which you wish to execute a command. To get the container Id of all the containers, you can use the following command −
sudo docker ps −a
The above command will display all the containers with their names, ids and other information. You can copy the container ID of the container in which you want to run the command. Let’s say you want to echo a statement inside that container, you can do so using the following command.
sudo docker exec −it <container−id> echo "Welcome to tutorialspoint"
Note that to execute the command inside the container using exec command, your container must be in the running state otherwise it would throw you an error specifying that the container with that container Id is not running.
Using the bash of the container
Another way to run a command inside a docker container environment is to launch the bash of that particular container and execute commands inside it. You can launch the bash of a container using the following command −
sudo docker run −it <image−name> bash
The above stated command would invoke the bash of the container associated with the specified image name in an interactive shell because of the −i flag. Inside the bash, you can execute the commands. For example, you can update an ubuntu docker using −
Or you can install a package such as −
apt−get install firefox
Using the dockerfile
This is the most common way to run or execute commands inside a docker container. Dockerfile contains a set of instructions that are executed step by step when you first build the image using the docker build command. You can run commands using the RUN instruction inside a docker file. For example, if you want to install firefox inside an ubuntu container, you can specify them in the dockerfile in the following way −
FROM ubuntu RUN apt−get −y update RUN apt−get install firefox
To conclude, it is quite evident that a docker container is no different than any linux terminal. You can execute commands in a similar manner as you do in your local system’s terminal. If you wish to run commands only once in your container at the beginning, maybe to install basic packages or libraries, you can specify them directly to your dockerfile. If you want to run a python shell or any application inside the docker container, you can easily launch the bash and execute your commands there.