DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Docker and Kubernetes Transforming Modern Deployment
  • Redefining DevOps: The Transformative Power of Containerization
  • A Concise Guide to DevSecOps and Their Importance in CI/CD Pipeline
  • Spinnaker Meets Minikube: Part 1

Trending

  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Simplifying Containerization With Docker Run Command

Simplifying Containerization With Docker Run Command

Here, you will learn how to use the docker run command to create and start a Docker container from an image with various configuration options.

By 
Ruchita Varma user avatar
Ruchita Varma
·
Mar. 30, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.7K Views

Join the DZone community and get the full member experience.

Join For Free

Docker run is a fundamental command for running applications in Docker containers. Before you start with Docker, it's important to know about some important commands. 

In this blog, we will explain the basic syntax of the Docker run command and explore some of its most common options to help you get started with running and managing Docker containers. Here, you will learn how to use the docker run command to create and start a Docker container from an image with various configuration options. 


Docker Run Command


What Is Docker Run Command?

The 'docker run' command is used to create and start a new container from a Docker image. The basic syntax of the 'docker run' command is as follows:

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

Where:

  • OPTIONS: optional flags that configure the behavior of the container, such as setting environment variables, mounting volumes, etc.
  • IMAGE: the name of the Docker image to be run.
  • COMMAND: the command to be executed when the container is started. If not provided, the default command specified in the Docker image will be used.
  • ARG: optional arguments passed to the command.

For example, to start a new container from the official Nginx image and map the container's port 80 to the host's port 8080, you would run the following command:

        docker run -p 8080:80 nginx

This command will create a new container based on the Nginx image, start it, and map port 80 inside the container to port 8080 on the host.

Examples of Docker Run Command

Here are some Docker runs command examples that you must look at:

1. Running a Simple Image

         docker run busybox echo "Hello, World!"

This command runs a new container from the 'busybox' image and executes the 'echo' command with the argument "Hello, World!".

2. Setting Environment

             docker run -e MY_VAR=value busybox env 

This command sets an environment variable 'MY_VAR' to the value "value" and runs a new container from the 'busybox' image. The 'env' command is executed to display the list of environment variables in the container.

3. Mounting a Volume

  docker run -v /host/directory:/container/directory busybox ls /container/directory

This command mounts a host directory '/host/directory' to the container directory '/container/directory' and runs a new container from the 'busybox' image. The 'ls' command is executed to list the contents of the container directory.

4. Running a Container in the Background

           docker run -d nginx

The command docker run -d nginx starts a Docker container based on the official Nginx image in detached mode. The -d flag runs the container in the background, and the nginx argument specifies the image name to use for the container. This command will pull the Nginx image from Docker Hub if it is not already present on your local machine, create a container based on that image, and start it in the background. 

5. Running a Container With a Specific Name

      docker run --name my-container busybox echo "Hello, World!"

This command runs a new container with the specified name "my-container" from the busybox image and executes the echo command with the argument "Hello, World!".

Options Available for Executing Docker Run Command

Some commonly used options for executing the docker run command inside the container:

  • -it: Runs the container in interactive mode, allowing you to interact with the container through the command line.

  • -d: Runs the container in the background.

  • --name: Specifies a name for the container.

  • --rm: Automatically removes the container when it exits.

  • -p: Maps a host port to a container port.

  • -e: Sets an environment variable inside the container.

  • -v: Mounts a host volume inside the container.

For example, to run an Ubuntu image and launch a shell inside the container, you could use the following command:

   docker run -it ubuntu /bin/bash

How to Start a Docker Container?

To start a Docker container, you can use the docker start command followed by the container ID or name. For example:
  docker start my-container

This command will start the container named "my-container." You can also use the container ID instead of the name.

If you haven't created the container yet, you can use the 'docker run' command to create and start a container in one step. For example:

     docker run -it --name my-container my-image

This command will create and start a new container named "my-container" based on the Docker image "my-image." The "-it" flag enables interactive mode so that you can interact with the container's command line. This is how you can start a Docker container.

Before you start with Docker, it's important to have a good understanding of Docker. Reading this blog will help you in getting started.

How Is the Docker Run Command Helpful?

After reading about how to start Docker, you'll now need to know how is the Docker Run Command helpful. The docker run command inside the container is one of the most important commands in the Docker CLI and is used to run Docker containers. When you run a Docker container, you are essentially running an instance of a Docker image. The docker run command allows you to specify various options and arguments to customize the behavior of the container. Here are some of the ways the docker run command can help you:

Isolation

Docker containers provide a lightweight and isolated environment for applications to run in, which makes it easier to manage and deploy applications. When you run a container with the docker run command, you can specify various options and arguments to customize the environment and behavior of the container.

Port Mapping 

The docker run command allows you to map ports from the host system to the container, which is useful for exposing applications running inside the container to the network. For example, if you have a web application running inside a container and you want to expose it to the network, you can use the -p option to map a host port to the container port.

Environment Variables 

The docker run command allows you to set environment variables inside the container. Environment variables are used to store configuration information and can be used by applications running inside the container. For example, you can set the DATABASE_URL environment variable to specify the location of a database used by your application.

Volumes

The docker run command allows you to mount host volumes inside the container, which is useful for sharing data between the host and the container. For example, you can use the -v option to mount a host directory inside the container, which allows your application to access files stored on the host.

Interactive Mode

The docker run command allows you to run a container in interactive mode, which is useful for debugging and troubleshooting. When you run a container in interactive mode with the -it option, you can interact with the container through the command line and run commands inside the container.

Overall, the docker run command is a powerful tool that allows you to run Docker containers and customize their behavior. Whether you are developing, deploying, or managing applications, the docker run command can help you get the job done.

Docker Run: Your One-Stop Solution for Container Management

In this blog, we explored how to use the Docker run command, several Docker runs command examples, how to start a Docker container, and numerous ways the docker run command can be used to simplify software development, testing, and deployment tasks, making the entire process faster, more efficient, and highly portable across different environments. 

Additionally, the docker run command can be used to manage the lifecycle of containers. You can start, stop, restart, or remove containers using the docker run command with the appropriate options.

Relation Between Kubernetes and Docker Run Command

Kubernetes uses the docker run command internally to start containers on worker nodes. When you create a Kubernetes deployment, it creates one or more replicas of your containerized application using the docker run command.

However, Kubernetes goes beyond just running individual containers. It enables you to deploy containerized applications at scale, manage their resources, provide load balancing, and perform rolling updates or rollbacks. 

While docker run is used to manage individual containers, Kubernetes is used to manage containerized applications at a higher level of abstraction, making it easier to manage and scale applications running in production.

Both effective container management and seamless Kubernetes management are essential for realizing the benefits of containerization and ensuring the success of container-based applications.

Kubernetes Software development applications Command (computing) Docker (software)

Published at DZone with permission of Ruchita Varma. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Docker and Kubernetes Transforming Modern Deployment
  • Redefining DevOps: The Transformative Power of Containerization
  • A Concise Guide to DevSecOps and Their Importance in CI/CD Pipeline
  • Spinnaker Meets Minikube: Part 1

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!