Docker has become the containerization platform of choice for many open source and proprietary projects. The platform simplifies code deployment and distribution, and developers can write code without worrying about the target environment(s).
At the heart of a Docker installation lies the docker run command for initiating and running a container.
In this blog post, we will discuss six scenarios where you can use the docker run command to control container startup behavior and affect container management. So, whether you’re a seasoned Docker user who knows your way around the platform or a newcomer who wants to optimize their Docker experience, this guide is for you.
Let’s start with a short overview of the command.
Table Of Contents
- What Does the docker run Command Do?
- Six Typical docker run Scenarios
- The Basic docker run Command Syntax
- Scenario #1: Run a Container With a Specified Name
- Scenario #2: Run a Docker Container in the Background
- Scenario #3: Run a Container in the Interactive Mode
- Scenario #4: Run a Container With Exposed Ports
- Scenario #5: Run a Container With Attached Storage Volume(s)
- Scenario #6: Terminate a Docker Container Upon Process Completion
What Does the docker run Command Do?
The docker run command is the primary way of creating and running containers using Docker images. It does a few key things: it kicks off a new container, runs a command inside it, and fetches the necessary image if required.
Under the hood, the docker run offers several crucial functionalities, such as handling container images, managing individual containers, implementing default options, and offering interactive processes. During a typical execution process, the command fetches hosts file configurations and resources, making it an indispensable tool in the Docker toolkit.
Six Typical docker run Scenarios
The best way to understand the capabilities of the Docker run command is to see it in action. Let’s first see the prerequisites and then go into the details of the scenarios.
Here are the prerequisites for running the docker run command:
- An active Docker on your machine.
- Terminal or command line access.
The Basic docker run Command Syntax
The docker run command in Docker has the following basic syntax:
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Usually, you specify an image when using the docker run command to run a container:
$ docker run [docker_image]
The command initially searches for the image on the local system. If Docker can’t find it, it automatically fetches it from the online registry.
For instance, we created a sample Docker image that prints Hello World as output. We have named this image hello-world.
We’ll use the following command to launch the test container:
$ docker run hello-world
The docker run command initiates the container with the hello-world image. The container runs the image, echos the message Hello World, and eventually halts.
The docker run command is very flexible, and you can use its various options to fine tune container launch behavior. Let’s see six scenarios where the docker run command helps you optimize container initialization and launch.
Scenario #1: Run a Container With a Specified Name
When you use the docker run command, the platform produces a container name made up of a random string of letters and digits. Users have a hard time remembering these random strings and identifying a container by these random names.
Docker allows you to assign a more recognizable name to containers to address this issue. You can give your container a unique name with the ‘–name’ flag.
Here’s the syntax command you can use to launch a container with a specified name:
$ docker container run --name [container_name] [docker_image]
For instance, use the following command to give the test container name a proper name.
$ docker container run --name 4c1296a922e6 hell-world
You can verify that the container has been assigned a name by listing down all available active and stopped containers with the following command:
$ docker ps -a
Scenario #2: Run a Docker Container in the Background
Docker offers two primary modes for running a container:
Attached mode, where the container runs in the foreground
Detached mode, where the container operates in the background.
By default, Docker initiates containers in the attached mode, where it’s connected to the terminal session, allowing you to see the output and messages.
If you wish to separate the container and your current terminal session, you can run the container in the background by employing the -d flag. Running in detached mode lets you close the active terminal session without affecting the container’s operation.
The syntax of the command for running a container in the background is:
$ docker container run -d [docker_image]
The container will execute its process and subsequently terminate. There won’t be any additional output in the current terminal session.
Scenario #3: Run a Container in the Interactive Mode
Docker offers an interactive mode for containers where you can execute commands within an active container. This interactive mode comes with a command prompt that you can use for command execution.
The syntax of the command is:
$ docker container run -it [docker_image]
docker exec is another frequently used command to execute commands in active containers. Here’s our guide to docker exec usage. Once you finish the tutorial, check out our detailed coverage of Docker Swarm for building high availability clusters.
Scenario #4: Run a Container With Exposed Ports
When initiating a container, Docker limits access to its processes to within the container itself. If you wish to allow external processes and services to connect to the container and access its processes and data, you need to expose (also known as publishing) specific ports.
You can use the -p flag with the docker run command to expose the container ports. For instance, the following command exposes the port 8080 of the container and forwards it to the port 80 of the local machine:
$ docker run -p 8080:80 [docker_image]
Scenario #5: Run a Container With Attached Storage Volume(s)
Docker containers don’t retain the data they generate. As a result, when the container’s process terminates, the container is halted and all its contents erased. You need to use a shared storage volume to retain the data after container termination.
You can mount an external storage volume (a directory in most instances) with the -v flag with the source directory location on your host machine where you wish to store data, followed by the corresponding location inside the container.
For instance, the following command mounts the local /home directory with the container:
$ docker run -v /home/:/home/docker/ [docker_image]
Scenario #6: Terminate a Docker Container Upon Process Completion
Usually, once a container finishes, it stops but retains its resources. If there are too many containers in this STOP state, you will lose precious system resources.
A good practice is to stop a container when execution finishes and delete it to release the resources. Docker allows you to configure containers to automatically get deleted upon completion by adding the –rm flag to the docker run command.
Here’s the syntax of the command:
$ docker run --rm [docker_image]
Understanding and leveraging the power of the docker run command is essential for optimizing your Docker containerization experience. This command offers a range of options that optimize container operations and the overall efficiency of the Docker platform.
RedSwitches stands out for its robust infrastructure, tailor-made to ensure your Dockerized applications run smoothly and reliably. Their dedicated hosting services are finely tuned to meet Docker’s unique requirements, offering a dependable foundation for container operations.
So, if you’re looking for a robust server for your Ubuntu projects, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Q. How do I use the docker run command?
To use the docker run command, you can run the following command: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]. Replace [OPTIONS] with the desired configurations, [IMAGE] with the Docker image you want to create a container from, and [COMMAND] [ARG…] with the command and arguments you want to run inside the container.
Q. Can you provide an example of using the docker run command?
To run a new container based on the ubuntu image in interactive mode and execute the bash command inside the container, use the following command: docker run -it ubuntu bash.
Q. How do I run a container in detached mode?
To run a container in detached mode, you can use the -d flag with the docker run command. For example, docker run -d ubuntu sleep infinity will run a new container based on the ubuntu image and keep it running indefinitely in the background.
Q. How can I remove a container?
You can remove a container with the docker rm command followed by the container ID or name. The syntax is: docker rm <container id>.
Q. How do I specify a port for a container?
To specify a port for a container, you can use the -p flag followed by the host port and container port. For example, docker run -p 8080:80 nginx will run a new container based on the nginx image and map port 8080 on the host to port 80 in the container.
Q. How do I interact with a running container?
To interact with a running container, you can use the docker exec command followed by the container ID or name and the command you want to run inside the container. For example, docker exec -it bash will start an interactive session inside the specified container.
Q. How do I run a command inside a container?
You can run a command inside a container with the docker exec command followed by the container ID or name and the command you want to run. For example, docker exec ls will list the files and directories inside the specified container.
Q. How do I keep a container running?
By default, a container will stop running once the command or process inside it completes. You can specify a long-running command or process to keep a container running. For example, docker run -d ubuntu tail -f /dev/null will start a new container based on the ubuntu image and keep it running indefinitely by using the tail command with the -f /dev/null argument.
Q. What are the differences between docker run and docker create?
The docker run command creates and starts a new container in one command, while the docker create command only creates a new container without starting it. You can later start the container using the docker start command.