How to Use Docker Run Command with Examples

how to use docker run command

The docker run command is one the most important command you should become familiar with. The docker run command is used to launch Docker containers from a specified image. It is a very useful command to build and run a container in detached mode, attached mode, interactive mode, mount a volume, set a container name and perform many tasks.

In this tutorial, we will show you how to use docker run commands with examples. We will see different methods that can be used to get started with our initial containers.

If you want to host Docker on Cloud and don’t know how to choose the best Cloud hosting provider. You can read my guide on The 8 Best Docker Hosting Platforms in 2023.

Prerequisites

  • A Linux system with Docker installed and running.
  • A root user or user account with sudo privileges.

Basic Syntax to Use the docker run Command

The basic syntax to use the docker run command is shown below:

1
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Or

1
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

To create or launch a new container, you will only need to specify an image name with the docker run command.

1
docker container run image-name

Or

1
dokcer run image-name

The above command will start a new container from the image stored in the local system. If the specified image is not available in the local system, the command pulls it from the online Docker Hub registry.

Note: Since the release of Docker 1.13, the Docker CLI has been restructured and all commands grouped under the object they interact with. The run command is now a subcommand of the docker container and you must use docker container run command to launch a container. However, Docker still supports the docker run command.

Run a Container Under a Specific Name

You can assign a name to the container when creating a container for the first time. If you use the basic run command without specifying a container name, Docker will assign a random name to the container.

It is always recommended to create a container with a generic name to recognize the containers. You can use –name flag to assign a container name as shown below:

1
docker container run --name [container-name] [docker-image]

Let’s run an Nginx container and give it the name docker-nginx as shown below:

1
docker container run --name docker-nginx nginx

Where:

  • docker-nginx is the name of the container.
  • nginx is the name of the Docker image that Docker will download from the Docker Hub registry.

You can now verify your container with assign name using the following command:

1
docker ps -a

You should see your newly created container with name “docker-nginx” in the following image:

docker assign container name

Run a Container in Attached Mode

There are several ways to run the container including, detached mode or background, attached mode or foreground and an interactive mode.

By default, Docker runs the container in attached mode. That means it’s attached to the terminal session and output of the process will be displayed on your terminal.

You can run a container without specifying any additional option will start the container in attached or foreground mode.

The following command will start the container from the Nginx image in the attached mode.

1
docker container run nginx

You should see the output of all process in the following output:

1
2
3
4
5
6
7
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

You can press CTRL + C to stop the running Nginx container.

Run a Container in Detached Mode

Running the container in detached mode is similar to running the process in the background. This will keep the container running after exiting from the terminal session.

You can use -d option to run a container in detached mode.

1
docker container run --name docker-nginx -d nginx

The above command will start the Nginx container in detached mode and run the process in the background. You can verify the running container with the following command:

1
docker ps

You should see the following output:

1
2
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0bb07d011cc9        nginx               "/docker-entrypoint.…"   7 minutes ago       Up 7 minutes        80/tcp              docker-nginx

If you want to attach your terminal to the Nginx running container, run the following command:

1
docker exec -it docker-nginx /bin/bash

Run a Container in Interactive Mode

You can also run a container in interactive mode to deal with the interactive processes. This will allows you to execute commands inside the container that is still running.

You can use the -i and -t option to start the container in interactive mode.

1
docker container run --name docker-nginx -it nginx /bin/bash

This will be attached to the container shell and the command prompt will change as shown below:

1
root@82ea93cbf662:/#

You can now able to run any command inside the container.

If the container is already running, you can attach the running container interactively using the exec option:

1
docker exec -it docker-nginx /bin/bash

The above command will get you inside an already running daemonized container and execute your required actions.

Run a Container and Publish Container Ports

If you run a container without publishing container port, you can access the process running in the container only from inside the container. You will need to publish a container port to the host machine ports in order to access the ports from the external machine.

You can use the option -p with the docker container run command to publish the port.

1
docker container run -p [host-ip]:[host-port]:[container-port]

Where :

  • host-ip is the IP address of the Docker host. This option is optional and it is not necessary to specify it.
  • host-port is the port of the Docker host.
  • container-port is the port of the container where the application is running.

For example, map the Nginx container port 80 to port 8080 on the Docker host localhost interface, run the following command:

1
docker container run --name docker-nginx -d -p 8080:80 nginx

You can now verify the port 8080 using the URL https://docker-host-ip:8080 in your web browser.

Remove the Container After Exit

When you exit from the running container, it stops but its file system still persists on the host system. In some cases, you may need to run a container for performing short-term tasks such as testing and backup. In that case, you can use the option –rm with docker run command to remove the container when it exits automatically:

1
docker container run --rm mongo

Note : This option is useful only for foreground containers.

Run a Container and Mount Host Volumes

Sometimes you may need to run a container that requires persistent storage. By default, Docker containers do not save the data. when a container is stopped and the process is completed, all data generated by the container is removed. In that case, you can use Docker volume to make the data persist and also share it across the multiple containers.

You can use the -v flag with docker run command to start the Nginx container and mount the host volume /mnt to the container volume /var/www.

1
docker container run --name docker-nginx -d -p 8080:80 -v /mnt:/var/www nginx

In the above example, any data written to /var/www within the container is actually accessing /mnt on the host. This option is useful for anyone running a database or application that requires persistence within Docker.

In simple terms, a volume mount can keep the data located on the host and allow you to sue the same volume mount to start another container.

Recommended Reading

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva