Quick one-liner: Learn how to run your first Docker container — from finding images on Docker Hub to accessing the container shell and cleaning up properly.
Why This Matters
Now that Docker is installed, it's time to run your first container.
If you're like most people starting with Docker, you might be tempted to just copy-paste commands without understanding what they do. I've been there. But here's the thing: knowing what each flag does means you'll understand what's happening when you run a container.
This guide walks you through running your first real container, step by step. No copy-paste without explanation. Every command broken down.
By the end, you'll know how to:
- Find and pull images from Docker Hub
- Run containers with the right flags
- Access a running container's shell
-
Stop and clean up properly
Prerequisites
Docker installed (rootless mode recommended — see Ubuntu guide or SLES guide)
5 minutes to run your first container
-
Terminal access to your Docker host
Finding Images on Docker Hub
Docker Hub is the primary registry for Docker images. Think of it as an app store for containers.
You can browse at hub.docker.com or search from the command line:
$ docker search nginx
Look for images with the Official badge — these are maintained by the software's creators and are regularly updated with security patches.
My advice: If there's an official image, use it. It's more likely to be secure, up-to-date, and well-maintained.
If there's no official image, look for:
- High download counts
- Recent updates
- Good star ratings
Each image page shows:
- Available tags (versions) — for example,
nginx:latest,nginx:1.25,nginx:alpine - Usage instructions and supported environment variables
- Pull count and last update date
Note: When no tag is specified, Docker uses latest by default. For production, pin to a specific version like nginx:1.25 so your builds don't break when a new version drops.
Running Your First Container
Let's run nginx — the most popular web server.
$ docker container run -d --rm --name dtnginx nginx
Let me break down each flag:
| Flag | What It Does |
|---|---|
-d |
Detach — runs in the background so your terminal stays free |
--rm |
Auto-remove — deletes the container when it stops (keeps things clean) |
--name dtnginx |
Name it — use "dtnginx" instead of a random ID like "a1b2c3d4e5f6" |
nginx |
The image — pulled from Docker Hub if not already present |
The first time you run this, Docker downloads the image:
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
09f376ebb190: Pull complete
5529e0792248: Pull complete
Status: Downloaded newer image for nginx:latest
And... done. The container is now running in the background. You didn't see anything pop up, but trust me, it's there.
Checking Container Status
Let's verify it's actually running.
$ docker ps
You should see:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "/docker-entrypoint..." 5 sec Up 4 seconds 80/tcp dtnginx
A few things to note:
-
CONTAINER ID — you can use this to manage the container. But since we gave it a name (
dtnginx), just use that. It's way easier to remember. - PORTS — shows nginx is listening on port 80 inside the container. You can't access it from your host yet — we'll cover port forwarding in a later post.
- STATUS — "Up 4 seconds" means it's running.
To see all containers (including stopped ones):
$ docker ps -a
Accessing the Container Shell
Here's something cool — you can open a shell inside a running container.
$ docker exec -it dtnginx /bin/bash
Let me explain the flags:
| Flag | What It Does |
|---|---|
-i |
Keeps standard input open — so you can type commands |
-t |
Allocates a terminal — so you get a proper prompt |
Together, -it is what you'll use 99% of the time.
Important: The best command to access a container depends on what's inside.
• nginx, Ubuntu, Debian → /bin/bash works
• Alpine-based images → Use /bin/sh instead (bash isn't installed)
• Redis → Has /bin/bash and /bin/sh, but redis-cli is more useful for interacting with the database
Always check the image documentation on Docker Hub to see what's available.
You're now inside the container. Check the prompt — it probably says something like root@a1b2c3d4e5f6.
You're root inside this container. But remember, this is isolated from your host system.
Let's verify nginx is actually running. The official nginx image includes curl:
dtnginx# curl localhost
You should see the default nginx welcome page HTML:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
Exit the container shell:
dtnginx# exit
And you're back on your host system.
Stopping and Cleaning Up
To stop the container:
$ docker stop dtnginx
Because we used --rm when we created it, the container is automatically removed after stopping.
Let's confirm:
$ docker ps -a
If you don't see dtnginx, that's good — it was automatically removed.
Note: If you had started a container without --rm, it would stick around in a stopped state. In that case, you'd need to clean it up manually:
docker rm dtnginx
Good habit to get into — clean up your stopped containers. If you have the containers from previous tutorials that are not removed and randomly named, you can remove them using the following command:
$ docker container rm container_id
Exercise
Try it yourself.
Part 1: nginx with Shell Access
-
Run an nginx container named
dtnginxin detached mode without the--rmflag -
Access the shell with
docker exec -it dtnginx /bin/bash -
Verify nginx is running by running
curl localhostinside the container - Exit the container shell
-
Stop the container with
docker stop dtnginx -
Verify that it was not removed with
docker ps -a -
Remove the container with
docker rm dtnginx
Part 2: Redis with redis-cli (No Shell Needed)
Not all containers require a shell to be useful. Redis is a great example — while it does have /bin/bash and /bin/sh available, the native redis-cli tool is far more useful for interacting with the database.
-
Run a Redis container named
dtrediswith the--rmflag:
docker run -d --rm --name dtredis redis
-
Access Redis using
redis-clidirectly:
docker exec -it dtredis redis-cli
- Set and get a key :
127.0.0.1:6379> SET mykey "Hello from Docker!"
OK
127.0.0.1:6379> GET mykey
"Hello from Docker!"
- Exit redis-cli :
127.0.0.1:6379> exit
- Stop the container :
docker stop dtredis
Key takeaway: The command you use to access a container depends on what's inside. For shells, try /bin/bash first, then /bin/sh. For databases and specialized applications, use their native CLI tools (redis-cli, psql, mysql, etc.) — they're often more useful than a generic shell.
What's Next
Now that you've run your first container, you can:
- Try other images — Redis, PostgreSQL, Node.js, Python
- Learn about port forwarding — make your container accessible from outside
- Explore container networking — connect containers to each other
Coming up: Learn how to manage your Docker environment — listing images, viewing logs, using environment variables, and cleaning up disk space. Then we'll cover Docker volumes to keep your data safe across container restarts.
Want More?
This guide covers the basics from Chapter 3: Running Docker Images in my book, "Levelling Up with Docker" — 14 chapters of practical, hands-on Docker guides.
📚 Grab the book: "Levelling Up with Docker" on Amazon
Found this helpful?
Share on LinkedInTweet about it
Questions? Drop a comment below or reach out on LinkedIn
Published: 17 Mar 2026
Author: David Tio
Tags: Docker, Beginners, Tutorial, Linux, DevOps, Container
Word Count: ~900
Top comments (0)