What is Docker?
Docker is a platform that lets developers automate the deployment of applications in lightweight, portable containers. Containers package an application with all its dependencies, making sure it runs the same way on different systems.
Key Concepts:
Containers: Lightweight, standalone, and executable software packages that include everything needed to run an application (code, runtime, system tools, libraries, and settings).
Images: Read-only templates used to create containers. An image includes everything needed to run an application—base operating system, application code, runtime, and dependencies.
Docker Engine: The runtime that builds and runs Docker containers. It has a client-server setup, with a Docker daemon that handles building, running, and managing containers, and a REST API to interact with the daemon.
Installation
To start using Docker, you need to install it on your operating system. Here are the installation steps for different platforms:
Docker Installation Guide
Windows:
Go to the Docker Desktop for WindowsT download page and download the installer.
Run the installer and follow the instructions.
After installation, the Docker Desktop should start automatically. If it doesn’t, start it manually.
Verify the installation by opening a terminal and running:
docker --version
.
macOS:
Go to the Docker Desktop for Mac download page and download the installer.
Open the downloaded
.dmg
file and drag the Docker icon to the Applications folder.Open Docker from the Applications folder.
Verify the installation by opening a terminal and running:
docker --version
.
Linux (Ubuntu):
Update your existing list of packages:
sudo apt update
.Install a few prerequisite packages:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
.Add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
.Add the Docker repository to APT sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
.Update the package database with the Docker packages from the newly added repo:
sudo apt update
.Install Docker:
sudo apt install docker-ce
.Verify that Docker is running:
sudo systemctl status docker
.
Basic Commands
Once Docker is installed, you can start using basic Docker commands:
docker run: Runs a command in a new container. Example:
docker run hello-world
.docker pull: Downloads an image from a Docker registry. Example:
docker pull nginx
.docker build: Creates an image from a Dockerfile. Example:
docker build -t my-image .
.docker ps: Shows all running containers. Example:
docker ps
.docker stop: Stops a running container. Example:
docker stop container_id
.docker rm: Deletes a stopped container. Example:
docker rm container_id
.
Practice Exercise:
Run the
hello-world
image:docker run hello-world
.Pull the
nginx
image:docker pull nginx
.List the running containers:
docker ps
.Run an
nginx
container:docker run -d -p 8080:80 nginx
.Stop the
nginx
container:docker stop container_id
.Remove the
nginx
container:docker rm container_id
.
Understanding Docker Images and Containers
Images vs. Containers:
Images: These are unchangeable files that have everything needed to run an application, like the source code, libraries, and tools.
Containers: These are running instances of images. You can start, stop, move, and delete containers.
Managing Images:
Creating an Image: Use a Dockerfile to define the steps needed to build an image. Example:
# Dockerfile FROM ubuntu:latest RUN apt-get update && apt-get install -y nginx CMD ["nginx", "-g", "daemon off;"]
Build the image:
docker build -t my-nginx-image .
.Listing Images:
docker images
.Removing an Image:
docker rmi image_id
.
Managing Containers:
Creating and Running a Container:
docker run -d my-nginx-image
.Listing Containers:
docker ps -a
.Stopping a Container:
docker stop container_id
.Removing a Container:
docker rm container_id
.
By learning these basic concepts and commands, you'll have a good starting point for using Docker to develop, ship, and run applications easily.