Skip to content
$ lynuxpro
← Docker

LESSON 2 OF 18 · BEGINNER · 18 MIN

Installing Docker and running containers

Free previewLesson 2 of the 2 free lessons in this Pro course.Unlock all 18

HOW IT WORKSWhat happens when you type docker run

POST /containersdocker CLIyou type a commanddockerdDocker Engine
1/5

The docker command is only a client. It sends your request to the Docker daemon (dockerd) over a local API socket.

$ docker run -d --name web -p 8080:80 nginx

This lesson gets Docker running on your machine and teaches the container life cycle: run, list, stop, start, inspect, exec and remove. These are the commands you will type every day, so practise them until they feel automatic.

In simple words

  • Docker Engine: the background service (dockerd) that builds and runs containers.
  • Docker CLI: the docker command you type. It sends your requests to the engine.
  • Container life cycle: the states a container moves through: created, running, stopped, removed.
  • Detached mode (-d): running a container in the background so your terminal stays free.

The pieces of Docker

PieceWhat it is
Docker CLI (docker)The command you type. It only sends requests
Docker daemon (dockerd)The background service that builds images and runs containers
containerd and runcLower-level runtimes the daemon uses to actually start containers
Docker DesktopmacOS and Windows app that runs all of the above in a small Linux VM
RegistryWhere images are stored and downloaded from (Docker Hub by default)

The CLI talks to the daemon over a socket, /var/run/docker.sock. That is why docker commands fail with "Cannot connect to the Docker daemon" when the service is not running.

Installing

macOS and Windows

Install Docker Desktop from docker.com (free for personal use, education and small businesses; larger companies need a paid subscription). Alternatives on macOS are OrbStack, Colima and Rancher Desktop.

Ubuntu (servers and CI runners)

Use Docker's official repository rather than the older docker.io package:

sudo apt-get updatesudo apt-get install -y ca-certificates curlsudo install -m 0755 -d /etc/apt/keyringssudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.ascecho "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \  https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" \  | sudo tee /etc/apt/sources.list.d/docker.listsudo apt-get updatesudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
OUTPUTterminalutf-8 · 9L

Then let your user run Docker without sudo (log out and back in afterwards):

sudo usermod -aG docker $USER
OUTPUTterminalutf-8 · 1L

Membership of the docker group is effectively root access to the host, so only add trusted users.

Amazon Linux 2023

sudo dnf install -y dockersudo systemctl enable --now dockersudo usermod -aG docker ec2-user
OUTPUTterminalutf-8 · 3L

Check it works

docker versiondocker run --rm hello-world
SHELLbashutf-8 · 2L

The container life cycle

created → running → (paused) → stopped (exited) → removed
OUTPUTterminalutf-8 · 1L

Run

docker run -d --name web -p 8080:80 nginx
SHELLbashutf-8 · 1L

The most important docker run options:

OptionMeaningExample
-dDetached: run in the background-d
--nameA name instead of a random one--name web
-p host:containerPublish a port-p 8080:80
-e KEY=valueSet an environment variable-e TZ=Asia/Kolkata
-v src:destMount a volume or folder-v data:/data
--rmDelete the container when it stops--rm
-itInteractive with a terminal-it ubuntu bash
--restartRestart policy--restart unless-stopped

Anything after the image name is the command to run inside the container, replacing the image default:

docker run --rm alpine echo "hello from alpine"docker run -it --rm ubuntu:24.04 bash      # a throwaway Ubuntu shell
OUTPUTterminalutf-8 · 2L

List

docker psdocker ps -a
SHELLbashutf-8 · 2L
CONTAINER ID   IMAGE    STATUS         PORTS                  NAMES3f9a1c2b7d4e   nginx    Up 2 seconds   0.0.0.0:8080->80/tcp   web
OUTPUTterminalutf-8 · 2L

docker ps shows running containers; -a includes stopped ones, which is where you find crashed containers and their exit codes.

Test it

curl -I http://localhost:8080
SHELLbashutf-8 · 1L

Logs

docker logs web
SHELLbashutf-8 · 1L

docker logs -f web follows the log live, and --tail 50 shows only the last 50 lines.

Stop, start and remove

docker stop webdocker ps -adocker start webdocker rm -f web
SHELLbashutf-8 · 4L
  • docker stop sends SIGTERM, waits 10 seconds, then SIGKILL, exactly like the Linux process lesson.
  • docker rm deletes a stopped container; rm -f stops and deletes in one go.

Get inside: exec

docker exec -it web bash         # open a shell in a running containerdocker exec web nginx -t         # run one command inside it
OUTPUTterminalutf-8 · 2L

exec runs an extra process in an existing container. It is for debugging, not for making changes you want to keep.

Inspect

docker inspect web                                   # full JSON config and statedocker inspect -f '{{.NetworkSettings.IPAddress}}' webdocker stats --no-stream                             # live CPU and memory use
OUTPUTterminalutf-8 · 3L

Housekeeping

Stopped containers and unused images pile up and fill disks, especially on CI runners:

docker container prune      # remove all stopped containersdocker image prune -a       # remove images not used by any containerdocker system df            # how much space Docker usesdocker system prune -a      # everything unused (careful on shared hosts)
OUTPUTterminalutf-8 · 4L

Real-world scenario: a quick database for local development

You need PostgreSQL to test a feature, without installing it:

docker run -d --name pg -e POSTGRES_PASSWORD=devpass -p 5432:5432 postgres:16docker psdocker logs pg
SHELLbashutf-8 · 3L

Your app connects to localhost:5432. When you are done, docker rm -f pg and your laptop is clean again. This pattern alone saves hours of installing and uninstalling services.

Common mistakes

  • Forgetting -d, so the terminal is attached to the container and Ctrl+C stops it.
  • Port already allocated: another container or app uses 8080. Pick another host port (-p 8081:80) or stop the other one.
  • Using latest everywhere. Pin versions (postgres:16.4) so a new release does not surprise you.
  • Running everything with sudo docker instead of fixing group membership, then creating root-owned files in your projects.

Practice

  1. Run nginx in the background on port 8080, check it with curl, read its logs, then remove it.
  2. Start a throwaway Alpine container that prints the date and deletes itself.
  3. Run PostgreSQL with a password environment variable and find it in docker ps.
  4. Stop a container and find its exit code with docker ps -a.

Cheat sheet

CommandWhat it does
docker run -d --name N -p H:C IMAGERun in the background with a port
docker ps -aAll containers and their status
docker logs -f NFollow logs
docker exec -it N shShell inside a running container
docker stop N / docker start NStop / start
docker rm -f NForce remove
docker system df / docker system pruneDisk usage / clean up

Key takeaways

  • The CLI is a client; the daemon does the work.
  • run, ps, logs, exec, stop and rm cover the whole container life cycle.
  • Pin image versions and clean up regularly so disks do not fill.

You've finished the free preview of Docker

Upgrade to Pro to unlock the remaining 16 lessons, every practice lab and a certificate when you finish.

See plans