DevOps
  • Introduction
    • About DevOps
    • Software Production
    • Software System
  • Terminal Programming
    • BASH - Basics Commands
    • BASH -Conditions and Loops
    • Some Examples
  • SOFTWARE TEAMS
    • Software Teams
      • Software Team Topologies
      • Effort-Cost Estimation
      • Technical Debt
      • Software Development Waste
    • Software Production Methods
      • SCRUM
      • KANBAN
      • WATER FALL
      • AGILE
    • Version Control
    • CAMS
  • CI/CD
    • Continuous Integration & Continuous Delivery
  • Cloud Platforms
  • Automation Strategies
  • PaaS - Containers
    • Docker
  • PaaS - Orchestration
    • Kubernetes
  • DevOps & DORA Metrics
  • Monitoring Tools
  • A Day in a DevOps
  • MLOps
Powered by GitBook
On this page
  • Major Components of Docker
  • How to Use Docker
  • Step 1: Install Docker
  • Step 2: Create a Dockerfile
  • Step 3: Build a Docker Image
  • Step 4: Run a Docker Container
  • Step 5: Manage Containers
  • Step 6: Push Images to a Registry
  • Step 7: Use Docker Compose
  • Steps to Build a Docker Image from Scratch
  • 1. Create a Dockerfile
  • 2. Choose a Base Image
  • 3. Add Files
  • 4. Run Commands
  • 5. Set Environment Variables
  • 6. Expose Ports
  • 7. Define the Command to Run
  • 8. Build the Image
  • 9. Verify the Image
  • 10. Run the Container
  • Example Dockerfile
  1. PaaS - Containers

Docker

PreviousPaaS - ContainersNextPaaS - Orchestration

Last updated 2 months ago

Docker is a powerful containerization platform that simplifies the process of building, shipping, and running applications. Here's an overview of its major components and how to use them:

Major Components of Docker

  1. Docker Client:

    • The Docker client is the interface through which users interact with the Docker daemon. It allows users to create, manage, and delete Docker objects like images, containers, networks, and volumes.

  2. Docker Daemon (Engine):

    • The Docker daemon is the core component that manages the creation, execution, and distribution of containers. It runs in the background and listens for Docker API requests.

  3. Docker Images:

    • Docker images are read-only templates used to build Docker containers. They contain all the necessary code, libraries, and configurations for an application.

  4. Docker Containers:

    • Containers are runtime instances of Docker images. They provide isolated environments for applications to run in, sharing the host OS kernel.

  5. Docker Registry:

    • Docker Registry stores Docker images. Docker Hub is the official public registry, but users can also use private registries for storing and sharing images.

  6. Docker Networking:

    • Docker networking allows containers to communicate with each other and the host system. It provides features like service discovery and load balancing.

How to Use Docker

Step 1: Install Docker

Step 2: Create a Dockerfile

Step 3: Build a Docker Image

  • docker build -t my-app .

Step 4: Run a Docker Container

  • docker run -d -p 8080:80 my-app

Step 5: Manage Containers

  • Use various Docker commands to manage your containers:

    • docker ps: List running containers.

    • docker stop: Stop a running container.

Step 6: Push Images to a Registry

  • docker tag my-app:latest <your-username>/my-app:latest
    docker push <your-username>/my-app:latest

Step 7: Use Docker Compose

  • services:
      web:
        build: .
        ports:
          - "8080:80"

    Run your application with:

    docker-compose up

By following these steps, you can effectively use Docker to containerize your applications, ensuring consistent and reliable deployments across different environments.

Building a Docker image from scratch involves creating a Dockerfile that outlines the steps to build your image. Here's a step-by-step guide on how to do it:

Steps to Build a Docker Image from Scratch

1. Create a Dockerfile

  • Start by creating a new text file named Dockerfile. This file contains instructions for building your Docker image.

2. Choose a Base Image

  • Use the FROM instruction to specify a base image. If you want complete control over the image contents, you can use FROM scratch. Otherwise, choose an official or verified base image from Docker Hub.

    FROM scratch

    or

    FROM ubuntu:20.04

3. Add Files

  • Use the ADD or COPY instruction to add files to your image. ADD can also handle URLs and tarballs, while COPY is more straightforward for local files.

    ADD hello /

    or

    COPY index.html /app/

4. Run Commands

  • Use the RUN instruction to execute commands during the build process, such as installing dependencies.

    RUN apt-get update && apt-get install -y python3

5. Set Environment Variables

  • Use the ENV instruction to set environment variables that will be available during both build and runtime.

    ENV NAME World

6. Expose Ports

  • Use the EXPOSE instruction to specify ports that your application will use.

    EXPOSE 80

7. Define the Command to Run

  • Use the CMD instruction to specify the default command to run when the container starts.

    CMD ["python", "app.py"]

8. Build the Image

  • Navigate to the directory containing your Dockerfile and run the following command to build the image:

    docker build -t my-image .
    • -t specifies the tag for your image.

    • . indicates that the Dockerfile is in the current directory.

9. Verify the Image

  • Use the docker images command to list all available images and verify that your new image is listed.

    docker images

10. Run the Container

  • Use the docker run command to start a new container from your image.

    docker run -d --name my-container -p 8080:80 my-image

Example Dockerfile

Here's a simple example of a Dockerfile that builds an image for a Python application:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

By following these steps, you can create a Docker image from scratch that encapsulates your application and its dependencies, ensuring consistent and reliable deployments across different environments.

Download and install Docker Desktop from the official Docker website. Follow the installation instructions for your operating system.

A Dockerfile is a script that contains instructions for building a Docker image. It specifies the base image, copies files, sets environment variables, and defines commands to run during the build process.

Use the docker build command to create an image from your Dockerfile. This command compiles the instructions in the Dockerfile into a usable image.

Use the docker run command to start a new container from the image you built. You can specify options like port mapping and detached mode.

docker rm: Remove a stopped container.

Use the docker push command to share your images with others or deploy them to different environments.

For applications with multiple containers, use Docker Compose to manage and orchestrate them. Create a docker-compose.yml file to define services and their dependencies.

1
3
1
3
1
3
1
3
1
2
3
4
4
6
5
6
6
6
4