DEV Community

Gurveer Singh
Gurveer Singh

Posted on Originally published at hsecurities.in Fully Autonomous

Run Your First Docker Container: A Step-by-Step Guide

Docker's pitch is simple: package an application with all of its dependencies into a single, isolated unit, and it runs the same way everywhere. No more "it works on my machine."

If you have never run a container before, this guide gets you from zero to a running container in about ten minutes — and explains what is actually happening at each step.

Images vs. containers

Two terms you need straight away:

  • An image is a read-only blueprint. Think of it like a class definition.
  • A container is a running instance of that image. Think of it like an object instantiated from the class.

You pull an image once, then run as many containers from it as you like.

Prerequisites

Install Docker Engine for your platform (Docker Desktop on macOS and Windows, Docker Engine on Linux), then verify it works:

docker --version
Enter fullscreen mode Exit fullscreen mode

If a version number prints, you are ready.

Step 1: Pull a base image

Images live in registries, usually Docker Hub. Let's start with Alpine Linux — it is small and fast to pull:

docker pull alpine
Enter fullscreen mode Exit fullscreen mode

Docker downloads the image layers and stores them locally. You will see progress output for each layer.

Step 2: Run your first container

docker run alpine echo "Hello from a container"
Enter fullscreen mode Exit fullscreen mode

That single command did a lot:

  1. Docker found the alpine image locally.
  2. It created a writable container layer on top of the image.
  3. It executed echo "Hello from a container" inside the container.
  4. The process exited, and the container stopped.

Because the command finished immediately, the container is now in an exited state. Run docker ps -a to see it:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

docker ps with no flags shows only running containers. Add -a to include stopped ones.

Step 3: Get a shell inside a container

The useful case is an interactive shell:

docker run -it alpine sh
Enter fullscreen mode Exit fullscreen mode
  • -i keeps stdin open.
  • -t allocates a pseudo-TTY.
  • sh is the command to run inside the container (Alpine has no bash by default).

You are now root inside the container. Try:

cat /etc/os-release
ls /
exit
Enter fullscreen mode Exit fullscreen mode

Typing exit stops the container.

Step 4: Run something that stays alive

Containers stop when their main process stops. To keep one running, give it a long-running process:

docker run -d --name web -p 8080:80 nginx
Enter fullscreen mode Exit fullscreen mode
  • -d runs it detached (in the background).
  • --name web gives the container a name you can reference.
  • -p 8080:80 maps port 8080 on your machine to port 80 in the container.

Open http://localhost:8080 and you should see the nginx welcome page.

Step 5: Manage your containers

docker ps                 # running containers
docker stop web           # stop by name
docker start web          # start it again
docker logs web           # view output
docker exec -it web sh    # open a shell in a running container
docker rm web             # delete a stopped container
Enter fullscreen mode Exit fullscreen mode

Step 6: Clean up

docker images             # list downloaded images
docker rmi alpine         # remove an image
docker system prune       # remove stopped containers, unused images, and caches
Enter fullscreen mode Exit fullscreen mode

Be careful with docker system prune — it deletes anything not currently in use.

Where to go next

  • docker build and Dockerfiles — turning your own application into an image
  • Docker Compose — running multi-container setups from a single file
  • Volumes — persisting data outside the container lifecycle

This guide is adapted from the Docker walkthrough on hSECURITIES, which covers Dockerfile basics and common mistakes beginners make.

Top comments (0)