Docker

How to Execute Commands in a Container

Docker is a containerized concept that builds, runs, and manages applications, software, and programs. Docker CLI is a versatile command line tool that contains numerous commands to manage these containers, like “docker run” to create and start the container, “docker ps” to list the containers, “docker inspect” to inspect the containers and many more.

Sometimes Docker users are required to access the internal environment of the Docker container to get container components, to resolve bugs, and for interaction or modifications. For this purpose, Docker allows its users to access the container’s SSH shell and execute the command inside the container.

This write-up will demonstrate how to execute commands in a container.

How to Execute Commands in a Container?

The Docker platform allows us to execute the commands within a Docker container to access internal components or files of containers by utilizing the “docker exec” command. To execute this command, start the container first. Then, run the “docker exec” command to process or run the command inside the Docker container.

For the demonstration, look at the provided instructions.

Step 1: Create DockerFile

First, make a file named “Dockerfile” and add the below-given instructions to the file. These instructions are used to containerize the “index.html” program:

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

 
Step 2: Make Docker Image

Next, generate the container template or image through “docker build <image-name>” command. The “-t” option is used to define the container image name:

docker build -t html-image .

 

Step 3: Run the Container

To execute the commands within a container, you must start the container first. For this purpose, utilize the “docker run <image-name>” command to create and start the container:

docker run --name html-container -d -p 80:80 html-image

 
In the provided command, the following options are used:

    • –name” to define the container’s name.
    • -d” to start the container in the backend.
    • -p” allocates the executing port for the container:

 

Step 4: Run Command Inside the Container

Next, to run the command within the Docker container, execute the “docker exec” command. Here, the “-it” option is utilized to execute the container interactively and assign the TTY-pseudo terminal to the container. The below command will run the container’s shell to execute the commands within the Docker container:

docker exec -it html-container sh

 
After that, execute the command inside the container as we have run the “echo” command to print the “Hello! Welcome to Linux Hint”:

echo "Hello! Welcome to Linux Hint"

 

Here, we have executed the “ulimit -n” command to view the container’s resource limit:

ulimit -n

 

That’s all about executing commands inside the container using the “docker exec” command.

Conclusion

To execute the commands inside the container, first, run the container in which you must execute the command. After that, utilize the “docker exec <container-name>” command. This command will open the container’s shell to process or run the commands within a container. This write-up has demonstrated how to execute commands inside the container in Docker.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.