Docker Installation
/ 3 min read
Table of Contents
Installing Docker
Linux Distros with APT Package Manager
To install Docker on Linux distros like Ubuntu or Debian that use the APT package manager, you can do so with the following commands:
# Remove previous versions:sudo apt-get remove docker docker-engine docker.io containerd runc
# Add packages to install and add the Docker repositorysudo apt-get install ca-certificates curl gnupg-agent lsb-release
# Add the GPG key for the Docker repositorycurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Add the repository to our sources.list file, which is where APT queries the repositories to download programsecho \ "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Update repositories to detect the Docker repositorysudo apt update
# Install Docker and its dependenciessudo apt install docker-ce docker-ce-cli containerd.ioConfiguration to use Docker without sudo:
# Add the user to the Docker groupsudo usermod -a -G docker $USER# To use it without needing to restart, run the following command to "log in" to the group in the current sessionnewgrp docker# Grant permissions to the Docker daemon socketsudo chmod 666 /var/run/docker.sock
# Docker should now work; if it still doesn't, we need to restart the systemreboot nowWindows
To install Docker on Windows:
- Go to the Docker website.
- Click on the download button for Windows.
- Once downloaded, follow the installer.
- Before running Docker, you will also need to install the WSL2 Kernel, which you can download here.
- Install this package, which will enable Docker virtualization.
Basic Docker Commands
# Create a Docker container:docker run -ti --name web ubuntu:latest
# Create a container with an open port:# The format is LocalPort:ContainerPort# By connecting to localhost:8000 from our browser, we would connect# to port 80 of the containerdocker run --name web2 -ti -p 8000:80 web:v1
# Create a container with a shared folder# The format is LocalFolder:FolderInContainerdocker run -ti --name web -ti -p 8000:80 -v C:\\Docker\\web:/var/www/html web:v1
# Create an image from a container:# If we are inside the container, exit# docker commit -m "Commit" containername repository:tagdocker commit -m "Image with Apache" web web:v1
# View images in Docker:docker images
# View containersdocker ps -a
# Remove a containerdocker rm web
# Connect to an already running containerdocker exec -ti web bashConclusion
Docker is an essential tool for modern development and deployment workflows. With these installation steps and basic commands, you’re ready to start containerizing your applications and enjoying the benefits of consistent, isolated development environments.