Introduction to Docker and Docker Compose: Beginners Guide




What is Docker

Docker is an open source platform that enables developers and engineers to build, deploy, run and manage containers.

Containers are standardized, executable components that combine application source code, together with the operating System libraries and dependencies required to run that code in any environment.

containers enable multiple application components to share the resources of a single instance of the host Operating System.

Containerization Image



Why use Docker

  1. Consistency: Docker deals with the infamous headache of “It works on my machine” problem. This problem occurs when an application works on the developers laptop, but when the application is deployed in to a server or the cloud, something breaks. Docker helps to package everything the app need into a container Image. That container image will run the same way on any machine (laptop, staging server, production in cloud).
  2. Light Weight: Docker containers share the Host OS kernel. they don’t need to boot an entire OS each time like is the case with traditional Virtual Machines. As a result:
    -Containers start quickly.
    -Save cost on hardware and cloud resources.
    -You can run many containers on a single machine.
  3. Scalable: With docker you can run multiple containers of the same app behind a load balancer. You can add more containers (Scale up) when demand increases of remove container (scale down) when demand decreases.
  4. Fast Deployment: With docker, you build an image and to starting a new container is an automated and repeatable process.



Terms and Tools within docker Architecture

  1. Docker Host:- This is the physical or virtual machine running a Docker engine compatible Operating System such as the Linux.
  2. Docker Engine:’- It a client/server application that consist of the Docker Daemon, Docker API that interacts with the Daemon, and a Docker CLI that talks to the daemon.
  3. Docker Daemon:- This is a service that creates and manages docker images by using commands from the client.
  4. Docker client:-Provides the Command Line Interface (CLI) that accesses the Docker API to communicate with the Docker Daemon over a unix socket or a network interface.
  5. Docker Object:- components of a docker deployment that help package and distribute applications. They include Images, containers, network, plugins, and volumes.
  6. Container:- This is the live running instance of a docker Image.
  7. Docker Image:- Contain executable applications source code and all tools, libraries and dependencies the application code needs to run as a container.
  8. Docker Build:- a command that has tools and features for creating a docker image.
  9. Docker file:- A simple text file containing instructions for how to build the docker container image. You can say it is a list of instructions that the docker engine will run to assemble the docker image.
  10. Docker Hub this is a public repository of docker images.
    11 Docker Compose: is a tool to manage multiple container applications where all containers run on the same docker host.



Docker Installation

sudo apt update
sudo apt install docker.io -y
sudo systemctl enable docker --now
Enter fullscreen mode

Exit fullscreen mode

Verify Installation

docker version



Running your first container

docker run hello-world

👆Docker pulls the image from Docker Hub and runs it inside a container



Basic docker commands

# Pull an image from Docker Hub
docker pull ubuntu

# Run a container
docker run -it ubuntu bash

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Stop a container
docker stop 

# Remove a container
docker rm 

# Remove an image
docker rmi 


Enter fullscreen mode

Exit fullscreen mode



Building an Image

create a file called Dockerfile:

# Use Python base image
FROM python:3.9-slim

# Set working directory
WORKDIR /app

# Copy files
COPY . /app

# Install dependencies
RUN pip install flask

# Run the app
CMD ["python", "app.py"]

Enter fullscreen mode

Exit fullscreen mode

Build and run

docker build -t myapp .
docker run -p 5000:5000 myapp
Enter fullscreen mode

Exit fullscreen mode



Managing Multi-Container Applications using Docker-compose

In real projects , applications often need multiple services working together. For example:

  • A web application (Flask, Django or Node.js)
  • A database (PostgreSQL, MongoDB)
  • Cache (Reds)
    Running and connecting each container manually with docker run can get messy.
    ## Why use docker compose
    We use docker compose to define and manage multi-container applications using a single YAML file (docker-compose.yml)



To run docker compose, just run:

docker-compose up



Sample Docker Compose File

Here is a simple example: a Flask app with a PostgreSQL database.

version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db

  db:
    image: postgres:13
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydb
    ports:
      - "5432:5432"
Enter fullscreen mode

Exit fullscreen mode



How it works

  1. Web -> Your Flask app (built from the dockerfile in the current directory).
  2. db -> A PostgreSQL database running in its own container.
  3. depends_on -> Ensures the database starts befor the web app

Running docker compose
docker-compose up

👆This launches both containers (web + db)

To Stop them, run:
docker-compose down



Why docker compose is useful

  1. Simplifies running multiple containers.
  2. Keeps your setup reproducible and sharable.
  3. Handles networking automatically (services can talk to each other by names, e.g., db)



Source link

Leave a Reply

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