Docker Basics

Learn the fundamentals of Docker and containerization.

What is Docker?

Docker is a platform that uses containerization technology to create and run applications in isolated environments called containers. Think of containers as lightweight, portable packages that include everything needed to run an application - code, runtime, system tools, and libraries.

Why Use Docker?

Docker solves the classic “it works on my machine” problem by ensuring applications run consistently across different environments. Whether you’re developing on your laptop, testing on a server, or deploying to the cloud, Docker containers behave the same way.

Key Docker Concepts

Images

A Docker image is a read-only template that contains the instructions for creating a container. Images are built from a Dockerfile and can be stored in registries like Docker Hub.

Containers

Containers are runnable instances of Docker images. They are isolated from each other and the host system, but can communicate through well-defined channels.

Dockerfile

A Dockerfile is a text file that contains the commands needed to assemble a Docker image. It’s like a recipe for building your container environment.

Installing Docker

Windows and macOS

Download Docker Desktop from the official Docker website. The installer includes Docker Engine, Docker CLI, and Docker Compose.

Linux

Install Docker using your package manager. For Ubuntu/Debian:

sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker

For other Linux distributions, check the official Docker installation guide.

Your First Docker Container

Let’s run your first container with a simple “Hello World” example:

docker run hello-world

This command downloads the hello-world image (if not already present) and runs it in a container. You should see a message confirming that Docker is working correctly.

Running a Web Server

Try running a simple Nginx web server:

docker run -d -p 8080:80 --name my-web-server nginx

Let’s break down this command:

  • docker run: Creates and starts a new container
  • -d: Runs the container in detached mode (background)
  • -p 8080:80: Maps port 8080 on your host to port 80 in the container
  • --name my-web-server: Gives the container a memorable name
  • nginx: The image to use

Now open your browser and navigate to http://localhost:8080. You should see the Nginx welcome page!

Managing Containers

List Running Containers

docker ps

List All Containers (including stopped ones)

docker ps -a

Stop a Container

docker stop my-web-server

Remove a Container

docker rm my-web-server

View Container Logs

docker logs my-web-server

Common Docker Commands

CommandDescription
docker imagesList available images
docker pull <image>Download an image from registry
docker run <image>Create and start a container
docker stop <container>Stop a running container
docker rm <container>Remove a container
docker rmi <image>Remove an image
docker exec <container> <command>Execute command in running container

Next Steps

Now that you understand Docker basics, learn how to create your own Dockerfile to containerize your applications.

Resources

Last updated on