Getting Started With Docker

Getting Started with Docker

So you’ve decided to try Docker. But where do you actually start?

The CLI can look intimidating at first. But once you understand a few core ideas though, the rest falls into place quickly.

This article covers the fundamentals: What images and containers really are, the commands you’ll use most, and how a container moves through its lifecycle.

Images and Containers — What’s the Difference?

These two terms get used interchangeably, which causes a lot of confusion early on, as they really are different things.

A container image is a downloadable file. It contains the application and everything it needs to run — libraries, configuration, binaries. Think of it like an executable file sitting on disk.

A container is that image running in memory. It’s a live process.

The image is the file. The container is the running instance.

When you ask Docker to run a container, it takes the image and starts a process from it. Multiple containers can be started from the same image, running independently of each other.

Running Your First Container

The most fundamental Docker command is docker run. Let’s start with the classic example:

docker run hello-world

What happens when you run this?

First, Docker checks if you have the hello-world image locally. If this is a new install of Docker, you won’t have this image, so it pulls it from Docker Hub. Then it creates a container from that image, starts it, and you see some output. The container then stops on its own.

That last bit is important. Some containers run, complete a task, and exit. Others, like web servers, run continuously. The hello-world container is the first type. Its only job is to print a message and quit.

Images, Tags, and Docker Hub

When Docker pulled that image, where did it come from?

Docker Hub is the default public registry. This is an online store of container images. Anyone can publish images there. Many popular applications already have official images on Docker Hub.

Every image has a name and a tag. Tags often represent versions. For example:

nginx:latest
nginx:1.27
postgres:16

The part before the colon is the image name. The part after is the tag. If you don’t specify a tag, Docker assumes latest.

Key Concept: Always check available tags before running an image in production. “Latest” doesn’t always mean what you think it does.

To download an image without running it:

docker pull traefik/whoami:v1.11

To see what images you have stored locally:

docker image ls

Running a Container in the Background

Let’s run a real web server:

docker run -d traefik/whoami:v1.11

The -d flag stands for detached mode. The container runs in the background, and you get your terminal back.

Without -d, the container ties up your terminal for as long as it’s running. That’s fine for short tasks, but not for services that run indefinitely.

Checking What’s Running

docker ps

This shows all currently running containers. You’ll see the container ID, the image it was built from, how long it’s been running, and the ports it’s using.

To see all containers, including stopped ones:

docker ps -a

Exposing Ports

An application within the container may open a network port. For example, a web server that uses port 80.

Depending on the network setup, this can allow other containers on the Docker server to access that web service on port 80.

However, by default, this port is available internally only. That is, we can’t access it from outside the Docker server. To change this, we explicitly map a container’s port to a port on the Docker server.

The -p flag maps a port on the Docker host to a port inside the container:

docker run -d -p 80:80 traefik/whoami

The format is host_port:container_port. So -p 80:80 means “listen on port 80 on the host, and forward traffic to port 80 inside the container.”

You can use different port numbers too. -p 8080:80 would make the web server accessible on port 8080 externally, while it still uses port 80 internally.

Naming Your Containers

By default, Docker assigns a random name to each container — something like hopeful_einstein. This is fine, but often it’s better if we assign more meaningful names.

Give your container a name with --name:

docker run -d -p 80:80 --name webserver traefik/whoami

This makes every subsequent command much cleaner. You can reference containers by name instead of hunting for an ID.

Stopping and Removing Containers

To stop a running container:

docker stop webserver

This sends a signal to the container and gives it time to shut down cleanly. The container still exists after stopping. It’s just not running anymore.

To remove it completely:

docker rm webserver

Or combine both steps:

docker rm -f webserver

If you want Docker to clean up automatically when a container stops, add --rm to your docker run command:

docker run --rm traefik/whoami

The Container Lifecycle

A container moves through several states during its life.

Created — The container exists but hasn’t started yet. docker create gets you here.

Running — The container has started and is executing. docker run or docker start gets you here.

Paused — The container’s processes are frozen. Memory is preserved. docker pause and docker unpause control this.

Stopped / Exited — Processes have ended. The container still exists and can be restarted with docker start.

Removed — The container is completely gone. Any internal data it had is gone with it. docker rm does this.

Viewing Logs

When a container is running in detached mode, you won’t see its output in your terminal. Use docker logs to check what it’s doing:

docker logs webserver

To follow the logs in real time (like tail -f in Linux):

docker logs --follow webserver

To see only recent logs, use --since:

docker logs --since 5m webserver

This is often the first thing to check when a container isn’t behaving as expected.

Running Commands Inside a Container

Sometimes you need to poke around inside a running container. The docker exec command lets you run a command inside it:

docker exec webserver ls -al

To open an interactive shell session:

docker exec -it webserver /bin/sh

The -it flags combine two things: -i keeps standard input open (so you can type), and -t gives you a terminal to type into.

Note that not all containers have a shell. Minimal images sometimes strip it out entirely to reduce size.

Cleaning Up Images

Images take up disk space. To see how much:

docker system df

To remove an image:

docker image rm nginx:latest

You can’t remove an image if there’s an existing container (even a stopped one) that was built from it. Remove the container first.

Where to Go Next

The full course on YouTube walks through all of these commands with live demos, so you can follow along:

Watch the free YouTube course → COMING SOON

The Udemy course goes much further — covering storage, networking, building your own images, Docker Compose, and more:

Enrol in the full Udemy course → COMING SOON