How to deploy first Docker image

DockerDockerBeginner
Practice Now

Introduction

Docker has revolutionized software development by providing a powerful platform for containerization. This tutorial will guide you through the essential steps of deploying your first Docker image, helping developers and IT professionals understand the fundamental concepts and practical implementation of Docker containers.


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/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/start("`Start 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-418473{{"`How to deploy first Docker image`"}} docker/ps -.-> lab-418473{{"`How to deploy first Docker image`"}} docker/run -.-> lab-418473{{"`How to deploy first Docker image`"}} docker/start -.-> lab-418473{{"`How to deploy first Docker image`"}} docker/stop -.-> lab-418473{{"`How to deploy first Docker image`"}} docker/pull -.-> lab-418473{{"`How to deploy first Docker image`"}} docker/images -.-> lab-418473{{"`How to deploy first Docker image`"}} docker/info -.-> lab-418473{{"`How to deploy first Docker image`"}} docker/version -.-> lab-418473{{"`How to deploy first Docker image`"}} end

Docker Basics

What is Docker?

Docker is an open-source platform designed to automate the deployment, scaling, and management of applications using containerization technology. It allows developers to package an application with all its dependencies into a standardized unit called a container.

Key Docker Concepts

Containers

A container is a lightweight, standalone, executable package that includes everything needed to run an application: code, runtime, system tools, libraries, and settings.

graph TD A[Application Code] --> B[Container] C[Dependencies] --> B D[System Libraries] --> B E[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

Why Use Docker?

  1. Consistency across different environments
  2. Lightweight and fast deployment
  3. Easy scalability
  4. Improved resource utilization
  5. Simplified configuration management

Docker Installation on Ubuntu 22.04

To install Docker on Ubuntu, use the following commands:

## Update package index
sudo apt update

## Install dependencies
sudo apt install apt-transport-https ca-certificates curl software-properties-common

## 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 the 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 update
sudo apt install docker-ce docker-ce-cli containerd.io

Verifying Docker Installation

After installation, verify Docker is working correctly:

## Check Docker version
docker --version

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

Docker Workflow

graph LR A[Develop] --> B[Build Image] B --> C[Push to Registry] C --> D[Pull Image] D --> E[Run Container]

Getting Started with LabEx

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

Environment Setup

System Requirements

Before setting up the Docker environment, ensure your system meets the following requirements:

Requirement Specification
Operating System Ubuntu 22.04 LTS
CPU 64-bit processor
RAM Minimum 4GB
Storage 20GB free disk space

Preparing the System

Update System Packages

sudo apt update
sudo apt upgrade -y

Install Required Dependencies

sudo apt install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release \
    software-properties-common

Docker Installation Methods

graph LR A[Docker Installation] --> B[Repository Installation] A --> C[Manual Package Installation] A --> D[Script-based Installation]

Repository Installation (Recommended)

Add Docker's Official GPG Key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Set Up Repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker Packages
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

Post-Installation Configuration

Add User to Docker Group

sudo usermod -aG docker $USER

Configure Docker to Start on Boot

sudo systemctl enable docker
sudo systemctl start docker

Verify Installation

Check Docker Version

docker --version
docker compose version

Run Hello World Container

sudo docker run hello-world

Docker Compose Setup

Install Docker Compose

sudo apt install docker-compose
Tool Purpose
Docker Desktop GUI for Docker management
Visual Studio Code IDE with Docker extension
Docker Hub Container image repository

LabEx Environment Recommendation

LabEx provides pre-configured Docker environments that simplify setup and learning, making it easier for beginners to get started with containerization technologies.

Troubleshooting Common Installation Issues

Potential Problems

  • Incompatible system architecture
  • Outdated system packages
  • Insufficient permissions
  • Network connectivity issues
  1. Ensure system meets minimum requirements
  2. Update system packages regularly
  3. Check internet connectivity
  4. Verify user permissions
  5. Consult official Docker documentation

First Container Deploy

Understanding Container Deployment

Container Deployment Workflow

graph LR A[Select Image] --> B[Pull Image] B --> C[Create Container] C --> D[Start Container] D --> E[Manage Container]

Pulling Docker Images

Official Docker Hub Images

## Pull Ubuntu image
docker pull ubuntu:latest

## Pull Nginx web server image
docker pull nginx:latest

## Pull Python development image
docker pull python:3.9

Running First Container

Basic Container Execution

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

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

## Run Python interactive container
docker run -it python:3.9 python

Container Management Commands

Command Description
docker ps List running containers
docker ps -a List all containers
docker start <container_id> Start stopped container
docker stop <container_id> Stop running container
docker rm <container_id> Remove container

Creating Custom Container

Dockerfile Example

## Use official Python base image
FROM python:3.9

## Set working directory
WORKDIR /app

## Copy application files
COPY . /app

## Install dependencies
RUN pip install flask

## Expose port
EXPOSE 5000

## Define startup command
CMD ["python", "app.py"]

Build and Run Custom Image

## Build Docker image
docker build -t myapp:v1 .

## Run custom container
docker run -p 5000:5000 myapp:v1

Container Networking

graph TD A[Container] -->|Port Mapping| B[Host Network] B -->|Expose Services| C[External Access]

Port Mapping Example

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

Container Volumes

Volume Management

## Create named volume
docker volume create mydata

## Mount volume to container
docker run -v mydata:/data ubuntu:latest

Best Practices

  1. Use official images
  2. Minimize image size
  3. Implement multi-stage builds
  4. Use specific image tags
  5. Avoid running containers as root

Monitoring Containers

## View container logs
docker logs <container_id>

## Monitor resource usage
docker stats

## Inspect container details
docker inspect <container_id>

LabEx Learning Environment

LabEx provides interactive Docker labs that help developers practice container deployment techniques in a hands-on, guided environment.

Advanced Deployment Techniques

Technique Description
Docker Compose Multi-container orchestration
Kubernetes Container scaling and management
Docker Swarm Native Docker clustering

Summary

By completing this tutorial, you've gained valuable insights into Docker containerization, learned how to set up a Docker environment, and successfully deployed your first container. These foundational skills will empower you to leverage Docker's capabilities for more complex application deployment and management in real-world scenarios.

Other Docker Tutorials you may like