Run a single Docker container explained step by step

From the series: Getting Started with Docker for Developers Hands-On Examples — Part 1

Are you disappointed by the Docker Getting-Started docs? You couldn’t find anything suitable on YouTube? You want real hands-on? Then get started reading this!

Running a single docker container explained step by step

Make sure you installed “Docker Desktop” and you have the Docker Daemon running. Then open a PowerShell on Windows, a Terminal on Mac/Linux and follow along! This is fun.

You can also follow along in this video, if you prefer:

Link to YouTube

Enter this into the command line:

docker run -it ubuntu /bin/bash

Showing a linux file system inside a Windows PowerShell. Screenshot of “docker run -it ubuntu /bin/bash” and then “ls” What happens here?

  1. the command runs the container and attaches to stdin, the standard input, where the /bin/bash process is opened

flags “-it”: interactive flag, TTY flag (gives you a terminal)

ls

Shows the file system from linux. It’s inside the container.

Now open a second PowerShell (or Terminal) and type in

docker ps

Showing two PowerShell windows. One with a running ubuntu container, the other one outputting “docker ps”. This lists the currently running container

CLI1: exit
  • On CLI1 to exit the container and the linux file system again

    CLI2: docker ps

  • Lists the currently running containers, which is empty

Two Powershell windows. In the upper one I typed exit, in the lower one “docker ps” and it outputs an empty list of running containers. CLI2: docker ps -a

  • Lists all containers, the running ones and the empty ones

CLI1: docker start container_identifier

  • We can restart a container that was previously running

I restarted the container with ID “c83ff57…” by typing “docker start c8” CLI2: docker ps

  • The container comes up as running again.
  • Mind: Creation date != running date

Docker ps is listing the container as “Up” again CLI1: docker attach container_identifier

  • We can re-attach to the container

I am re-attaching to the running container CLI2: docker stop container_identifier

  • It will stop the running container
  • It will automatically kick you out

Entering “docker stop c8” in the lower powershell window automatically triggers an “exit” in the first one. docker ps

  • The container is not running anymore, hence not listed here

    docker ps -a

  • The container is still in our system, but stopped

    docker rm container_identifier

  • Clean up: Remove the container

    docker ps -a

  • And the list is empty again, our container is gone

Listing container, cleaning up, and listing an empty list That’s it for this article. In the next part we are going to run multiple docker containers from the command line step by step.

Heads-Up: If you like this, then also check out the video course Understanding Docker and Docker-Compose — 100% Hands-On**. **It’s a 2.5h Quickstart into Docker. If you want more, then checkout my blog where I blog regularly about Blockchain, Solidity, Ethereum, Docker and other Technical Topics