Docker: A Simple Intro

·

2 min read

What is Docker?

Docker is a tool that helps developers create, deploy, and run applications using containers. Containers package everything an application needs to work, so it runs the same way on any computer.

Why Use Docker?

  • Consistency: Works the same on different computers.

  • Lightweight: Uses fewer resources than virtual machines.

  • Fast Deployment: Quickly starts and stops applications.

  • Scalability: Easily scales up applications.

How Does Docker Work?

Docker uses images and containers:

  • Image: A blueprint for your application.

  • Container: A running instance of an image.

Installing Docker

  1. Go to Docker's official website.

  2. Download and install Docker suited for your OS.

  3. Verify installation using:

     docker --version
    

Basic Docker Commands

  • Check version:

      docker --version
    
  • List running containers:

      docker ps
    
  • List all containers (including stopped ones):

      docker ps -a
    
  • Download an image:

      docker pull <image-name>
    
  • Run a container:

      docker run <image-name>
    
  • Stop a running container:

      docker stop <container-id>
    
  • Remove a container:

      docker rm <container-id>
    
  • Remove an image:

      docker rmi <image-name>
    

Dockerfile: Automating Container Creation

A Dockerfile is a script that builds an image automatically.

Example Dockerfile:

# Use base image
FROM ubuntu:latest

# Set working directory
WORKDIR /app

# Copy files
COPY . .

# Install dependencies
RUN apt update && apt install -y curl

# Set the command to run
CMD ["echo", "Hello, Docker!"]

Building and Running a Custom Image

  1. Create a Dockerfile in your project folder.

  2. Build the image:

     docker build -t my-app .
    
  3. Run the container:

     docker run my-app
    

Docker Compose: Running Multiple Containers

Docker Compose allows you to define and run multiple containers using a docker-compose.yml file.

Example docker-compose.yml:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"

To run:

docker-compose up

Conclusion

Docker simplifies application deployment by packaging everything into containers. With Docker, you can run applications consistently across different environments. Learning Docker’s basic commands, Dockerfiles, and Docker Compose will help you get started with containerization easily.