Docker and Docker Compose

https://github.com/heig-vd-dai-course

Web · PDF

L. Delafontaine and H. Louis, with the help of GitHub Copilot.

This work is licensed under the CC BY-SA 4.0 license.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Objectives

  • Learn the differences between bare metal, virtualization and containerization
  • Learn how the OCI specification defines images, containers, and registries
  • Learn how to use Docker and Docker Compose to build, publish, and run applications in containers
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Prepare and setup your environment

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Install Docker and Docker Compose

  • Install Docker and Docker Compose
  • Configure Docker and Docker Compose to:
    • Run without sudo (root)
    • Start automatically at boot
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Check and run the code examples

  • Check the code examples
  • Run the code examples
  • Helps to understand the concepts
  • Modify/play with the code examples
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Bare metal, virtualization and containerization

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Bare metal, virtualization and containerization

  • Bare metal: software runs directly on hardware
  • Virtualization: software runs on a virtual machine
  • Containerization: software runs in a container
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Bare metal

  • The traditional way to run software
  • Software runs directly on hardware
  • Software has full access to the hardware
  • Security issues, hard to maintain, hard to migrate
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Virtualization

  • Virtualization runs virtual machines
  • A virtual machine is complete operating system
  • A virtual machine is isolated from the host
  • Virtual machines are heavy and use a lot of resources
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Containerization

  • Containerization starts containers
  • Containers contain all the dependencies to run the software
  • Containers are isolated from each other
  • Containers are lightweight and use the host kernel
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

OCI, images, containers, and registries

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

OCI, images, containers, and registries

  • Image: read-only template for container creation
  • Container: runnable instance of an image
  • Registry: service storing images
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Docker Hub

  • The official registry
  • Hosts millions of images
  • Can be used to store and share images
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

GitHub Container Registry

  • GitHub's registry
  • Hosts images in the same place as the code
  • Will be used in this course for simplicity
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Docker

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Docker

  • Created in 2013
  • Container engine
  • Composed of two parts:
    • Docker daemon (background process)
    • Docker CLI
  • Can be used to build, run and publish containers
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Dockerfile specification

  • Build a Docker image
  • Based on an existing image
  • Defines a set of instructions to build the image
  • Written in plain text
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Code examples

Check the code examples in the heig-vd-dai-course-code-examples Git repository:

  • Basic Dockerfile
  • Dockerfile with command
  • Dockerfile with entrypoint and command
  • Dockerfile with run and copy commands
  • Dockerfile with build arguments
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Summary

  • Docker is a container engine composed of two parts: the Docker daemon and the Docker CLI
  • The Docker CLI is used to manage containers and images
  • The Dockerfile specification defines a standard for building Docker images
  • A Dockerfile is used to build a Docker image
  • A Docker image is used to create a container
  • A container is a runnable, isolated, instance of an image
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Docker Compose

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Docker Compose

  • Can be used to deploy a multi-container application
  • Can be committed with the application
  • Can be used to deploy the application on any Docker host
  • Easy to use
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Docker Compose specification

  • Defines the application
    • Services: containers
    • Volumes: shared directories
    • Networks: network communication
  • Written in YAML
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Code examples

Check the code examples in the heig-vd-dai-course-code-examples Git repository:

  • Basic Docker Compose
  • Docker Compose with ports
  • Docker Compose with volumes
  • Docker Compose with environment variables
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Summary

  • Docker Compose allows to define a multi-container Docker application in a Docker Compose file
  • A Docker Compose file can consist of a set of services, volumes and networks
  • A Docker Compose file (docker-compose.yaml) can be easily shared and versioned with the application
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Make containers communicate with each other using Docker networks

More details for this section in the course material. You can find other resources and alternatives as well.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Make containers communicate with each other using Docker networks

Docker networks allow containers to communicate with each other. Each container can be connected to one or more networks.

Check the code examples in the heig-vd-dai-course-code-examples Git repository:

  • Make two containers communicate with each other with Docker
  • Make two containers communicate with each other with Docker Compose
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Questions

Do you have any questions?

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Practical content

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

What will you do?

Containerize the previous Java IOs project:

  • Create the Dockerfile and Docker Compose files
  • Publish on GitHub Container Registry
  • Run it on any Docker host
HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Find the practical content

You can find the practical content for this chapter on GitHub.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Finished? Was it easy? Was it hard?

Can you let us know what was easy and what was difficult for you during this chapter?

This will help us to improve the course and adapt the content to your needs. If we notice some difficulties, we will come back to you to help you.

➡️ GitHub Discussions

You can use reactions to express your opinion on a comment!

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

What will you do next?

We are arriving at the end of the first part of the course.

An evaluation will be done to check your understanding of all the content seen in this first part.

More details will be given in the next chapter.

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0

Sources

HEIG-VD - DAI Course 2024-2025 - CC BY-SA 4.0