What is Docker?
Docker is a containerization platform that allows you to package applications and their dependencies into lightweight, portable containers. These containers can run consistently across different environments, from development laptops to production servers.
Docker containers are similar to virtual machines but much more efficient. They share the host OS kernel and use fewer resources while providing the same isolation benefits.
- Consistency: Applications run the same way everywhere
- Efficiency: Lightweight compared to virtual machines
- Scalability: Easy to scale applications up or down
- Portability: Move applications between environments easily
Prerequisites
Before installing Docker, ensure you have:
- A 64-bit operating system
- Administrative/root access to your system
- At least 4GB of RAM (8GB recommended)
- Virtualization enabled in BIOS (for Windows)
Installing Docker on Ubuntu
Ubuntu provides an easy way to install Docker using the official Docker repository. This method ensures you get the latest version with security updates.
Update Package Index
First, update your existing list of packages:
Terminalsudo apt update
Install Prerequisites
Install packages that allow apt to use repositories over HTTPS:
Terminalsudo apt install apt-transport-https ca-certificates curl gnupg lsb-release
Add Docker's GPG Key
Add Docker's official GPG key:
Terminalcurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add Docker Repository
Set up the stable repository:
Terminalecho "deb [arch=amd64 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
Install Docker Engine
Update the apt package index and install Docker Engine:
Terminalsudo apt update sudo apt install docker-ce docker-ce-cli containerd.io
Add User to Docker Group
Add your user to the docker group to run Docker without sudo:
Terminalsudo usermod -aG docker $USER
Note: Log out and back in for this change to take effect.
Installing Docker on CentOS
CentOS installation is similar to Ubuntu but uses yum/dnf package manager instead of apt.
Remove Old Versions
Remove any older versions of Docker:
Terminalsudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
Install Required Packages
Install the yum-utils package:
Terminalsudo yum install -y yum-utils
Add Docker Repository
Set up the Docker repository:
Terminalsudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Install Docker Engine
Install Docker Engine, containerd, and Docker Compose:
Terminalsudo yum install docker-ce docker-ce-cli containerd.io
Start Docker Service
Start and enable Docker service:
Terminalsudo systemctl start docker sudo systemctl enable docker
Installing Docker on Windows
Docker Desktop for Windows provides an easy way to run Docker on Windows 10/11 with WSL 2 (Windows Subsystem for Linux) backend.
System Requirements
Ensure your system meets the requirements:
- Windows 10 64-bit: Pro, Enterprise, or Education (Build 16299 or later)
- Windows 11 64-bit: Home or Pro version 21H2 or higher
- WSL 2 feature enabled
- Virtualization enabled in BIOS
- At least 4GB RAM
Enable WSL 2
Open PowerShell as Administrator and run:
PowerShell (Admin)wsl --install
Restart your computer when prompted.
Download Docker Desktop
Visit docker.com/docker-desktopand download Docker Desktop for Windows.
Install Docker Desktop
Run the installer and follow the installation wizard. Make sure to:
- Enable "Use WSL 2 instead of Hyper-V" option
- Add shortcut to desktop (optional)
Start Docker Desktop
Launch Docker Desktop from the Start menu. It may take a few minutes to start the first time as it sets up the WSL 2 integration.
If you're using Windows 10 Home, you'll need to upgrade to Windows 10 Pro or use Docker Toolbox (legacy solution) instead of Docker Desktop.
Verify Installation
After installing Docker, verify that it's working correctly by running a test container:
docker --version
You should see output similar to:
Docker version 24.0.7, build afdd53b
Now run the hello-world container to verify Docker is working:
docker run hello-world
This command downloads a test image and runs it in a container. If successful, you'll see a "Hello from Docker!" message.
Basic Docker Commands
Now that Docker is installed, here are some essential commands to get you started:
Container Management
# List running containers docker ps # List all containers (including stopped) docker ps -a # Run a container docker run nginx # Run a container in detached mode docker run -d nginx # Stop a container docker stop <container_id> # Remove a container docker rm <container_id>
Image Management
# List images docker images # Pull an image from Docker Hub docker pull ubuntu # Remove an image docker rmi <image_id> # Build an image from Dockerfile docker build -t myapp .
System Information
# Show Docker system information docker info # Show Docker disk usage docker system df # Clean up unused resources docker system prune
Conclusion
Congratulations! You've successfully installed Docker on your system. Docker is now ready to help you containerize your applications and streamline your development workflow.
Next steps you might consider:
- Learn about Dockerfile and how to create custom images
- Explore Docker Compose for multi-container applications
- Set up a container registry for your images
- Learn about Docker networking and volumes
- Explore container orchestration with Kubernetes
You're now ready to start containerizing your applications with Docker. Happy containerizing!