Docker has become an indispensable tool for developers and system administrators alike. It allows for easy containerization of applications and services, providing a reliable and efficient way to run and manage them.
However, when it comes to working with Docker, there is often confusion between two commonly used commands: docker run and docker exec. Many people believe that these two commands do the same thing, but in reality, they have different functions and purposes.
In this article, we will explore the differences between docker run and docker exec. By understanding these differences, you can effectively work with Docker containers and ensure that your applications and services are running smoothly.
Docker Run
Docker run is the command used to create and start a new Docker container. It is used to launch a new instance of a container based on a specific image. For example, the following command will create a new container based on the nginx image:
docker run -d --name my-nginx nginx
In this command, -d specifies that the container should be run in detached mode, meaning it will run in the background. --name specifies the name of the container, and nginx is the name of the image from which the container is created. Once the command
is run, a new container will be created and started, running the nginx image.
Docker run can also be used to set environment variables, expose ports, mount volumes, and more. It is a powerful command that provides a lot of flexibility when
working with Docker containers.
Docker Exec
Docker exec is the command used to execute a command inside an already running Docker container. It allows you to run additional commands on a running container, without the need to create a new instance of the container. For example, the following command will execute the ls command inside the my-nginx container:
docker exec my-nginx ls
In this command, my-nginx is the name of the running container, and ls is the command to be executed inside the container. Once the command is run, the output of the ls command will be displayed on the console.
docker exec can also be used to start an interactive shell inside a running container, allowing you to execute multiple commands inside the container. For example, the following command will start an interactive shell inside the my-nginx container:
docker exec -it my-nginx /bin/bash
In this command, -it specifies that an interactive shell should be started, and /bin/bash is the command to be executed inside the container. Once the command is run, you will be dropped into a shell inside the running container, allowing you to execute
multiple commands.
Key Differences
The key difference between docker run and docker exec is that docker run is used to create and start a new container, while docker exec is used to execute a command inside an already running container.
Another difference is that
docker run creates a new container from an image, while docker exec works with an existing container. This means that docker run can be used to create new instances of a container, while docker exec can be used to modify or interact with an existing
instance.
Finally, docker run provides many more options and settings for creating and configuring containers, while docker exec is mainly used for executing commands inside a container.