- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to run a command inside Docker Container?
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 −
apt−get update
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.
- Related Articles
- How do I run a command on an already existing Docker container?
- How to fix Ctrl+C inside a Docker container?
- Mounting a volume inside docker container
- How to pass command line arguments to a python Docker container?
- Installing Linux Packages Inside a Docker Container
- Working with Java inside Docker Container
- From inside of a Docker container, how do I connect to the localhost of the machine
- How to find Docker container processes?
- How to get a Docker Container IP address?
- How to backup and restore a Docker Container?
- How to run Gunicorn on Docker?
- Creating a MySQL Docker Container
- How to run splash using Docker toolbox?
- How to Debug a Node.js app in a Docker Container?
- How many CPUs does a docker container use?
