Docker Run


The docker run command is a fundamental and commonly used command in Docker. It allows you to create and start a new container based on a Docker image. This command is used to launch and run applications or services within isolated containers. Here's the basic syntax of the docker run command:

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

  • OPTIONS: These are various flags and options you can use to customize the behavior of the container.
  • IMAGE: The name or ID of the Docker image from which to create the container.
  • COMMAND: The command to run inside the container.
  • ARG...: Additional arguments to pass to the command running inside the container.

Here are some commonly used options and examples of using the docker run command:

Basic Usage:

docker run myimage

  • This command starts a new container based on the myimage Docker image.

Interactive Mode:

docker run -it myimage /bin/bash

  • The -it flags enable interactive mode, allowing you to interact with the container's shell (/bin/bash in this example).

Detached Mode (Background):

docker run -d myimage

  • The -d flag runs the container in detached mode, which means it runs in the background, and you get control of your terminal back.

Port Mapping:

docker run -p 8080:80 myimage

  • The -p flag maps port 8080 on the host to port 80 in the container, allowing you to access a service running in the container on port 80 through port 8080 on the host.

Volume Mounting:

docker run -v /host/path:/container/path myimage

  • The -v flag mounts a volume from the host file system into the container, allowing data to be shared between the host and the container.

Environment Variables:

docker run -e MY_VARIABLE=myvalue myimage

  • The -e flag sets environment variables within the container.

Container Naming:

docker run --name mycontainer myimage

  • The --name flag allows you to specify a custom name for the container.

Removing Containers After Exit:

docker run --rm myimage

  • The --rm flag automatically removes the container when it exits.

Limiting Resources:

docker run --cpu-shares 512 --memory 512m myimage

  • You can limit CPU and memory resources using the --cpu-shares and --memory flags.

These are just some common examples of how the docker run command can be used. The options and configurations can vary depending on your specific use case and the Docker image you are running. It's a versatile command that allows you to create and manage Docker containers with flexibility and control.

Docker Run


Enroll Now

  • Docker
  • Kubernetes