Docker - Introduction


What is Docker?

Docker is an open-source platform designed to automate the deployment, scaling, and management of applications in lightweight containers. Containers enable developers to package applications with all necessary dependencies, ensuring consistent behavior across different environments.


Why Use Docker?

Docker provides several benefits that make it an essential tool for modern application development and deployment:


Key Features of Docker

Docker offers several key features that make it a popular choice for developers and organizations:

Feature Description
Portability Containers can run on any system that supports Docker, ensuring consistency across environments.
Isolation Each container operates in its isolated environment, minimizing conflicts between applications.
Resource Efficiency Containers are lightweight and share the host OS kernel, allowing for efficient resource usage.
Scalability Docker enables easy scaling of applications through its orchestration tools like Docker Swarm.
Version Control Docker images can be versioned and managed through Docker Hub or private registries.
Security Containers provide a layer of security by isolating applications from the host system.

1. Introduction to Docker Architecture

Docker uses a client-server architecture consisting of the following components:


2. How Docker Works

Docker simplifies the process of creating, deploying, and running applications by using containers. Here's how it works:

  1. Create a Dockerfile: Define the environment and dependencies for your application in a Dockerfile.
  2. Build an Image: Use the docker build command to create an image from the Dockerfile.
  3. Run a Container: Deploy a container using the docker run command, specifying the image and any necessary parameters.
  4. Manage Containers: Start, stop, and manage containers using Docker commands like docker start, docker stop, and docker ps.

3. Installing Docker

Docker can be installed on various operating systems. Below are the installation steps for Windows, macOS, and Linux:

On Windows

  1. Download Docker Desktop for Windows from the Docker website.
  2. Run the installer and follow the prompts to complete the installation.
  3. Launch Docker Desktop and ensure it is running.

On macOS

  1. Download Docker Desktop for Mac from the Docker website.
  2. Open the downloaded package and drag Docker to the Applications folder.
  3. Launch Docker Desktop and ensure it is running.

On Linux

  1. Update your package index:
    sudo apt-get update
  2. Install Docker’s package dependencies:
    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. Add the Docker repository to APT sources:
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  5. Update your package index again and install Docker:
    sudo apt-get update
    sudo apt-get install docker-ce
  6. Verify that Docker is installed correctly:
    sudo docker run hello-world

4. Basic Docker Commands

Familiarizing yourself with basic Docker commands will help you manage images and containers effectively:

Command Description
docker --version Check Docker version.
docker build . Build an image from the Dockerfile in the current directory.
docker images List all Docker images on your system.
docker run IMAGE_NAME Run a container from the specified image.
docker ps List running containers.
docker stop CONTAINER_ID Stop a running container.
docker rm CONTAINER_ID Remove a stopped container.
docker rmi IMAGE_ID Remove a Docker image.

5. Example: Creating a Simple Docker Container

Let's create a simple Node.js application and run it in a Docker container.


Step 1: Create a Node.js Application

  1. Create a directory for your application and navigate into it:
    mkdir my-node-app
    cd my-node-app
  2. Create a package.json file:
    {
      "name": "my-node-app",
      "version": "1.0.0",
      "description": "A simple Node.js application",
      "main": "app.js",
      "scripts": {
        "start": "node app.js"
      },
      "dependencies": {
        "express": "^4.17.1"
      }
    }
  3. Create an app.js file:
    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
      res.send('Hello, Docker!');
    });
    
    app.listen(3000, () => {
      console.log('App listening on port 3000');
    });

Step 2: Create a Dockerfile

Create a Dockerfile in the same directory:

# Use the official Node.js image as the base image
FROM node:14

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the package.json and package-lock.json files
COPY package*.json ./

# Install the application dependencies
RUN npm install

# Copy the application code
COPY . .

# Expose the application port
EXPOSE 3000

# Define the command to run the application
CMD ["npm", "start"]

Step 3: Build the Docker Image

Run the following command to build the Docker image:

docker build -t my-node-app .

Step 4: Run the Docker Container

Run the container using the built image:

docker run -p 3000:3000 my-node-app

Visit http://localhost:3000 in your web browser to see the running application.


Summary

Docker is a powerful tool that simplifies the development and deployment of applications by providing a consistent environment. With its lightweight containers, Docker helps developers ensure their applications run seamlessly across different systems, making it an essential tool in modern software development.