Introduction

In this section, we will explore Linux containers and Docker, a popular platform for developing, shipping, and running applications inside containers. Containers provide a lightweight, portable, and efficient way to deploy applications consistently across different environments.

Key Concepts

What are Containers?

  • Containers are lightweight, standalone, and executable software packages that include everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings.
  • Containers share the host system's kernel but run in isolated user spaces.

Benefits of Containers

  • Portability: Containers can run consistently across different environments.
  • Efficiency: Containers are lightweight and use fewer resources compared to virtual machines.
  • Scalability: Containers can be easily scaled up or down to handle varying loads.
  • Isolation: Containers provide process and file system isolation, enhancing security.

Docker Overview

  • Docker is an open-source platform designed to automate the deployment, scaling, and management of containerized applications.
  • Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon to build, run, and manage containers.

Installing Docker

Step-by-Step Installation

  1. Update Package Index:

    sudo apt-get update
    
  2. Install Prerequisites:

    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
    
  3. Add Docker’s Official GPG Key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
  4. Set Up the Stable Repository:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    
  5. Install Docker:

    sudo apt-get update
    sudo apt-get install docker-ce
    
  6. Verify Docker Installation:

    sudo docker --version
    

Basic Docker Commands

Running a Container

  • Pull an Image:
    sudo docker pull hello-world
    
  • Run a Container:
    sudo docker run hello-world
    

Listing Containers

  • List Running Containers:
    sudo docker ps
    
  • List All Containers:
    sudo docker ps -a
    

Managing Containers

  • Stop a Container:
    sudo docker stop <container_id>
    
  • Remove a Container:
    sudo docker rm <container_id>
    

Managing Images

  • List Images:
    sudo docker images
    
  • Remove an Image:
    sudo docker rmi <image_id>
    

Creating a Dockerfile

Example Dockerfile

A Dockerfile is a text document that contains all the commands to assemble an image.

# Use an official Python runtime as a parent image
FROM python:3.8-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"]

Building and Running the Docker Image

  • Build the Image:
    sudo docker build -t my-python-app .
    
  • Run the Container:
    sudo docker run -p 4000:80 my-python-app
    

Practical Exercise

Exercise: Create and Run a Simple Web Server in a Docker Container

  1. Create a Directory for Your Project:

    mkdir my-web-server
    cd my-web-server
    
  2. Create a Simple Python Web Server:

    • Create a file named app.py:

      from flask import Flask
      app = Flask(__name__)
      
      @app.route('/')
      def hello_world():
          return 'Hello, World!'
      
      if __name__ == '__main__':
          app.run(host='0.0.0.0', port=80)
      
    • Create a file named requirements.txt:

      Flask
      
  3. Create a Dockerfile:

    FROM python:3.8-slim
    WORKDIR /app
    COPY . /app
    RUN pip install --no-cache-dir -r requirements.txt
    EXPOSE 80
    CMD ["python", "app.py"]
    
  4. Build the Docker Image:

    sudo docker build -t my-web-server .
    
  5. Run the Docker Container:

    sudo docker run -p 4000:80 my-web-server
    
  6. Access the Web Server:

    • Open a web browser and navigate to http://localhost:4000. You should see "Hello, World!".

Solution

The solution involves following the steps outlined in the exercise. Ensure that each command is executed correctly and verify the output at each stage.

Conclusion

In this section, we covered the basics of Linux containers and Docker, including installation, basic commands, and creating a Dockerfile. We also walked through a practical exercise to create and run a simple web server in a Docker container. This knowledge provides a solid foundation for working with containerized applications and leveraging Docker for efficient and consistent deployments.

© Copyright 2024. All rights reserved