How to Install and Use Docker on Ubuntu: A Complete Guide for Beginners

Introduction

Docker has revolutionized how we develop, ship, and run applications. This guide will walk you through installing Docker on Ubuntu and get you started with containerization. Whether you’re a developer, system administrator, or tech enthusiast, this guide will help you understand and use Docker effectively.

What is Docker?

Docker is a platform that allows you to automate the deployment of applications inside lightweight, portable containers. Think of containers as lightweight VMs that package everything needed to run an application:

  • The code
  • Runtime environment
  • System tools
  • System libraries
  • Settings

Benefits of Docker:

  • Consistency: Applications run the same way everywhere
  • Lightweight: Containers share the host OS kernel
  • Quick deployment: Start, stop, and restart applications quickly
  • Isolation: Applications run independently without conflicts

Prerequisites

Before installing Docker, ensure your Ubuntu system meets these requirements:

  • Ubuntu 20.04 or newer
  • 64-bit system
  • Sudo privileges
  • At least 4GB RAM
  • Internet connection

Installation Steps

1. Update System Packages

sudo apt update
sudo apt upgrade -y

2. Install Required Dependencies

sudo apt install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common \
    gnupg

3. Add Docker’s Official GPG Key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

4. Set Up Docker Repository

echo "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

5. Install Docker Engine

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

6. Verify Installation

sudo docker run hello-world
sudo usermod -aG docker $USER
newgrp docker

Basic Docker Commands

Check Docker Version

docker --version

View System Information

docker info

List Running Containers

docker ps

List All Containers (Including Stopped)

docker ps -a

List Downloaded Images

docker images

Creating Your First Container

1. Pull and Run Nginx Web Server

docker run -d -p 80:80 --name my-nginx nginx

This command:

  • -d: Runs container in detached mode (background)
  • -p 80:80: Maps host port 80 to container port 80
  • --name my-nginx: Names the container
  • nginx: Specifies the image to use

2. Verify Container is Running

docker ps

3. Access Nginx

Open your browser and visit: http://localhost

4. Stop and Remove Container

docker stop my-nginx
docker rm my-nginx

Working with Docker Images

Searching for Images

docker search ubuntu

Pulling Images

docker pull ubuntu:latest

Creating Custom Images

  1. Create a Dockerfile:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
  1. Build the Image:
docker build -t my-custom-nginx .

Docker Compose Basics

1. Install Docker Compose

sudo apt install docker-compose

2. Create a Simple Docker Compose File

Create docker-compose.yml:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example

3. Run Docker Compose

docker-compose up -d

Best Practices

  1. Use Official Images: Always prefer official images from Docker Hub
  2. Tag Images Properly: Use meaningful tags for version control
docker build -t myapp:1.0 .
  1. Use .dockerignore: Exclude unnecessary files
node_modules
npm-debug.log
Dockerfile
.dockerignore
  1. Minimize Layers: Combine RUN commands in Dockerfile
RUN apt-get update && \
    apt-get install -y package1 package2 && \
    apt-get clean
  1. Use Multi-stage Builds: Reduce final image size
FROM node:14 AS builder
WORKDIR /app
COPY . .
RUN npm install && npm run build

FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html

Troubleshooting Common Issues

1. Permission Denied

Solution: Add user to docker group

sudo usermod -aG docker $USER

2. Container Won’t Start

Check logs:

docker logs container_name

3. Cannot Connect to Docker Daemon

Solution: Start Docker service

sudo systemctl start docker

4. Port Already in Use

Solution: Change port mapping or stop conflicting service

sudo netstat -tuln | grep 80

5. Disk Space Issues

Clean unused resources:

docker system prune -a

Conclusion

You’ve now learned the basics of Docker on Ubuntu! Practice these commands and concepts, and gradually explore more advanced features like:

  • Network configuration
  • Volume management
  • Container orchestration
  • Custom image building
  • Multi-container applications

Remember: Docker has excellent documentation and a supportive community. Don’t hesitate to consult the official docs or ask questions on Docker forums when you need help.

Additional Resources

Leave a Reply

Your email address will not be published. Required fields are marked *