How to run first Docker container

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful platform that revolutionizes software development and deployment through containerization technology. This tutorial provides a comprehensive guide for developers and IT professionals to understand the fundamentals of Docker and successfully run their first container, enabling efficient and consistent application environments across different systems.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/ImageOperationsGroup(["`Image Operations`"]) docker(("`Docker`")) -.-> docker/SystemManagementGroup(["`System Management`"]) docker/ContainerOperationsGroup -.-> docker/create("`Create Container`") docker/ContainerOperationsGroup -.-> docker/rm("`Remove Container`") docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/ImageOperationsGroup -.-> docker/pull("`Pull Image from Repository`") docker/ImageOperationsGroup -.-> docker/images("`List Images`") docker/SystemManagementGroup -.-> docker/info("`Display System-Wide Information`") docker/SystemManagementGroup -.-> docker/version("`Show Docker Version`") subgraph Lab Skills docker/create -.-> lab-418476{{"`How to run first Docker container`"}} docker/rm -.-> lab-418476{{"`How to run first Docker container`"}} docker/ps -.-> lab-418476{{"`How to run first Docker container`"}} docker/run -.-> lab-418476{{"`How to run first Docker container`"}} docker/stop -.-> lab-418476{{"`How to run first Docker container`"}} docker/pull -.-> lab-418476{{"`How to run first Docker container`"}} docker/images -.-> lab-418476{{"`How to run first Docker container`"}} docker/info -.-> lab-418476{{"`How to run first Docker container`"}} docker/version -.-> lab-418476{{"`How to run first Docker container`"}} end

Docker Fundamentals

What is Docker?

Docker is an open-source platform that enables developers to automate application deployment, scaling, and management through containerization. It provides a lightweight, portable, and self-sufficient environment for running applications.

Core Concepts

Containers

Containers are lightweight, standalone, executable packages that include everything needed to run an application: code, runtime, system tools, libraries, and settings.

graph LR A[Application Code] --> B[Container] C[Dependencies] --> B D[Runtime Environment] --> B

Docker Images

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

Docker Architecture

Component Description
Docker Client Command-line interface for interacting with Docker
Docker Daemon Background service managing Docker objects
Docker Registry Storage and distribution system for Docker images

Key Advantages

  1. Consistency: Ensures identical environments across development and production
  2. Isolation: Separates applications and their dependencies
  3. Efficiency: Lightweight compared to traditional virtual machines
  4. Scalability: Easy to scale applications horizontally

Use Cases

  • Microservices deployment
  • Continuous Integration/Continuous Deployment (CI/CD)
  • Cloud-native application development
  • Development and testing environments

Getting Started with LabEx

For hands-on learning and practical experience with Docker, LabEx provides interactive environments to help you master containerization technologies.

Docker vs Traditional Virtualization

graph TD A[Traditional Virtualization] --> B[Full OS for each application] A --> C[High Resource Overhead] D[Docker Containerization] --> E[Shared OS Kernel] D --> F[Lightweight Containers]

By understanding these fundamentals, you'll be well-prepared to dive into Docker and leverage its powerful containerization capabilities.

Setup and Config

System Requirements

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

  • 64-bit operating system
  • Linux kernel 3.10 or higher
  • Minimum 2GB RAM
  • Sufficient disk space

Installation Methods

Method 1: Official Docker Repository

## Update package index
sudo apt-get update

## Install required packages
sudo apt-get install ca-certificates curl gnupg lsb-release

## 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

## Set up stable 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

## Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Method 2: Convenience Script

## Download and run Docker installation script
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Post-Installation Configuration

User Group Configuration

## Add current user to docker group
sudo usermod -aG docker $USER

## Verify installation
docker --version

Docker Configuration Files

File Path Purpose
/etc/docker/daemon.json Docker daemon configuration
~/.docker/config.json User-specific Docker settings

Docker Compose Installation

## Download Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

## Apply executable permissions
sudo chmod +x /usr/local/bin/docker-compose

## Verify installation
docker-compose --version

Verification Workflow

graph TD A[Install Docker] --> B[Add User to Docker Group] B --> C[Verify Docker Version] C --> D[Run Test Container] D --> E[Confirm Successful Setup]

Testing Docker Installation

## Run hello-world container
sudo docker run hello-world

LabEx Recommendation

For comprehensive Docker learning and hands-on practice, LabEx offers interactive environments that guide you through practical Docker configurations and use cases.

Common Troubleshooting

  • Restart Docker service: sudo systemctl restart docker
  • Check Docker status: sudo systemctl status docker
  • Verify network connectivity: docker info

Running First Container

Basic Docker Commands

Pulling an Image

## Pull Ubuntu image from Docker Hub
docker pull ubuntu:latest

Container Lifecycle Commands

Command Description
docker run Create and start a new container
docker ps List running containers
docker ps -a List all containers
docker start Start a stopped container
docker stop Stop a running container

Interactive Container Execution

## Run Ubuntu container interactively
docker run -it ubuntu:latest /bin/bash

Container Modes

graph LR A[Container Modes] --> B[Interactive Mode] A --> C[Detached Mode] A --> D[Foreground Mode] A --> E[Background Mode]

Detached Mode Example

## Run Nginx web server in background
docker run -d -p 80:80 nginx:latest

Port Mapping

## Map container port to host port
docker run -d -p 8080:80 nginx:latest

Volume Mounting

## Mount host directory to container
docker run -v /host/path:/container/path ubuntu:latest

Environment Variables

## Set environment variables
docker run -e MYSQL_ROOT_PASSWORD=secret mysql:latest

Container Management

## Remove a container
docker rm container_name

## Remove all stopped containers
docker container prune

Advanced Container Operations

Inspecting Containers

## View container details
docker inspect container_name

## Check container logs
docker logs container_name

Best Practices

  1. Use official images
  2. Keep containers lightweight
  3. Use specific image tags
  4. Implement proper resource limits

LabEx Learning Recommendation

LabEx provides comprehensive Docker container management tutorials to help you master practical container skills.

Container Workflow

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

Common Scenarios

Scenario Docker Command
Web Server docker run -d -p 80:80 nginx
Database docker run -d mysql:latest
Python App docker run -it python:3.9

Troubleshooting Tips

  • Check container status: docker ps
  • View container logs: docker logs
  • Restart container: docker restart

Summary

By following this tutorial, you have learned the essential steps to set up Docker, configure your environment, and run your first container. Docker offers a robust solution for application packaging, distribution, and deployment, allowing developers to create portable and scalable software solutions with minimal configuration and maximum consistency across different computing environments.

Other Docker Tutorials you may like