EN_Virt_Docker - somaz94/DevOps-Engineer GitHub Wiki

Docker & Dockerfile & Docker Compose

2. Docker & DockerFile & Docker Compose

Docker is an open-source platform for deploying applications within software containers. It provides additional abstraction and automation for operating system-level virtualization on Linux.

Docker utilizes Linux kernel's resource isolation features (like cgroups and kernel namespaces) and union file systems (such as OverlayFS) to allow independent containers to run within a single Linux instance, thus avoiding the overhead of starting and maintaining virtual machines.

Main Features of Docker

  1. Containerization: Packages applications and their dependencies into containers, ensuring consistency across development, test, and production environments.
  2. Lightweight: Containers share the machine's OS kernel and do not require an OS for each application, making them lighter than virtual machines.
  3. Portability: Dockerized applications can run anywhere on any platform that supports Docker.
  4. Isolation: Ensures each container is isolated, having its own file system, networking, and process space.
  5. Docker Hub: A repository for Docker images where users can pull images and use them as the basis for their applications.

Dockerfile

A Dockerfile is a text document containing all commands a user can call from the command line to assemble an image. Using docker build, users can create automated builds that execute a series of command line instructions.

Basic Structure of Dockerfile
  • FROM: Specifies the base image.
  • RUN: Executes commands in a new layer above the current image and commits the results.
  • COPY: Copies new files, directories, or remote file URLs from src to the container's dest path.
  • CMD: Provides default values for running containers. Only the last CMD takes effect.
  • EXPOSE: Informs Docker that the container listens on specified network ports at runtime.
  • ENV: Sets an environment variable <key> to the value <value>.
  • ENTRYPOINT: Configures a container to run as an executable.
Example Dockerfile
# Use the official Python runtime as a parent image
FROM python:3.7-slim

# Set the working directory in the container
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"]
  • This Dockerfile creates a Python environment, sets the working directory, copies the current directory's contents, installs dependencies, exposes a port, sets an environment variable, and specifies the command to run on container startup.

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. You can configure your application's services, networks, and volumes using the docker-compose.yml file.

Example docker-compose.yml
version: "3"
services:
  web:
    image: "webapp:latest"
    ports:
      - "5000:5000"
  db:
    image: "postgres:latest"
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
  • The corresponding doker-compose.yml file defines web application services and postgreSQL database services. The web service runs on 5000 ports, and the db service is configured through environmental variables.

Reference

⚠️ **GitHub.com Fallback** ⚠️