← Back to Home
🐳

Images & Containers in Practice

The handful of commands you'll use every single day with Docker.

10 min read📚DevOps Fundamentals

Now that you understand the concepts, let’s build the muscle memory. You only need about ten commands to work confidently with Docker day to day.


🎯 Learning Objectives

By the end of this lesson you will:


The Container Lifecycle

Command What it does
docker run <image> Create and start a container
docker ps List running containers
docker ps -a List all containers (including stopped)
docker stop <id> Gracefully stop a container
docker rm <id> Remove a stopped container
bash
docker run -d --name web -p 8080:80 nginx   # start named container
docker ps                                    # see it running
docker stop web                              # stop it
docker rm web                                # remove it

💡 Name your containers

--name web gives the container a friendly name so you can type docker stop web instead of copying a random ID. Small habit, big time-saver.


Looking Inside a Running Container

Two commands do most of your debugging:

bash
docker logs web            # view a container's output
docker logs -f web         # follow logs live
docker exec -it web bash   # open a shell inside the container

docker exec -it drops you into a shell inside the container, so you can inspect files, environment variables, and processes as if you were SSH’d into a tiny machine.

bash — 80×24
student@devops:~$docker exec -it web bash -c 'cat /etc/os-release | head -2'

Managing Images

Command What it does
docker images List local images
docker pull <image> Download an image
docker rmi <image> Remove an image
docker image prune Clean up unused images

⚠ Disk fills up fast

Images, stopped containers, and unused volumes quietly eat disk space on build servers. docker system prune -a clears the lot — powerful, but it deletes anything not currently in use, so use it deliberately.


Ports and Volumes

Two flags you’ll use constantly:

bash
docker run -d \
-p 5432:5432 \
-e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data \
--name db postgres

Here the database’s files live in a named volume pgdata, so your data isn’t lost when the container restarts.

💡 Containers are disposable, data is not

Treat containers as throwaway. Anything you need to keep — databases, uploads — belongs in a volume, never inside the container’s own filesystem.


🧪 Hands-on Lab

📝

Run, Inspect and Clean Up

  1. Run an Nginx container named myweb on port 8080
  2. View its logs, then open a shell inside it
  3. Stop and remove it, then confirm it’s gone

🧠 Knowledge Check

Knowledge Check

Which command opens an interactive shell inside a running container named 'web'?

Knowledge Check

Where should a database keep its data so it survives the container being removed?


💼 Interview Preparation

Interview Q&A

A container keeps crashing on startup. How do you debug it?


Summary

You can now manage the full container lifecycle, inspect containers with logs and exec, handle images, and persist data with volumes. Next, you’ll package your own application into an image by writing a Dockerfile.

Up Next

Writing a Dockerfile

You've mastered this lesson. Continue your journey to becoming a DevOps Engineer.

Start Next Lesson