DevFox Labs
HomeCoursesRoadmapsChallengesToolsAboutContact
beginner25 minby DevFoxUpdated on 1 Aug 2026

Installing Docker and Running Your First Container

Get Docker onto macOS, Windows or Linux, prove it works, and run something real in one command — then look at what actually happened underneath.

  • Docker
Installing Docker and Running Your First Container. Read this lesson at .

Previous lesson · 1 of 13Why Containers ExistNext lesson · 3 of 13Images, Containers and the Difference

On this page

  • What you are actually installing
  • Installing on macOS
  • Installing on Windows
  • Installing on Linux
  • Proving it works
  • Your first container
  • Reading the run command
  • Seeing and stopping what you started
  • Cleaning up
  • Cheat sheet
  • Where to go next
DevFox Labs

Structured lessons and courses for developers who care about craft.

Platform

HomeCoursesRoadmapsAll lessonsChallengesAboutContactRSS feed

Legal

Terms of ServicePrivacy PolicyCookie Policy

© 2026 DevFox Labs. All rights reserved.

    Knowing why containers exist is one thing; having them on your own machine is another, and this is the lesson where the idea becomes something you can type. By the end of it you will have Docker installed, you will have run a real web server without installing a web server, and you will know how to stop and clean up what you started.

    Take the installation slowly. It is the one step in this course that differs between operating systems, and everything afterwards is identical no matter which one you are on.

    What you are actually installing

    Before downloading anything, it helps to know what the pieces are, because the error messages later refer to them by name.

    There are two pieces, and they can fail independently. That is what makes the most common beginner error confusing until you know this.

    The Docker CLI

    The `docker` command you type.

    It does almost nothing itself: it packages your request, sends it to the engine, and prints whatever comes back.

    docker --version proves only this half exists.

    The Docker Engine

    A background service that does the work.

    A daemon — always running, waiting to be told what to do. It builds images, starts containers and manages them.

    docker info is the command that proves this half is actually running.

    You install both together and they can still be in different states

    On macOS and Windows you install Docker Desktop, which bundles the engine, the CLI, a small Linux virtual machine for the engine to live in, and a graphical dashboard. On Linux the engine runs natively and there is nothing to virtualise.

    Installing on macOS

    Download Docker Desktop from docker.com/products/docker-desktop and pick the build that matches your chip — Apple silicon for M1 and later, Intel for older Macs. Choosing wrong is not dangerous; the installer simply refuses.

    Open the downloaded .dmg, drag Docker into Applications, then launch it. The first start takes a minute while it creates its virtual machine. You will know it is ready when the whale icon in the menu bar stops animating.

    If you use Homebrew, this does the same thing:

    You still have to launch the Docker application once afterwards. Installing the package does not start the engine, and until the engine is running the docker command has nothing to talk to.

    Installing on Windows

    Docker on Windows runs your containers inside WSL 2, the Windows Subsystem for Linux — a real Linux kernel Microsoft ships with Windows. Containers are a Linux feature, so something has to provide the Linux part.

    Install WSL 2 first, from PowerShell as Administrator:

    Restart when it asks. Then install Docker Desktop from docker.com/products/docker-desktop, accepting the default option to use the WSL 2 backend.

    Work inside the Linux filesystem

    Once WSL is installed you get an Ubuntu terminal. Keep your projects inside it — under /home/you/ rather than /mnt/c/Users/you/. Files on the Windows side are reached across a translation layer, and builds over that layer can be several times slower.

    Installing on Linux

    On Linux, install the engine itself; there is no Desktop layer to add. Docker publishes its own repository, and using it matters: the docker.io package in a distribution's own repositories is often years behind.

    For Ubuntu or Debian, the official convenience script is the shortest honest route:

    Then add yourself to the docker group so you do not need sudo for every command:

    What the docker group means

    Membership of the docker group is equivalent to root access on that machine — the daemon runs as root and will do what you ask of it. On a personal machine this is a fair trade for convenience. On a shared or production server, it is a decision to make deliberately rather than by habit.

    Proving it works

    Whichever route you took, two commands confirm the result. Run the first:

    You should see something like Docker version 27.3.1, build ce12230. The exact numbers do not matter.

    That only proves the CLI is installed. This one proves the daemon is running and reachable:

    A working setup prints a long block describing the engine — how many containers exist, the storage driver, the kernel version. If instead you see this, the CLI is fine and the engine is not:

    On macOS or Windows, launch Docker Desktop and wait for it to finish starting. On Linux, start the service with sudo systemctl start docker. This error will find you again on some morning when Desktop did not auto-start, so it is worth recognising on sight.

    Your first container

    Now the interesting part. Run this:

    Open http://localhost:8080 in a browser and a real web server answers you. Nothing was installed on your machine to make that happen.

    Read the output in your terminal, because it narrates exactly what Docker did:

    1. 1

      Look for the image locally

      None found — which is what Unable to find image 'nginx:latest' locally means. Run the same command again later and this step succeeds instead.

    2. 2

      Pull it from Docker Hub

      The default public registry. Those Pull complete lines are the image's pieces arriving one at a time.

    3. 3

      Create a container from the image

      The image is not changed by this, and it is not running yet. The next lesson is about why that distinction matters.

    4. 4

      Start it, and wire up the port

      Your port 8080 is connected to port 80 inside the container, which is why the browser finds anything at all.

    One command, four steps. Only the second one needs the network.

    Your terminal is now showing the server's log output and will not return to a prompt. That is normal: the container is running in the foreground, and you are attached to it. Reload the page in your browser and watch a new line appear.

    Press Ctrl + C to stop it.

    Reading the run command

    That short line had three parts, and every container you ever start has the same shape:

    The image comes last and says what to run. The options come before it and say how to run it. -p 8080:80 publishes a port: traffic arriving at 8080 on your machine is forwarded to 80 inside the container. The order is always host:container, which is worth memorising now because getting it backwards produces a page that never loads and no error at all.

    Two more options you will use constantly:

    -d is short for detach: the container runs in the background and you get your prompt back immediately. --name web gives it a name you choose, instead of the cheerful random one Docker invents.

    Where the random names come from

    Without --name, Docker generates something like elegant_hopper — an adjective and a scientist. They are memorable enough for a throwaway container, and naming anything you intend to interact with twice will save you copying identifiers around.

    Seeing and stopping what you started

    With -d the container is running invisibly. These are the commands that make it visible again.

    You get an id, the image it came from, how long it has been up, the port mapping and the name. Any command that takes a container can take either the name or the id — and with an id, the first few characters are enough.

    Bad — starts a fresh container every time, and quietly accumulates them:

    Good — restarts the container that already exists:

    docker run always creates something new. Reaching for it when you meant "start that thing again" is how people end up with forty stopped containers holding disk space, and how the second run fails on a port the first one still owns.

    Cleaning up

    Stopped containers keep their filesystem and their logs, which is useful for diagnosing a crash and wasteful once you are done.

    For a throwaway container, --rm avoids the mess entirely by deleting it the moment it exits:

    Two commands worth remembering as a pair

    docker ps shows what is running; docker ps -a shows what exists. When a container "disappeared", it almost always exited and is sitting in the second list with a status explaining why.

    Cheat sheet

    Where to go next

    You have used the words image and container as though the difference were obvious, and it is not — it is the single idea that makes everything else in Docker click. The next lesson takes it apart: why an image is a recipe and a container is the meal, what a tag really points at, and why the file you edited inside a container vanished when it stopped.

    Before moving on, run something other than nginx. Try docker run -it python:3.12 python for a Python prompt on a version you do not have installed, or docker run -it ubuntu bash for a shell in a clean Ubuntu. Then exit, and notice your own machine is untouched.

    text
    Cannot connect to the Docker daemon at
    unix:///var/run/docker.sock. Is the docker daemon running?
    text
    Unable to find image 'nginx:latest' locally
    latest: Pulling from library/nginx
    a480a496ba95: Pull complete
    f3ace1b8ce45: Pull complete
    Digest: sha256:0c86dddac19f2ce4fd716ac58c0fd87bf69bfd4edbd...
    Status: Downloaded newer image for nginx:latest
    text
    CONTAINER ID  IMAGE  STATUS        PORTS                 NAMES
    c9a1f4e2b8d7  nginx  Up 2 minutes  0.0.0.0:8080->80/tcp  web
    bash
    brew install --cask docker
    bash
    curl -fsSL https://get.docker.com -o get-docker.sh
    sh get-docker.sh
    bash
    sudo usermod -aG docker $USER
    newgrp docker              # apply the new group in this shell
    bash
    docker --version
    bash
    docker info
    bash
    docker run -p 8080:80 nginx
    bash
    docker run -p 8080:80 nginx
    #          ^^^^^^^^^^ ^^^^^
    #          options    image
    bash
    docker run -d -p 8080:80 --name web nginx
    bash
    docker ps
    bash
    docker logs web            # what the container has printed
    docker logs -f web         # ...and keep following it live
    docker stop web            # ask it to shut down (SIGTERM)
    docker start web           # run it again
    docker rm web              # delete it permanently
    bash
    docker run -d -p 8080:80 nginx
    docker run -d -p 8080:80 nginx    # "port is already allocated"
    bash
    docker start web
    bash
    docker ps -a               # everything, including stopped
    docker rm web              # remove one
    docker container prune     # remove every stopped container
    bash
    docker run --rm -p 8080:80 nginx
    bash
    # Confirming the install
    docker --version                    # CLI is installed
    docker info                         # daemon is running and reachable
    
    # Running
    docker run nginx                    # run in the foreground
    docker run -d nginx                 # run detached, in the background
    docker run -p 8080:80 nginx         # host port 8080 -> container 80
    docker run --name web nginx         # choose the container's name
    docker run --rm nginx               # delete it as soon as it exits
    docker run -it ubuntu bash          # interactive shell in a container
    
    # Looking
    docker ps                           # running containers
    docker ps -a                        # all containers, running or not
    docker logs web                     # a container's output
    docker logs -f web                  # follow the output live
    
    # Lifecycle
    docker stop web                     # ask it to shut down
    docker start web                    # start an existing container again
    docker restart web                  # stop then start
    docker rm web                       # delete a stopped container
    docker container prune              # delete every stopped container
    powershell
    wsl --install