How to use Docker basic commands

DockerDockerBeginner
Practice Now

Introduction

Docker has revolutionized software development by providing a powerful platform for containerization. This comprehensive tutorial will guide you through the fundamental Docker commands, helping developers and system administrators understand how to efficiently manage Docker containers, images, and networks. Whether you're a beginner or looking to refresh your Docker skills, this guide will provide practical insights into using Docker's command-line interface.

Docker Core Concepts

What is Docker?

Docker is an open-source platform designed to automate application deployment, scaling, and management through containerization. It enables developers to package applications with all their dependencies into standardized units called containers, which can run consistently across different computing environments.

Key Docker Concepts

1. Containers

Containers are lightweight, standalone, executable packages that include everything needed to run an application:

  • Code
  • Runtime
  • System tools
  • System libraries
  • Settings
graph LR A[Application Code] --> B[Container] C[Dependencies] --> B D[System Libraries] --> B E[Configuration] --> B

2. Images

Docker images are read-only templates used to create containers. They serve as blueprints for container creation and can be stored in registries like Docker Hub.

3. Dockerfile

A Dockerfile is a text document containing instructions for building a Docker image. It defines the environment, dependencies, and configuration for an application.

Example Dockerfile:

FROM ubuntu:22.04
RUN apt-get update
RUN apt-get install -y python3
COPY . /app
WORKDIR /app
CMD ["python3", "app.py"]

4. Docker Architecture

graph TB A[Docker Client] --> B[Docker Daemon] B --> C[Container Runtime] B --> D[Image Registry] B --> E[Image Storage]

5. Container vs Virtual Machines

Feature Containers Virtual Machines
Resource Usage Lightweight Heavy
Startup Time Seconds Minutes
Isolation Level Process-level Full OS-level
Performance High Lower

Benefits of Docker

  1. Consistency across environments
  2. Faster deployment
  3. Improved scalability
  4. Better resource utilization
  5. Simplified dependency management

Use Cases

  • Microservices architecture
  • Continuous Integration/Continuous Deployment (CI/CD)
  • Cloud-native application development
  • Simplified application testing

Getting Started with LabEx

LabEx provides interactive Docker learning environments that help developers quickly understand and practice containerization technologies.

Docker Installation Guide

Prerequisites

Before installing Docker, ensure your Ubuntu 22.04 system meets the following requirements:

  • 64-bit operating system
  • Kernel version 3.10 or higher
  • Sufficient system resources

Installation Methods

1. Install Using Repository

Step 1: Update Package Index
sudo apt-get update
Step 2: Install Required Packages
sudo apt-get install ca-certificates curl gnupg lsb-release
Step 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
Step 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
Step 5: Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

2. Verify Installation

sudo docker --version
sudo docker run hello-world

Installation Workflow

graph TD A[Update System] --> B[Install Dependencies] B --> C[Add GPG Key] C --> D[Configure Repository] D --> E[Install Docker] E --> F[Verify Installation]

Post-Installation Steps

1. Manage Docker as Non-Root User

sudo usermod -aG docker $USER
newgrp docker

2. Configure Docker to Start on Boot

sudo systemctl enable docker
sudo systemctl start docker

Docker Installation Options

Method Pros Cons
Repository Easy updates Requires internet connection
Official Package Direct control Manual updates
Script Installation Quick setup Less secure

Troubleshooting Common Issues

  • Check system compatibility
  • Verify network connectivity
  • Ensure sufficient disk space
  • Update system packages regularly

LabEx Docker Learning Environment

LabEx provides interactive Docker installation tutorials and hands-on practice environments to help developers master containerization technologies quickly and effectively.

Component Minimum Specification
CPU 2 cores
RAM 4 GB
Disk Space 20 GB
OS Ubuntu 22.04 LTS

Basic Docker Commands

Docker Command Structure

Docker commands follow a standard syntax:

docker [command] [options] [arguments]

Image Management Commands

1. Pull Images

docker pull ubuntu:latest
docker pull nginx:alpine

2. List Images

docker images
docker image ls

3. Remove Images

docker rmi ubuntu:latest
docker image rm nginx:alpine

Container Management Commands

1. Create and Run Containers

docker run -d --name web-server nginx
docker run -it ubuntu:latest /bin/bash

2. List Containers

docker ps        ## Running containers
docker ps -a     ## All containers

3. Start, Stop, Restart Containers

docker start web-server
docker stop web-server
docker restart web-server

4. Remove Containers

docker rm web-server
docker container prune  ## Remove all stopped containers

Container Interaction Commands

1. Execute Commands in Container

docker exec -it web-server bash
docker exec web-server ls /var/www/html

2. View Container Logs

docker logs web-server
docker logs -f web-server  ## Follow log output

Docker Workflow

graph TD A[Pull Image] --> B[Create Container] B --> C[Start Container] C --> D[Interact/Execute] D --> E[Stop Container] E --> F[Remove Container]

Advanced Docker Commands

1. Build Custom Images

docker build -t myapp:v1 .

2. Docker Volumes

docker volume create myvolume
docker run -v myvolume:/app nginx

3. Network Management

docker network create mynetwork
docker run --network mynetwork nginx

Common Docker Command Categories

Category Purpose Example Commands
Image Management Manage Docker images pull, push, build
Container Management Control containers run, start, stop
System Management Docker system info info, version
Network Management Manage networks network create
Volume Management Manage persistent data volume create

Best Practices

  • Use specific image tags
  • Clean up unused resources
  • Use Docker Compose for complex setups
  • Implement multi-stage builds

LabEx Docker Learning

LabEx offers interactive environments to practice these Docker commands, helping developers gain practical containerization skills efficiently.

Useful Docker Command Options

Option Description Example
-d Detached mode docker run -d nginx
-it Interactive terminal docker run -it ubuntu
--name Name a container docker run --name web nginx
-p Port mapping docker run -p 8080:80 nginx

Summary

By mastering these Docker basic commands, you've gained essential skills for container management and deployment. Docker offers a robust solution for creating, running, and managing containers across different environments. As you continue to explore Docker's capabilities, remember that these fundamental commands form the foundation of efficient containerization strategies. Practice and experiment with these commands to become proficient in Docker technology and streamline your development workflows.

Other Docker Tutorials you may like