Uncategorized

How to Use Docker Run Command – A Complete Guide

Introduction

A lunch box contains everything a person needs to fill up his belly. In a very similar way, a docker container consists of all the required items to run the software. These essential items include application code, software, libraries, configuration files, and much more. 

But what if we don’t create a docker container on our system? You can choose to uninstall the docker container, but then it would become very difficult to manage different software, their dependencies, frameworks, etc. 

This is a fantastic article to learn about docker containers and docker run command. We will guide you to run commands on docker container. So, let’s move forward to our first headline, which is about searching the name of a container instance.

Finding the Name of a Docker Container

Finding the Name of a Docker Container

It is crucial to identify the name of a docker container to run specific software. It’s similar to finding the name of a person in a class of 40 students so that you can address them directly without any confusion. Without knowing the container’s name, the software cannot be executed properly. Also, every time you want to execute a command, you will need to write the name of the docker. So, let’s learn this important process before we get to know how to use the docker run command.

You must run the given command in the terminal to start looking for the container instance name.

docker ps
Finding the Name of a Docker Container

In the above output, the term “container ID” is auto-generated, and the term “image” is associated with templates of the software. The next term, which is “command,” is used to instruct the system to start a certain process when the docker container starts. Then we have the term “created,” which is the indication of when the docker was created.

As a user, you can see the list of every container with the below code, regardless of its status. 

docker ps -a

If you want to see a list of all containers and their names, use the command ‘docker ps -a’. It’s important to note that container names must be unique on the Docker host. If you’re searching for a specific container, you can use ‘docker ps -a’ to find it, whether it’s running or stopped.

Different Ways to Use the Docker Run Command

Different Ways to Use the Docker Run Command

Run a Container Under a Specific Name:

When creating a container, you can give it a custom name with the –name flag. This will help in a quick reference to the container. See the docker run command example below.

docker run --name my_container_name my_image

Run a Container in the Background:

When you start a container, it normally runs in the front of your screen. However, to make it run in the background, use the flag “-d”.

Command: docker run -d my_image

Run a Container Interactively:

When you want to interact with a container and access its shell or run a command inside it, use the -it flags. These flags let you do these actions easily. 

Command: docker run -it my_image /bin/bash

Run a Container and Publish Container Ports:

If you’re running a service in your container and want to access it from your machine, you can do so by publishing its ports to the host using the -p flag. It’s a simple and effective way to ensure easy access to all the services you need.

Command: docker run -p 8080:80 my_image

Run a Container and Mount Host Volumes:

The user can easily share and keep their data safe by using the -v flag (or –volume) to mount directories from the host into the container. This is a better approach to ensure your files are always available and secure.

Command: docker run -v /path/on/host:/container/mounted/path my_image

Run a Docker Container and Remove it Once the Process is Complete:

If you want to ensure that a container is automatically removed as soon as it exits, you can make use of the –rm flag. This will save you the hassle of manually removing the container later on.

Command: docker run --rm my_image

Also Read: How to SSH into a Docker Container and Run Commands?

Conclusion

Docker is a helpful tool for creating, deploying, and managing isolated environments. The docker run command is a crucial feature of Docker that unlocks its full potential. With this command, you can create, start, and manage containers based on Docker images easily. Understanding the basic syntax and various flags of this command can help you deploy and manage containers for your applications efficiently. Whether you are a developer, a sysadmin, or a DevOps professional, mastering this command is a crucial step in leveraging the power of containerization technology for your projects. Finally, the run command on the docker container is an amazing tool for working with Docker containers. By using it, you can efficiently deploy and manage your containerized applications.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *