Introduction to Docker

Docker is an open-source platform that automates the deployment, scaling, and management of applications inside lightweight containers. Containers package an application and its dependencies into a single executable unit, ensuring that the application runs consistently across different environments.

Key Concepts

1. Containers: Lightweight, portable, and self-sufficient environments that include everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings.

2. Images: Immutable snapshots of containers that can be used to create new containers. Images are created from a set of instructions written in a `Dockerfile`.

3. Dockerfile: A text file that contains a series of commands used to build a Docker image.

4. **Docker Hub**: A public registry where Docker images are stored and shared. You can pull pre-built images or push your images to Docker Hub.

## Setting Up Docker

### Prerequisites

1. A machine with a supported operating system (Linux, macOS, or Windows).

2. Admin/root privileges on the machine to install Docker.

Installation Steps

For Windows

1. **Download Docker Desktop**: Go to [Docker Desktop for Windows](https://desktop.docker.com/win/stable/Docker%20Desktop%20Installer.exe) and download the installer.

2. **Install Docker Desktop**: Run the installer and follow the on-screen instructions.

3. **Verify Installation**: Open a terminal (Command Prompt or PowerShell) and run:

  “`sh

  docker –version

  “`

  This should display the installed Docker version.

#### For macOS

1. **Download Docker Desktop**: Go to [Docker Desktop for Mac](https://desktop.docker.com/mac/stable/Docker.dmg) and download the installer.

2. **Install Docker Desktop**: Open the downloaded `.dmg` file and drag Docker to your Applications folder.

3. **Start Docker Desktop**: Open Docker from your Applications folder.

4. **Verify Installation**: Open a terminal and run:

  “`sh

  docker –version

  “`

#### For Linux

1. **Update Packages**: Update your existing list of packages with:

  “`sh

  sudo apt update

  “`

2. **Install Prerequisites**:

  “`sh

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

  “`

3. **Add Docker’s Official GPG Key**:

  “`sh

  curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –

  “`

4. **Set Up the Stable Repository**:

  “`sh

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

  “`

5. **Install Docker Engine**:

  “`sh

  sudo apt update

  sudo apt install docker-ce

  “`

6. **Verify Installation**:

  “`sh

  sudo systemctl status docker

  “`

## Using Docker

### Basic Commands

1. **Run a Container**: 

  “`sh

  docker run hello-world

  “`

  This command runs a test container to ensure Docker is installed correctly.

2. **List Running Containers**:

  “`sh

  docker ps

  “`

3. **List All Containers**:

  “`sh

  docker ps -a

  “`

4. **Stop a Container**:

  “`sh

  docker stop <container_id>

  “`

5. **Remove a Container**:

  “`sh

  docker rm <container_id>

  “`

6. **Pull an Image**:

  “`sh

  docker pull <image_name>

  “`

### Creating a Docker Image

1. **Create a Dockerfile**: In your project directory, create a file named `Dockerfile` and add the following content:

  “`Dockerfile

  # Use an official Python runtime as a parent image

  FROM python:3.8-slim

  # Set the working directory

  WORKDIR /usr/src/app

  # Copy the current directory contents into the container at /usr/src/app

  COPY . .

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

  “`

2. **Build the Image**:

  “`sh

  docker build -t my-python-app .

  “`

3. **Run the Image as a Container**:

  “`sh

  docker run -p 4000:80 my-python-app

  “`

### Example: Running a Simple Web Server

1. **Create a Project Directory**:

  “`sh

  mkdir my-web-server

  cd my-web-server

  “`

2. **Create a Python Application**:

  Create a file named `app.py` with the following content:

  “`python

  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)

  “`

3. **Create a `requirements.txt` File**:

  “`sh

  Flask==2.0.1

  “`

4. **Create a `Dockerfile`** (as shown above).

5. **Build and Run the Docker Image**:

  “`sh

  docker build -t my-web-server .

  docker run -p 4000:80 my-web-server

  “`

6. **Access the Application**:

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

## Conclusion

Docker simplifies the process of developing, deploying, and running applications by using containerization. By following the steps above, you can install Docker on your system, create Docker images, and run containers to host your applications. Docker also enables you to share your applications and dependencies with others, ensuring consistency across different environments.

Leave a comment

Your email address will not be published. Required fields are marked *