---
title: "Up your DevOps Game With These Docker Run Example"
description: "Learn more ways to use docker run commands for efficient application deployment and management with docker run examples in this tutorial!"
canonical: "https://adamtheautomator.com/docker-run-example/"
---

# Up your DevOps Game With These Docker Run Example

> Learn more ways to use docker run commands for efficient application deployment and management with docker run examples in this tutorial!

Source: https://adamtheautomator.com/docker-run-example/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Up your DevOps Game With These Docker Run Examples](https://adamtheautomator.com/wp-content/uploads/2022/04/Up-your-DevOps-Game-With-These-Docker-Run-Examples.jpg)

# Up your DevOps Game With These Docker Run Examples

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)12 April 20229 min. read

Categories: [DevOps](/category/devops/)

Tags:[Command Line](/tag/command-line/)[Docker](/tag/docker/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Docker Run Example # 1: Running a Container Interactively](#docker-run-example--1-running-a-container-interactively)
*   [Docker Run Example # 2: Running the Container in Detached Mode](#docker-run-example--2-running-the-container-in-detached-mode)
*   [Creating a Bridge Network](#creating-a-bridge-network)
*   [Docker Run Example # 3: Running a Container with Port Forwarding](#docker-run-example--3-running-a-container-with-port-forwarding)
*   [Creating a User-defined Network](#creating-a-user-defined-network)
*   [Conclusion](#conclusion)

The `docker run` command is one of the first commands you should learn after installing Docker. But have you wondered how else you can use `docker run` commands to deploy and manage your applications effectively? Going through one Docker run example (`docker run)` would be great. But guess what, you’ll get more on this tutorial!

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

In this article, you’ll learn how to up your DevOps game with some Docker run example that you can use to improve your workflow.

Read on and never be lost when running Docker commands again!

## **Prerequisites**

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following.

*   A Linux machine – This demo uses Ubuntu 20.04, but any Linux distribution will work. Here’s [how to install Ubuntu](https://adamtheautomator.com/install-ubuntu/).

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   Docker installed on the machine.

Related:[How to Install and Use Docker on Ubuntu (In the Real World)](https://adamtheautomator.com/docker-ubuntu/)

*   A user account with root/**[sudo privileges](https://www.liquidweb.com/kb/how-to-set-up-and-manage-sudo-permissions/).**

## Docker Run Example # 1: Running a Container Interactively

You may have been running containers for some time now, but did you know there are a few preferable situations to run a Docker container in interactive mode?

Docker lets you run containers in interactive mode when:

*   You’re troubleshooting a problem and need to see the output of the commands executed inside the container.
*   You’re experimenting with a new Docker command and want to see the command results in real-time.
*   You’re running a process in the container that requires manual input from you.

When executing a Docker run example ( [`docker run`](https://docs.docker.com/engine/reference/commandline/run/) ) command with no options, the default behavior is to run the container in interactive mode. When you run a container in interactive mode, you can see the output of the commands that you execute in the container on your terminal as they are executed.

1\. Run the following command to run a container (`my_nginx`) in interactive mode and publish (`-p`) port `80` of the container to the port `8080` of your host machine. You can change the name of the container as you prefer.

The command uses the `nginx` image from the Docker hub to create the container and automatically removes (`--rm`) the container when the container is stopped.

```bash
docker container run --rm --name my_nginx -p 8080:80 nginx
```

> _Since you’re using a non-root user with_ [_sudo privileges_](https://www.liquidweb.com/kb/how-to-set-up-and-manage-sudo-permissions/)_, there’s no need to prepend sudo when running Docker commands_

Related:[Troubleshooting Docker Permission Denied Problems](https://adamtheautomator.com/docker-permission-denied/)

You’ll see the log output of the nginx web server in your terminal directly, as shown below.

![Running a Container (my\_nginx) in Interactive Mode](https://adamtheautomator.com/wp-content/uploads/2022/04/image-122.png)

Running a Container (my\_nginx) in Interactive Mode

Running an interactive container provides a lot of information, but perhaps you prefer to filter out information you only need. If so, append the -a flag and specify a [standard stream](https://docs.docker.com/engine/reference/commandline/run/#options) (-a) for the container, as shown below.

Specifying a standard stream tells Docker to display only the stdout output of the my\_nginx container.

```bash
docker container run -a stdout --rm --name my_nginx -p 8080:80 nginx
```

By default, if you don’t provide the -a flag, stdin, stdout, and stderr are all attached to the terminal. The -a flag takes the following arguments and helps declutter your output to see only the relevant information:

*   `-a stdin` – attaches `stdin` to the container, sending all inputs on your terminal to the container.
    
*   `-a stdout` – attaches `stdout` to the container, displaying all output generated by the container to your terminal.
    
*   `-a stderr` – attaches `stderr` to the container, displaying all error messages generated by the container to your terminal.
    

You can see below that the output is less cluttered now.

![Viewing the stderr Output](https://adamtheautomator.com/wp-content/uploads/2022/04/image-123.png)

Viewing the stderr Output

2\. Next, open another terminal and run the [`docker ps`](https://docs.docker.com/engine/reference/commandline/ps/) command to see the list of containers running on your host machine.

```bash
docker ps
```

Below, you can see your my\_nginx container listed in the output.

![Listing Available Containers](https://adamtheautomator.com/wp-content/uploads/2022/04/image-124.png)

Listing Available Containers

3\. Finally, run the `curl` command below to test the nginx web server.

```bash
curl http://localhost:8080
```

You can see below that the log output of the nginx web server is displayed in your terminal (left-hand side) as soon as the output generates.

![Testing the nginx Web Server](https://adamtheautomator.com/wp-content/uploads/2022/04/image.gif)

Testing the nginx Web Server

## Docker Run Example # 2: **Running the Container in Detached Mode**

You’ve just learned to run your containers in interactive mode and see the output in your terminal as they execute. But if you’re not eyeing for outputs of the container in your terminal, you can run the container in detached mode.

In detached mode, the container runs in the background, and you don’t see the output of the commands you execute in the container in your terminal.

Run the following command to run a container (`my_nginx`) in detached mode (`-d`).

```docker
docker container run --rm --name my_nginx -d -p 8080:80 nginx
```

Once the container is running in detached mode, Docker redirects the output to the container’s logs. At this point, the my\_nginx container is running in the background command outputs, as shown below.

You can also see the unique container ID in the output. You can use this ID to attach the container or start/stop/remove the container.

![Running the my\_nginx Container in Detached Mode](https://adamtheautomator.com/wp-content/uploads/2022/04/image-125.png)

Running the my\_nginx Container in Detached Mode

Now, run the `docker ps` command to see the list of containers running on your host machine.

> _Since the container (my\_nginx) is running in the background, you can run any commands without opening a new terminal._

```bash
docker ps
```

You’ll see the following output, which shows that the **my\_nginx** container is now running in detached mode.

![Listing Running Containers](https://adamtheautomator.com/wp-content/uploads/2022/04/image-126.png)

Listing Running Containers

If for some reason, your NGINX webservers aren’t working correctly while your container is running in detached mode, you’ll have to attach your terminal to the running container. You can attach to a running container by running the [`docker attach`](https://docs.docker.com/engine/reference/commandline/attach/) command followed by the container’s ID or name (`my_nginx`), as shown below.

```docker
docker container attach my_nginx
```

As you see below, you’ve successfully attached to the **my\_nginx** container. Now, you can find out the root cause of the issue and fix it.

![Attaching your Terminal to a Container (my\_nginx)](https://adamtheautomator.com/wp-content/uploads/2022/04/image-127.png)

Attaching your Terminal to a Container (my\_nginx)

## **Creating a Bridge Network**

One of the advantages of using Docker is that it manages your containers networking for you. With Docker, you don’t have to worry about configuring your network. Docker sets up the networking for you automatically. Including creating networks, connecting containers to networks, and managing IP addresses.

See how Docker sets up networking for your containers and creates a bridge network.

1\. Run the `docker` command below to run an Ubuntu container with full access to the networking resources on the host machine (`--net=host`), which is handy for debugging.

The -ti options tell Docker to keep the container in interactive mode. You must specify this option when you are working with Bash shell. Running this command as in the “Running a Container Interactively” section does not work.

```bash
docker run -ti --rm --net=host ubuntu bash
```

![Running an Ubuntu Container](https://adamtheautomator.com/wp-content/uploads/2022/04/image-128.png)

Running an Ubuntu Container

2\. Next, run the `apt update` command in the container to update the package list.

```bash
apt update -y
```

![Updating the Package List](https://adamtheautomator.com/wp-content/uploads/2022/04/image-129.png)

Updating the Package List

3\. Run the below command to `install` the `bridge-utils` package. This package is needed to create and manage bridge devices.

```bash
apt install bridge-utils
```

![installing the bridge-utils package](https://adamtheautomator.com/wp-content/uploads/2022/04/image-130.png)

installing the bridge-utils package

4\. Now, run the following [`brctl show`](https://man7.org/linux/man-pages/man8/brctl.8.html) command to list the bridge networks on your system.

```bash
brctl show
```

You’ll see a list of bridge networks similar to the following output. Notice that there is a bridge device named docker0. This network is the virtual network that Docker uses to create virtual networks for your containers if you do not specify a custom bridge virtual network.

![Showing Bridge Network ](https://adamtheautomator.com/wp-content/uploads/2022/04/image-131.png)

Showing Bridge Network

5\. Open another terminal and run the [`docker network`](https://docs.docker.com/engine/reference/commandline/network/) command below to create a new network. You can name the network differently, but in this tutorial, the network is named `my-ata-network`.

```bash
docker network create my-ata-network
```

You’ll see a unique network ID returned, as shown below. Note down the network ID as you’ll match it with the new bridge network ID later (step six).

![](https://adamtheautomator.com/wp-content/uploads/2022/04/image-132.png)

Creating a New Network (my-ata-network)

6\. Return to your Ubuntu container terminal, and run the following command to list the bridge network (`show`) on your system again.

```bash
brctl show
```

As shown below, you’ll see a new bridge network listed in the output named br-d9ba7f94ac73. You can see the d9ba7f94ac73 part matches the network ID returned in step five.

This output confirms that Docker created a new bridge network named my-ata-network and added the Ubuntu container to that bridge network.

![Listing the Bridge Networks](https://adamtheautomator.com/wp-content/uploads/2022/04/image-133.png)

Listing the Bridge Networks

## Docker Run Example # 3: Running a Container with Port Forwarding

Docker uses the built-in firewall features of the Linux kernel, namely the `iptables` command, to create firewall rules. These firewall rules control when packets get sent between the bridges and thus become available to the containers attached to those bridges.

Take a look at how Docker accomplishes port forwarding under the hood with `iptables`.

1\. Run the following command to start a privileged Ubuntu container (`--privileged=true`) with full access to all networking resources on the host, including port forwarding.

```bash
docker run -ti --rm --net=host --privileged=true ubuntu bash
```

2\. Next, run the below command to `update` the package list and `install` `iptables`.

```bash
apt update && apt install iptables
```

![Installing iptables](https://adamtheautomator.com/wp-content/uploads/2022/04/image-134.png)

Installing iptables

3\. Run the `iptables` command below on the privileged Ubuntu container to list (`-L`) the `nat` table rule.

```bash
iptables -n -L -t nat
```

What you can see below are just the default rules shipped with Ubuntu.

![Listing the nat Table Rules](https://adamtheautomator.com/wp-content/uploads/2022/04/image-135.png)

Listing the nat Table Rules

4\. Now, execute the below command run an `Ubuntu` container with port forwarding to your host on port `8080`.

```bash
docker run -ti --rm -p 8080:8080 ubuntu bash
```

![Running an Ubuntu Container with Port Forwarding](https://adamtheautomator.com/wp-content/uploads/2022/04/image-136.png)

Running an Ubuntu Container with Port Forwarding

5\. Finally, rerun the `iptables` command below to list the `nat` table rule.

```docker
iptables -n -L -t nat
```

Notice below that a new rule is added, forwarding all packets from the host’s port (8080) to the container’s port (8080). You can see that exposing ports in Docker is merely port forwarding at the network layer.

![Listing nat Table Rules](https://adamtheautomator.com/wp-content/uploads/2022/04/image-137.png)

Listing nat Table Rules

## Creating a User-defined Network

Your Docker DCA exam may ask you to create bridge networks for developers. How? Using the `docker network` command, you can create a bridge network (a User-defined virtual network). The `docker network` provides a way for containers on different networks to communicate.

1\. Run the `docker network` command below to create a new bridge network named `ata-app-net` using the `bridge` driver.

```bash
docker network create --driver bridge ata-app-net
```

![Creating a New Bridge Network (ata-app-net)](https://adamtheautomator.com/wp-content/uploads/2022/04/image-138.png)

Creating a New Bridge Network (ata-app-net)

2\. Next, run the following command to list (`ls`) the bridge networks on your host.

```bash
docker network ls
```

Notice below that a new user-defined bridge network named ata-app-net has been created.

![Listing the Host’s Bridge Networks](https://adamtheautomator.com/wp-content/uploads/2022/04/image-139.png)

Listing the Host’s Bridge Networks

3\. Run the below command to view (`inspect`) the details of the `ata-app-net` bridge network.

```bash
docker network inspect ata-app-net
```

Below, notice that the ata-app-net bridge network has a single subnet (172.21.0.0/16). This subnet is automatically assigned when you create a bridge network. You can also see the driver (bridge) associated with the bridge network.

![Viewing the Details of the ata-app-net Bridge Network](https://adamtheautomator.com/wp-content/uploads/2022/04/image-140.png)

Viewing the Details of the ata-app-net Bridge Network

4\. Now, execute this Docker run example ( `docker run` ) command below to run a couple of alpine containers on your user-defined bridge networks to try to ping each other.

```bash
docker run -dit --name ata-app1 --network ata-app-net alpine ash
```

![Running a Couple of Alpine Containers](https://adamtheautomator.com/wp-content/uploads/2022/04/image-141.png)

Running a Couple of Alpine Containers

5\. Run the `docker ps` command to list the running containers.

```bash
docker ps
```

As you see below, both containers are now running.

![Listing the Running Containers](https://adamtheautomator.com/wp-content/uploads/2022/04/image-142.png)

Listing the Running Containers

6\. Rerun the docker network command below to `inspect` your `ata-app-net` bridge network.

```bash
docker network inspect ata-app-net
```

You can see below that your two containers are now connected to the ata-app-net bridge network along with their IP addresses and names.

![Inspecting your ata-app-net Bridge Network](https://adamtheautomator.com/wp-content/uploads/2022/04/image-143.png)

Inspecting your ata-app-net Bridge Network

7\. Now, run the `docker container attach` below to connect to your `ata-app1` container.

```bash
docker container attach ata-app1
```

8\. Ping to `ata-app2` to verify that the containers can communicate with each other.

```bash
ping ata-app2
```

![Pinging to ata-app2](https://adamtheautomator.com/wp-content/uploads/2022/04/image-1.gif)

Pinging to ata-app2

9\. Next, ping `google.com` to verify that **ata-app1** can communicate with the internet.

```bash
ping google.com
```

![Pinging to google.com to Verify that ata-app1 Can Communicate with the Internet.](https://adamtheautomator.com/wp-content/uploads/2022/04/image-2.gif)

Pinging to [google.com](http://google.com/) to Verify that ata-app1 Can Communicate with the Internet.

10\. Finally, repeat steps seven to nine to connect to `ata-app2` and ping `ata-app1`.

Be sure to press CTRL + PQ to detach from the ata-app1 container and keep it running. Or else, the container will stop, and when you ping ata-app1 from ata-app2, the ping command will fail.

```bash
docker container attach ata-app2
ping ata-app1
ping google.com
```

Below, you can see all packets are transmitted and received, which confirms both containers can communicate with each other and the internet.

![Pinging ata-app1 and google](https://adamtheautomator.com/wp-content/uploads/2022/04/image-3.gif)

Pinging ata-app1 and google

## **Conclusion**

In this tutorial, you’ve learned many Docker run example ( `docker run` )you can use to ease up deploying and managing your applications. You can now use Docker more effectively by understanding how `docker run`s and how Docker sets up networking for your containers.

At this point, you already know how to take advantage of Docker run example ( `docker run` ) commands to up your DevOps game.

With this knowledge, why not deploy your applications more [efficiently and in a scalable](https://www.ibm.com/support/pages/scaling-docker-bridge-network) way?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fdocker-run-example%2F&text=Up%20your%20DevOps%20Game%20With%20These%20Docker%20Run%20Examples)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fdocker-run-example%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fdocker-run-example%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/02/featured_image-13.webp)

### [Azure Container Apps: Instant Preview Environments per PR](/build-ephemeral-preview-environments-every-pr/)

Build isolated preview environments for every pull request using Azure Container Apps revision labels and Azure DevOps pipelines with scale-to-zero economics.

![](https://adamtheautomator.com/wp-content/uploads/2025/11/55418e927e511ae263219c072e27d637c2a967a5036de7020b329582db775c26.png)

### [Automating Docker Container Health Checks with Python and Local Notifications](/docker-health-checks-python/)

Docker's built-in health checks are passive—they tell Docker when a container fails, but do they tell you? In this tutorial, we'll build a lightweight Python monitoring system that runs entirely on your infrastructure with zero external dependencies. You'll learn to detect container failures in real-time, send instant alerts, and maintain a complete audit log of every state change.

![](https://adamtheautomator.com/wp-content/uploads/2023/07/gitea-docker.jpg)

### [Gitea Docker: Your Ultimate Self-Hosted Git Solution](/gitea-docker/)

Learn how to install a Gitea Docker instance and self-host Git repositories securely in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
