Installing Docker and First Commands

beginner
10 min

Installing Docker and First Commands

Now that you know why containers matter, it is time to get Docker running on your own machine. In this lesson you will install Docker on Windows, macOS, or Linux, verify the installation, and practice the handful of commands that make up the daily Docker workflow.

Installing Docker

Windows and macOS

On Windows and macOS the easiest path is Docker Desktop, a bundle that includes the Docker Engine, the command line client, and a management GUI. Download it from docker.com, run the installer, and start the app. On Windows, Docker Desktop uses WSL 2 (Windows Subsystem for Linux) under the hood, so enable WSL 2 if the installer asks.

Linux

On Linux you install Docker Engine directly. On Ubuntu or Debian the quick route is the official convenience script:

bash
curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh sudo usermod -aG docker $USER

The last command lets you run docker without sudo. Log out and back in for it to take effect.

Verifying the Installation

Confirm everything works with two commands:

bash
docker --version docker run hello-world

The hello-world container prints a message confirming that the client, the daemon, and the registry connection all work. If you see that message, your setup is complete.

The Essential Commands

Running containers

bash
docker run nginx # run in the foreground docker run -d nginx # run detached (background) docker run -d -p 8080:80 nginx # map host port 8080 to container port 80 docker run -d --name web nginx # give the container a friendly name

The -d flag detaches the container so your terminal stays free, -p publishes ports, and --name saves you from typing random generated names later.

Listing and inspecting

bash
docker ps # running containers docker ps -a # all containers, including stopped ones docker images # images stored locally docker logs web # view output from the container named web

docker ps is the command you will type most often. Each row shows the container ID, image, status, and port mappings.

Stopping and cleaning up

bash
docker stop web # graceful stop docker rm web # remove a stopped container docker rmi nginx # remove an image from local storage

Stopped containers still occupy disk space until removed, so make docker rm part of your routine, or use docker run --rm to auto-remove a container when it exits.

Getting a Shell Inside a Container

Sometimes you want to poke around inside a running container. The exec command runs an extra process in it:

bash
docker exec -it web sh

The -it flags give you an interactive terminal. Inside, you can inspect files, check processes, and test commands. Type exit to leave - the container keeps running.

A Quick Practice Session

Try this sequence to cement the workflow:

bash
docker run -d -p 8080:80 --name my-nginx nginx docker ps docker logs my-nginx docker stop my-nginx docker rm my-nginx

You just ran a production-grade web server, checked its logs, and cleaned up - in under a minute.

Key Takeaways

  • Docker Desktop is the standard install on Windows and macOS; Linux uses Docker Engine directly.
  • docker run hello-world verifies your whole setup end to end.
  • Core daily commands: run, ps, logs, stop, rm, exec.
  • Use -d for background containers, -p for port mapping, and --name for readable names.
  • Clean up stopped containers regularly to reclaim disk space.

Next up: Images vs Containers, where we dig into the most important distinction in Docker and how layers make images efficient.

Installing Docker and First Commands - Docker | CodeYourCraft | CodeYourCraft