Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to run a command inside Docker Container?
After you build a Docker image and create a running instance of it (a Docker container), you might want to execute commands inside the container to install packages, browse directories, or perform various tasks. Docker provides several methods to access the container environment and execute commands. In this article, we will explore different ways to run commands inside Docker containers.
Using Docker exec Command
The docker exec command allows you to run commands in a running container from your local machine's command line. First, you need the container ID of the target container.
Get Container Information
sudo docker ps -a
This command displays all containers with their names, IDs, and status information. Copy the container ID where you want to execute commands.
Execute Commands
sudo docker exec -it <container-id> echo "Welcome to tutorialspoint"
Important: The container must be in a running state to use docker exec. If the container is stopped, you'll receive an error.
Interactive Shell Access
sudo docker exec -it <container-id> bash
This opens an interactive bash shell inside the running container, allowing you to execute multiple commands.
Using Container Bash at Startup
You can launch a new container and immediately access its bash shell using the docker run command:
sudo docker run -it <image-name> bash
The -it flags create an interactive terminal session. Inside the bash shell, you can execute commands such as:
apt-get update apt-get install firefox
Using Dockerfile Instructions
The most common approach for running commands is through a Dockerfile during the image build process. The RUN instruction executes commands and creates new image layers.
Example Dockerfile
FROM ubuntu RUN apt-get -y update RUN apt-get install -y firefox RUN mkdir /app WORKDIR /app
When you build the image with docker build, these commands execute step-by-step, creating a customized image with your required software and configuration.
Comparison of Methods
| Method | Use Case | Container State | Persistence |
|---|---|---|---|
| docker exec | Ad-hoc commands in running containers | Must be running | Changes lost when container stops |
| docker run -it | Interactive sessions in new containers | Creates new container | Changes lost when container stops |
| Dockerfile RUN | Permanent setup during image build | Build time | Permanent in image |
Common Use Cases
Development and Debugging: Use
docker execto inspect running containers and troubleshoot issues.Package Installation: Use Dockerfile
RUNinstructions for permanent software installation.Interactive Exploration: Use
docker run -itto explore container environments interactively.
Conclusion
Docker containers function similarly to Linux terminals, allowing you to execute commands through multiple methods. Use Dockerfile instructions for permanent changes during image building, docker exec for running containers, and docker run -it for interactive sessions with new containers.
