# Mastering Docker: Run, Manage, and Interact with Containers!

### Overview

In this guide, we’ll cover essential Docker commands and workflows to help you master container management. You’ll learn how to run your first Docker container, pull images, work interactively inside containers, and manage them effectively in different modes. Let’s dive in!

# 🚀Running Your First Container

**Command:** <mark>docker run hello-world</mark>

* **Purpose:** This command runs a Docker container using the `hello-world` image.
    
* **What Happens:** Docker looks for the `hello-world` image locally. If it doesn’t find it, Docker pulls the image from Docker Hub (a repository of Docker images). `hello-world` Docker image is a basic test image to confirm that Docker is installed correctly. It simply prints a message and then exits immediately because its purpose is only to confirm that Docker is working.
    
* **Use Case:** Useful for verifying that Docker is installed and operational on your system. This is a simple test to ensure everything is set up correctly.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724942324791/5e18bcb6-1493-428a-a0ab-396cf4866245.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724942508534/b022cd52-b1b2-474e-87e2-a54f56da7b94.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726143724883/214be2ea-1e41-49a9-8a15-9186c4ee5d96.avif align="center")

## Interacting with Running Docker Containers

### **📥 Pulling Images**

Command: <mark>docker pull ubuntu</mark>

* **Purpose:** This command pulls the `ubuntu` image from Docker Hub.
    
* **What Happens:** Docker first checks if the `ubuntu` image is available locally. If not, it downloads the image from Docker Hub, a central repository where Docker images are stored. The image is then saved in your local Docker image repository.
    
* **Use Case:** To obtain the latest version of the `ubuntu` image from Docker Hub, which can be used to create and run containers.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724944124179/bc06e3e7-a81d-45bc-a836-d821a26d2204.png align="left")

### 📋 Listing Local Images

Command: <mark>docker image ls</mark>

**Explanation:**

* **Purpose:** Lists all Docker images available on your local machine.
    
* **What Happens:** Docker displays a list of all images you have downloaded or built on your system, showing details such as repository name, tag, and image ID.
    
* **Use Case:** To check which Docker images are available locally and verify that the image you want is downloaded.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724944161727/be0cddfb-61a1-45a6-a131-c7f4ae629b2b.png align="left")

### ⚙️ Running a Container in Interactive Mode

**Command:** <mark>docker run -it --name ct1 ubuntu</mark>

* **Purpose:** Runs a new container from the `ubuntu` image in interactive mode.
    
* **What Happens:** Docker creates and starts a new container from the `ubuntu` image with the name `ct1`. The `-it` flag allows you to interact with the container’s terminal. The container’s terminal is attached to your current terminal session, letting you run commands inside it.
    
* **Use Case:** To start a new container from the `ubuntu` image and interact with it through a command-line interface.
    

### 📝 Creating Files Inside a Container

Command: <mark>touch f1 f2 f3</mark>

* **Purpose:** Creates three empty files named `f1`, `f2`, and `f3` inside the container’s filesystem.
    
* **What Happens:** Inside the container, the `touch` command creates the specified files in the current directory.
    
* **Use Case:** To create files for testing or data manipulation inside the container.
    

Command: <mark>ls</mark>

* **Purpose:** Lists files and directories in the current directory.
    
* **What Happens:** Displays the contents of the current directory inside the container, showing files like `f1`, `f2`, and `f3` if they were created.
    
* **Use Case:** To verify the presence of files and check the contents of directories within the container.
    

### ❌ Exiting and Stopping Containers

Command: **<mark>exit</mark>**

* **Purpose:** Exits the interactive terminal session and stops the container.
    
* **What Happens:** The `exit` command closes the terminal session. If the container was started with `-it` (interactive mode), this will also stop the container.
    
* **Use Case:** To end your session within the container and stop it if you no longer need it running
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724945603060/b8ae6141-252b-442c-89be-d7de0c04dfd0.png align="left")

### 📜 Listing Containers

**Command: <mark>docker ps</mark>**

**Explanation:**

* **Purpose:** Lists all running Docker containers.
    
* **What Happens:** Shows containers that are currently active and running on your system.
    
* **Use Case:** To check which containers are currently active and managing resources.
    

**Command:** <mark>docker ps -a</mark>

**Explanation:**

* **Purpose:** Lists all Docker containers, including those that are stopped or exited.
    
* **What Happens:** Displays all containers on your system, regardless of their current state (running, stopped, or exited).
    
* **Use Case:** To view the status of all containers, including those that are not currently running.
    

**Command:** <mark>docker run -it --name ct2 ubuntu</mark>

**Explanation:**

* **Purpose:** Runs a new `ubuntu` container in interactive mode with the name `ct2`.
    
* **What Happens:** Creates and starts a new container named `ct2` from the `ubuntu` image. The `-it` flag opens an interactive terminal session.
    
* **Use Case:** To start another container for different tasks or testing, with a unique name.
    

### 🔄 Re-attaching or Executing Commands Inside a Running Container

**Command:** <mark>Press </mark> `Ctrl+P+Q` **<mark>Explanation:</mark>**

* **Purpose:** Detaches from the container’s terminal session without stopping the container.
    
* **What Happens:** This key combination exits the interactive terminal session but leaves the container running in the background.
    
* **Use Case:** To leave the container’s terminal while keeping the container active.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725885832257/2e4e30c8-1839-4468-b656-8144d3c0e194.png align="center")

After detaching, if you run `docker ps`, You'll see the `ct2` container listed as running.

**Command:** <mark>docker exec -it ct2 /bin/sh</mark>

**Explanation:**

* **Purpose:** Opens a new terminal session (`/bin/sh`) inside the running container named `ct2`.
    
* **What Happens:** You get a new interactive shell session inside the `ct2` container, allowing you to run commands and interact with the container’s filesystem.
    
* **Use Case:** To execute commands or inspect the container’s environment without stopping it.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724947159801/a8d73cb4-f45f-42af-a797-e0b42c1aeee9.png align="left")

**Command:** <mark>docker attach ct2</mark>

* **Purpose:** This command attaches your terminal to the main process of the running container `ct2`.
    
* **What Happens:** Connects to the original terminal session of the container, allowing you to see output and interact with the main process if you detached earlier.
    
* **Use Case:** To return to the main terminal session of a container after detaching.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724957192681/41989e59-ab49-4202-8f06-d194b6482965.png align="left")

* **Logging**: View real-time logs and monitor container output.
    
* **Automating Scripts**: Interact with or monitor long-running scripts inside the container.
    
* **Long-Running Processes**: Connect to and track the main process in a container.
    

### 🌐 Understanding Running Modes: Foreground vs. Background

When working with Docker, you’ll often need to decide whether to run containers in the foreground (where logs are shown) or in the background (allowing multitasking). Here's a quick breakdown:

### Foreground Mode:

**Command:** `docker run httpd`

**What it does:** Runs the container in the foreground, displaying real-time logs and output directly in your terminal.

**Use Case:** When you need to see real-time logs and interact with the container immediately.

### Background Mode (Detached):

**Command:** `docker run -d httpd`

**What it does:** Runs the container in the background, allowing your terminal to be free for other commands. The container keeps running silently in the background.

**Use Case:** For long-running services like web servers or databases that don’t require active monitoring.

### ✨ Interactive Terminal Modes

* **docker run -it:** Starts a new container with an interactive terminal session. Great for when you need to run commands directly inside the container.
    
* **docker exec -it:** Opens a new shell in an already-running container. This allows you to inspect or manage containers without stopping them.
    

### 🔄 Detaching from Containers

**Command:** `Ctrl + P + Q`

**What it does:** Detaches from the container’s terminal session without stopping the container.

**Use Case:** Use this when you want to exit the container's terminal but keep the container running in the background.

### 📖 Conclusion

By the end of this guide, you’ve:

* Run your first Docker container.
    
* Pulled images, run containers in interactive mode, and created files inside them.
    
* Managed containers in both foreground and background modes.
    

Now you have the foundational skills to run, interact with, and manage Docker containers efficiently. Keep practicing and exploring new Docker functionalities to enhance your container management expertise!

Happy Dockering! 🚀
