Skip to content
Ercan Ermis
Ercan Ermis

notes for everyone about cloud technology

  • Cloud
    • AWS
    • GCP
  • Container
    • Kubernetes
    • Docker
  • Linux
  • DevOps
  • Privacy Policy
  • Contact
Ercan Ermis

notes for everyone about cloud technology

Mastering Docker Run Command: A Comprehensive Guide

Ercan, July 29, 2023July 29, 2023

The use of Docker in contemporary software development is widespread, and for good reason. Docker helps developers build lightweight and portable software containers that simplify application deployment. One of the most powerful commands in Docker’s command-line interface (CLI) is the docker run command. However, its vast number of options can make it a bit daunting for beginners.

In this blog post, we will demystify the docker run command and explain how to leverage its capabilities to run Docker containers effectively. Let’s get started!

The Docker Run Command

At its most basic, the docker run command creates a new container from a Docker image and starts it. However, this command offers a multitude of options to customize your containers according to your application’s needs. Here’s how the command structure looks:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

Now, let’s delve deeper into some of the most commonly used options:

Name (--name)

Assigning a custom name to your Docker container can be incredibly useful, especially when you’re dealing with multiple containers. You can specify a name for your Docker container using the --name flag:

docker run -d --name my_container ubuntu

Port Mapping (-p or --publish)

Docker allows you to map a network port inside your Docker container to a port on your host machine using the -p or --publish flag. This capability is essential when deploying services that need to be accessible over a network:

docker run -d -p 8080:80 ubuntu

Volume Mounting (-v or --volume)

To persist data generated and used by your Docker containers or to provide data from your host to the container, Docker provides volumes. You can mount a volume using the -v or --volume flag:

docker run -d -v /path/on/host:/path/in/container ubuntu

Environment Variables (-e or --env)

You can set environment variables that the process inside your Docker container can use. These variables are a good way to provide configuration settings to your application:

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

Link (--link)

The --link option allows containers to discover each other and securely transfer information about one container to another container.

docker run -d --link database:db ubuntu

Memory (-m or --memory)

Limiting a container’s memory usage can be critical when managing resources on your Docker host. The -m or --memory flag allows you to limit a container’s memory usage:

docker run -d -m 512m ubuntu

Network (--net)

The --net option allows you to specify the networking mode for the container. For instance, using --net=host makes the container use the host’s network stack:

docker run -d --net=host ubuntu

Restart (--restart)

The --restart flag allows you to specify a restart policy for how Docker should handle container exits. The always option ensures the container is always restarted, maintaining high availability:

docker run -d --restart=always ubuntu

User (--user or -u)

The --user option allows you to specify the user that runs the command in the container. This can be useful to ensure correct file permissions or to enhance the container’s security:

docker run -d --user=username ubuntu

Working Directory (-w or --workdir)

The -w or --workdir option sets the working directory inside the container for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile:

docker run -d -w /path/to/dir/ ubuntu

Now, let’s see an example of a docker run command that includes multiple options:

docker run -d \
--name my_container \
-p 8080:80 \
-v /path/on/host:/path/in/container \
-e "ENV_VAR=value" \
--link database:db \
-m 512m \
--net=host \
--restart=always \
--user=username \
-w /path/to/dir/ \
ubuntu

This command chain gives you a powerful, flexible way to configure and manage Docker containers to suit various needs. Remember, not all options need to be used together in all cases; adjust the command to fit your specific requirements.

Wrapping Up

In conclusion, the docker run command is a versatile tool that empowers developers to efficiently manage and control Docker containers. By understanding and effectively using these options, you can significantly enhance your Docker workflow and productivity.

This guide just scratches the surface of Docker’s capabilities. I encourage you to explore the Docker documentation and experiment with these options to get a feel for what’s possible with Docker.

Whether you’re a developer, DevOps professional, or IT administrator, mastering the docker run command is an essential skill that will surely serve you well in your Docker journey.

Happy Dockering!

Share on Social Media
x facebook linkedin reddit
Docker docker

Post navigation

Previous post
Next post
  • AWS (45)
    • Serverless (4)
  • Best (9)
  • DevOps (16)
  • Docker (10)
  • GCP (3)
  • Kubernetes (3)
  • Linux (13)
  • Uncategorized (6)

Recent Posts

  • Automating AWS CloudWatch Log Group Tagging with Python and Boto3
  • Automating AWS ECR Tagging with Python and Boto3
  • Automating ECR Image Cleanup with Bash
  • Update ECR Repositories with Bash Script
  • Why Automated Tests Are Essential in Your CI/CD Pipeline and Development Flow
  • Streamline Your AWS ECR Management with This Powerful Bash Script
  • Setting up DKIM for Google Workspace (Gmail) using Terraform and AWS Route 53
  • Automate AWS Site-to-Site VPN Monitoring
  • Optimizing Docker Images: Tips for Reducing Image Size and Build Time
  • Monitoring EC2 Disk Space with a Simple Bash Script and Slack Alerts
  • Securing Docker Containers: Best Practices for Container Security
  • Mastering Dockerfile: Writing Efficient, Scalable Container Builds
  • Migrating a Git Repository from GitLab to GitHub with GPG-Signed Commits
  • Accessing AWS Services in Private Subnets Without 0.0.0.0/0
  • Understanding AWS Regions, Availability Zones, and VPCs: A Comprehensive Guide
©2025 Ercan Ermis | WordPress Theme by SuperbThemes