How to Use Docker Run Command with Examples – phoenixNAP

introduction

One of the

first and most important commands that Docker users learn is the docker run command. This is not a surprise since its main function is to build and run containers.

There are many different ways to run a container. By adding attributes to the basic syntax, you can configure a container to run in separate mode, set a container name, mount a volume, and perform many more tasks.

In this tutorial, you’ll learn how to use docker run commands with examples.

Prerequisites

  • Accessing a command-line/terminal window
  • A

  • user account with sudo privileges An
  • existing Docker installation

How to use

the docker run command

The basic syntax of the command is:

docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

To run a container, the only thing you need to include in the command is the image it’s based on:

docker run [docker_image]

You can run containers from locally stored Docker images. If you use an image that is not on your system, the software extracts it from the online registry.

As an example, we use a Dockerfile to create a sample Docker image with the task of echoing the Hello World message. For us, the image has the ID e98b6ec72f51. The name of the image will vary depending on the container you want to run.

The command

to run our example container would be:

docker run e98b6ec72f51

The container performs the prescribed task (echoes the Hello World message) and then stops

. Run a container with a specific name

When using the run basic command, Docker automatically generates a container name with a string of randomly selected numbers and letters. Since there’s a

small chance you can remember or recognize containers by these generic names, consider setting the container name to something more memorable.

Using the -name attribute Allows you to assign a container name. The command to

run a container with a specific name is: docker container run -name [container_name] [docker_image] For example, we can run the example container and give it the container_instance name using the command: docker container

run

-name -name container_instance e98b6ec72f51

You can check if you have successfully set a container name by displaying a list of all containers (running and stopped) with the command:

Docker PS -a

As in the image below, you should see your newly created container

. Run a container in the background (separate mode)

There are two ways to run a container: in attached mode (foreground) or in separate mode (background).

By default, Docker runs the

container in attachment mode

. Which means that it is connected to the terminal session, where it displays the output and messages.

If you want to keep the container and the current terminal session separate, you can run the container in the background by using the -d attribute. Using the separate mode also allows you to close the open terminal session without stopping the container.

The command to run a container in the background is: docker container run -d [docker_image]

For our example, the command is:

docker

container run

-d e98b6ec72f51

The output you will receive will be similar to the one you see in the image above. The container will execute the process and then stop. No other output will be displayed within the terminal session.

Running a container

interactively

Docker allows you to run a container in interactive mode. This means that you can execute commands inside the container while it is still running.

By interactively using the container, you can access a command prompt within the running container. To do this

, run the following command: docker container run -it [docker_image] /bin/bash

The command prompt will change, moving it to the bash shell as in the following example

. Run a container

and publish container ports

When you run a container, the only way to access the process is from within it. To allow external connections to the container, you must open (publish) specific ports.

You must add the -p option to the docker run command, as well as the following information: -p [host_ip]:[host_port]:[container_port] The

host_ip element is optional and does not need to be specified when you run the command.

For example, to map container TCP port 80 to port 8080 on the Docker host, you would run:

docker container run -p

8080:80

[docker_image]

Run a container and mount host volumes The

Docker containers do not save the data they produce. As soon as the process is finished, the container stops and everything inside is removed.

If you want to have persistent data that is stored even after the container is stopped, you must enable storage volume sharing.

To mount volumes, use the -v attribute with the specified location of the directory where you want to save the data, followed by where that data will be located within the container.

-v [/host/volume/location]:[/container/storage]

The full docker container run command is: docker container run

-v [/host/volume/location]:[/container/storage] [docker_image] Run a

Docker container and delete it Once the process is complete Once a container

executes

its tasks, it stops, but the file system it consists of remains on the system.

If you only need one container to run the described task and have no use of it or your file system afterwards, you can set it to be deleted once you’re done.

To run a container

that will be automatically deleted after exiting, use the command: docker container run

-rm [docker_image]

Conclusion

Docker has earned a prominent place in development trends. The sooner you become familiar with its command-line interface, the better. The first step is to master the docker run command.

You can use this guide to learn the basics or as a helpful reminder when needed.