The Docker platform enables developers to build, deploy, and manage containerized applications. The docker run command is used to create and start a new container from a Docker image. It combines several functions in one command, such as creating a writable container layer over the specified image, configuring the container, and executing a specified command.

In this guide, we’ll explore the docker run command in-depth, covering its options and providing practical examples to help you get started.

Prerequisites

  • A server running Ubuntu with Docker installed.
  • A root user or a user with sudo privileges.

Basic Syntax

The basic syntax of the docker run command is:

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

Explanation:

  • OPTIONS: Various flags to modify the behavior of the container.
  • IMAGE: The name of the Docker image you want to use.
  • COMMAND: The command to run inside the container (optional).
  • ARG…: Arguments to the command (optional).

1. Running a Container Interactively

To run a container interactively, you can use the -it options. The -i flag keeps the container’s STDIN open, and the -t flag allocates a pseudo-TTY. This is useful for debugging or running shell sessions.

docker run -it ubuntu /bin/bash

This command starts an interactive bash shell in an Ubuntu container.

2. Running a Container in the Background

To run a container in the background (detached mode), use the -d option. This is useful for running services or applications.

docker run -d nginx

This command starts an Nginx web server container in the background. You can verify it using the following command:

docker ps

Output:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS     NAMES
c187638e79a3   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 9 seconds   80/tcp    priceless_bardeen

3. Exposing Ports

To access the services running inside a container, you can expose ports using the -p option.

docker run -d -p 8080:80 nginx

This command maps port 80 inside the container to port 8080 on the host machine.

4. Mounting Volumes

Volumes allow you to persist data outside the container’s filesystem. You can use the -v option to mount a volume.

docker run -d -v /mnt:/opt nginx

This command mounts the /mnt directory on the host to the /opt directory inside the container.

5. Setting Environment Variables

You can set environment variables inside a container using the -e option.

docker run -d -e "ENV_VAR=value" nginx

This command sets the ENV_VAR environment variable to a value inside the container.

6. Naming a Container

By default, Docker assigns a random name to each container. You can specify a custom name using the –name option.

docker run -d --name my-nginx nginx

This command starts an Nginx container with the name my-nginx.

7. Removing Containers Automatically

To automatically remove a container when it exits, you can use the –rm option.

docker run --rm ubuntu /bin/echo "Hello, world!"

This command removes the container after it prints “Hello, world!” and exits.

8. Passing Commands to Containers

You can pass commands to a container at runtime. This is useful for running scripts or commands inside the container.

docker run ubuntu /bin/echo "Hello, Docker!"

This command runs the echo command inside an Ubuntu container.

9. Limiting Resource Usage

To limit the CPU and memory usage of a container, you can use the –cpus and –memory options.

docker run -d --cpus="1.0" --memory="512m" nginx

This command limits the container to use at most one CPU core and 512 MB of memory.

Conclusion

The docker run command is versatile and essential for managing Docker containers. Understanding its various options and flags is crucial for effectively deploying and managing containers. The examples provided here are just a starting point; the command can be customized further based on specific needs and use cases. You can now manage and run a Docker container on VPS hosting from Atlantic.Net!