Docker

What is Difference Between Docker Run and Docker Exec Command

Docker CLI provides different commands to build, deploy, and share programs and applications in containers. More specifically, Docker containers are isolated environments or components that encapsulate the programs and essential packages. These containers are built and managed through different commands, such as the “docker run” command creates and starts the container, the “docker ps” is used to list the containers, and the “docker rm” command removes the containers.

This blog will distinguish between the “docker exec” and “docker run” commands.

Difference Between “docker run” and “docker exec” Command

The “docker exec” and “docker run” commands are interconnected with each other. The “docker run” reads the instructions or commands from the Docker image and executes them to create and start the Docker container. In contrast, the “docker exec” command runs the commands inside the container. To use the “docker exec”, you must start the container first through the “docker start” or “docker run” command.

How to Use the “docker run” Command?

The “docker run” command is frequently used for building and executing the Docker container to deploy the program within a container. To use the “docker run” command, first, create the Docker image from which the “run” command will read and run the commands or instructions to containerize the application.

For this purpose, follow our linked article and make the Docker image. After that, use the “docker run” command to create and run the container through the given instructions.

Run the Container

To run the Docker image instruction to create and start the container to dockerize the program, utilize the provided command:

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

 

The above command contains the following options:

  • –name” is utilized to specify the container’s name.
  • -d” option runs the container as a backend service or in detached mode.
  • -p” assign the exposing port of the local host to the container:

How to Use Docker Exec Command in Docker?

The “docker exec” command runs the command within running containers. This command is helpful to inspect or access the internal component of the Docker container. You can also utilize it to modify the containerized program, and users can create, modify and delete files from containers as well.

To use the “docker exec” command, first, create and start the container by following the first section. Then, execute the commands within a container through the “docker exec”. This command will launch the container shell to execute the command within an executing container:

docker exec -it html-container sh

 

For illustration, we have executed the “echo” command to print “Hello! Welcome to Linux Hint”:

echo "Hello! Welcome to Linux Hint"

 

We have elaborated on the difference between “docker run” and “docker exec” commands and how these are interconnected with each other.

Conclusion

The primary difference between the “docker run” and “docker exec” command is the “docker run” command read and execute the instruction from the Docker image to create and start the containers. However, the “docker exec” command executes the additional or external commands within an executing container. This blog has demonstrated the distinction between the “docker run” and “docker exec” commands.

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.