Running Commands Inside Docker Container
If you are working on an application inside the Docker Container, you might need commands to install packages or access file system inside the Docker Container. Executing commands inside Docker Containers should be easy enough for you since you have to do it multiple times across your development phase. Docker provides you with many ways to execute commands inside the Containers.
In this article, we are going to discuss firstly what is docker and docker container and then explain the different ways to execute any type of command inside the Docker Container. At the end of the article, we're sharing the FAQs to address the common questions regarding running commands inside the docker container.
What is Docker ?
Docker is an open-source container platform tool that helps in containerizing the application. It allows the developers to package their application with all its dependencies into a single package making it as single executable bundle. It ensures consistent environments across different stages of development. It helps the developers in simplifying the development lifecycle making it easier to build, ship, and run the applications anywhere.
What is a Docker Container ?
A Docker container is a lightweight executable software package. It includes all the necessary things to run a piece of software such as application code, runtime, system tools, libraries, and settings. Docker Containers comes with isolation that is one container isolated from one another and the host system ensures to run consistently irrespective of the environment that supporting docker.
How to Run Commands Inside Docker Container ?
The following are the methods of running commands inside the docker container:
Method 1: Using Bash
You can directly access the bash of the Docker Container and execute commands there. It’s very easy to launch the bash of the Container and you can do so using this command.
sudo docker run -it ubuntu bash
The above command runs an Ubuntu Container and fires up its bash.
Once you have access to the bash, you can start executing any command there. In this example, we will perform an echo command execution.
echo geeksforgeeks
Method 2: Using the Docker exec Command
In order to run a command inside a Docker Container using the exec command, you have to know the Container Id of the Docker Container. You can get the Container Id using the following Command.
sudo docker container ls
or
sudo docker ps -a
Once you have the Container ID, you can use the Docker exec command. But you have to confirm that the Container is running before you can execute the exec command. To start the Container, use this command.
sudo docker start d64b00529582
After that, execute the exec command.
sudo docker exec -it d64b00529582 echo "GeeksforGeeks"
Method 3: By using the Dockerfile
When you are creating a large application, it is always advised that you execute your commands by specifying it inside the Dockerfile. However, you should only include those commands inside the Dockerfile which you want to execute while building the Container. For executing commands on the go, you can use any of the two methods above. To execute commands through Dockerfile, you can specify them using the Docker Run Commands.
FROM ubuntu:latest
RUN echo "geeksforgeeks"
After you have created the above Dockerfile, you can build the images using the Docker build command.
sudo docker build -t sample-image .
You can see that after step 2, “geeksforgeeks” has been printed.
Why to Run commands inside Docker Container ?
The following are the some of the benefits and reasons to run commands inside the docker container:
-
Isolation: Containers provides the isolated environment ensuring the commands run effectively without having the effect of host system or any other containers.
-
Consistency: Docker containers will encapsulate all the dependencies, libraries and configurations that it is needed for the application to make sure that all the commands to run consistency across different environments from development to production.
-
Portability: The commands executed inside the container can be easily moved and run across different systems ensuring that the environment behaves the same way everywhere, facilitating seamless deployment and scalability.
-
Security: Containers offer an additional layer of security by isolating applications from the host system, limiting the potential impact of vulnerabilities and minimizing security risks.
How to run multiple commands inside the docker container ?
We can run multiple commands inside the docker container with many different ways. Here we are showing it through docker exec command.
The following command helps you to execute multiple commands interactively within a running container using the docker exec command with sh -c:
docker exec -it <container_id> sh -c "apt-get update && apt-get install -y curl && echo 'Installation complete'"
Here, try on replacing the
Difference Between Docker Run and CMD ?
The following are the difference between docker run and cmd:
Feature | RUN | CMD |
---|---|---|
Execution Time | Executes the commands during the build process | Specifies the default command to run when the container starts |
Layer Creation | Creates a new layer in the Docker image | Does not create a new layer, just sets the default execution command |
Usage | Used for installing software packages and dependencies, configuring the environment | Used for defining the primary command or script to run in the container |
Overriding Behavior | Commands are executed and cannot be overridden at runtime | Can be overridden by specifying a different command in docker run |