About Docker

Index

Docker Overview

Docker is a platform for developing, shipping, and running applications inside containers. Containers are lightweight, standalone, executable packages that include everything needed to run a piece of software, including the code, runtime, system tools, system libraries, and settings.

Benefits of using Docker:

  • Consistency: Ensures your application runs the same way in any environment (development, testing, production).
  • Portability: Easily move applications between different infrastructures.
  • Isolation: Containers isolate applications from each other and the host system, improving security and stability.
  • Resource Efficiency: Containers share the host OS kernel, making them lightweight and efficient.

Core Concepts:

  • Images: Read-only templates that contain the instructions for creating a container.
  • Containers: Runnable instances of an image.
  • Dockerfile: A text file that contains the instructions for building a Docker image.
  • Docker Hub: A public registry for sharing Docker images.

Install docker & docker compose

Here’s how to install Docker and Docker Compose on different operating systems:

Ubuntu:

  1. Update the package index: sudo apt update
  2. Install Docker: sudo apt install docker.io
  3. Verify the installation: sudo docker run hello-world
  4. Install Docker Compose: sudo apt install docker-compose
  5. Verify the installation: docker-compose --version

macOS:

  1. Download Docker Desktop from https://www.docker.com/products/docker-desktop
  2. Install Docker Desktop.
  3. Docker Compose is included with Docker Desktop.

Windows:

  1. Download Docker Desktop from https://www.docker.com/products/docker-desktop
  2. Install Docker Desktop.
  3. Docker Compose is included with Docker Desktop.

For more detailed instructions, refer to the official Docker documentation: https://docs.docker.com/get-docker/

Get Started

Let’s run a simple “Hello World” container:

  1. Create a file named hello.txt with the following content:

    1
    Hello, world!
  2. Build a Docker image from a Dockerfile:

    Create a file named Dockerfile (without any extension) with the following content:

    1
    2
    3
    FROM alpine:latest
    COPY hello.txt /
    CMD ["cat", "/hello.txt"]
  3. Build the image:

    1
    docker build -t hello-world .
  4. Run the container:

    1
    docker run hello-world

    This will print “Hello, world!” to the console.

This example demonstrates the basic workflow of building and running a Docker container.

Static page with docker

Here’s how to create a Dockerfile for a static website:

  1. Create a directory for your static website:

    1
    2
    mkdir my-static-website
    cd my-static-website
  2. Create an index.html file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <!DOCTYPE html>
    <html>
    <head>
    <title>My Static Website</title>
    </head>
    <body>
    <h1>Hello, world!</h1>
    </body>
    </html>
  3. Create a Dockerfile:

    1
    2
    FROM nginx:latest
    COPY index.html /usr/share/nginx/html/
  4. Build the image:

    1
    docker build -t my-static-website .
  5. Run the container:

    1
    docker run -d -p 80:80 my-static-website

    Now you can access your website at http://localhost.

Springboot with docker

Here’s how to dockerize a Spring Boot application:

  1. Create a Spring Boot application (if you don’t have one already).

  2. Create a Dockerfile:

    1
    2
    3
    4
    5
    FROM openjdk:17-jdk-slim
    WORKDIR /app
    COPY target/*.jar app.jar
    EXPOSE 8080
    ENTRYPOINT ["java", "-jar", "app.jar"]
  3. Build the Spring Boot application:

    1
    mvn clean install
  4. Build the Docker image:

    1
    docker build -t my-springboot-app .
  5. Run the container:

    1
    docker run -d -p 8080:8080 my-springboot-app

    Now you can access your Spring Boot application at http://localhost:8080.