Running Commands Inside Docker Container
Last Updated :
23 Jul, 2025
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 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 <container_id> with your container name or container id.
Difference Between Docker Run and CMD?
The following are the difference between docker run and cmd:
Feature | RUN | CMD |
---|
Execution Time | Docker run executes the commands during the build process | In dockerfile Docker specifies the default command to run when the container starts |
Layer Creation | It creates a new layer in the Docker image | It does not create a new layer, just sets the default execution command |
Usage | It is used for installing software packages and dependencies, configuring the environment | it is used for defining the primary command or script to run in the container |
Overriding Behavior | These commands are executed and cannot be overridden at runtime | it can be overridden by specifying a different command in docker run |
Docker Container Management Commands
Similar Reads
DevOps Tutorial DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle, from planning and coding to testing, deploying, and monitoring.The main idea of DevOps is to i
9 min read
Introduction
What is DevOps ?DevOps is a modern way of working in software development in which the development team (who writes the code and builds the software) and the operations team (which sets up, runs, and manages the software) work together as a single team.Before DevOps, the development and operations teams worked sepa
10 min read
DevOps LifecycleThe DevOps lifecycle is a structured approach that integrates development (Dev) and operations (Ops) teams to streamline software delivery. It focuses on collaboration, automation, and continuous feedback across key phases planning, coding, building, testing, releasing, deploying, operating, and mon
10 min read
The Evolution of DevOps - 3 Major Trends for FutureDevOps is a software engineering culture and practice that aims to unify software development and operations. It is an approach to software development that emphasizes collaboration, communication, and integration between software developers and IT operations. DevOps has come a long way since its in
7 min read
Version Control
Continuous Integration (CI) & Continuous Deployment (CD)
Containerization
Orchestration
Infrastructure as Code (IaC)
Monitoring and Logging
Microsoft Teams vs Slack Both Microsoft Teams and Slack are the communication channels used by organizations to communicate with their employees. Microsoft Teams was developed in 2017 whereas Slack was created in 2013. Microsoft Teams is mainly used in large organizations and is integrated with Office 365 enhancing the feat
4 min read
Security in DevOps