Docker

containers and cranes at hamburg port

Docker is an open-source platform that simplifies application development, deployment, and management by using containers. Containers are lightweight, portable, and encapsulate an application and its dependencies, ensuring consistency across environments.

Key Features of Docker

1. Containerization: Package applications with their dependencies into containers.

2. Portability: Containers run the same on any system with Docker installed.

3. Efficiency: Lightweight containers use fewer resources compared to virtual machines.

4. Version Control: Track application builds and dependencies with Docker images.

5. Isolation: Each container runs in its own isolated environment.

Docker Components

1. Docker Engine:

• Core of Docker, responsible for building, running, and managing containers.

2. Docker Images:

• A read-only template for creating containers.

• Typically pulled from Docker Hub, the central repository.

3. Docker Containers:

• Running instances of images, isolated from the host system.

4. Docker Compose:

• A tool for defining and running multi-container applications.

5. Dockerfile:

• A script containing instructions for building Docker images.

Installing Docker

1. For Linux

1. Update your package index:

sudo apt update

2. Install prerequisites:

sudo apt 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 gpg –dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

4. Add Docker’s repository:

echo “deb [arch=$(dpkg –print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

5. Install Docker:

sudo apt update

sudo apt install docker-ce docker-ce-cli containerd.io

2. For Windows or macOS

• Download the Docker Desktop installer from Docker’s website.

• Follow the installation instructions.

Common Docker Commands

1. Start Docker Service

sudo systemctl start docker

2. Run a Container

docker run -d -p 80:80 nginx

• -d: Detached mode (runs in background).

• -p 80:80: Maps port 80 on the host to port 80 in the container.

3. List Running Containers

docker ps

4. Stop a Container

docker stop <container-id>

5. Pull an Image

docker pull ubuntu

6. Build an Image

Create a Dockerfile and build an image:

docker build -t my-image .

7. Remove Unused Images

docker image prune

Using Docker Compose

Install Docker Compose

1. Install via package manager (Linux):

sudo apt install docker-compose

2. Verify installation:

docker-compose –version

Sample Compose File

Create a docker-compose.yml:

version: ‘3’

services:

  web:

    image: nginx

    ports:

      – “8080:80”

  app:

    image: python:3.9

    volumes:

      – .:/code

    command: python /code/app.py

Run the application:

docker-compose up -d

Advanced Features

1. Docker Volumes: Persist data between container restarts.

docker volume create my-volume

docker run -v my-volume:/data my-container

2. Docker Networking: Connect containers to custom networks.

docker network create my-network

docker run –network my-network my-container

3. Docker Swarm: Orchestrate clusters of Docker nodes for high availability.

4. Docker Registry: Host your own image repository (e.g., private Docker Hub).

Let me know if you’d like guidance on a specific Docker use case or troubleshooting!

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.