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
-
Update Package Index:
sudo apt-get update
-
Install Prerequisites:
sudo apt-get 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 apt-key add -
-
Set Up the Stable Repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
-
Install Docker:
sudo apt-get update sudo apt-get install docker-ce
-
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
-
Create a Directory for Your Project:
mkdir my-web-server cd my-web-server
-
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
-
-
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"]
-
Build the Docker Image:
sudo docker build -t my-web-server .
-
Run the Docker Container:
sudo docker run -p 4000:80 my-web-server
-
Access the Web Server:
- Open a web browser and navigate to
http://localhost:4000
. You should see "Hello, World!".
- Open a web browser and navigate to
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.
Linux Mastery: From Beginner to Advanced
Module 1: Introduction to Linux
Module 2: Basic Linux Commands
- Introduction to the Command Line
- Navigating the File System
- File and Directory Operations
- Viewing and Editing Files
- File Permissions and Ownership
Module 3: Advanced Command Line Skills
- Using Wildcards and Regular Expressions
- Piping and Redirection
- Process Management
- Scheduling Tasks with Cron
- Networking Commands
Module 4: Shell Scripting
- Introduction to Shell Scripting
- Variables and Data Types
- Control Structures
- Functions and Libraries
- Debugging and Error Handling
Module 5: System Administration
- User and Group Management
- Disk Management
- Package Management
- System Monitoring and Performance Tuning
- Backup and Restore
Module 6: Networking and Security
- Network Configuration
- Firewall and Security
- SSH and Remote Access
- Intrusion Detection Systems
- Securing Linux Systems
Module 7: Advanced Topics
- Virtualization with Linux
- Linux Containers and Docker
- Automating with Ansible
- Linux Kernel Tuning
- High Availability and Load Balancing