Introduction to Docker Containers

Oh Docker... let's get started.

Introduction to Docker containers - Learn

Docker overview

The problem containers solve

There's usually more than one team working on the success of your application. There's the development team that creates the application, and the operations team that take care of the deployment and management of it. Typically each team will have an environment they work in:

etc.

There are some challenges that occur because of this setup:

This is where containers come in to save the day.

What is a container?

A container is a loosely isolated environment that allows us to build and run software packages. These packages (called container images) include the code and all dependencies to run applications quickly and reliably on any computing environment.

The container image becomes the unit we use to distribute our applications.

The process of deploying and running our apps with containers is called containerization.

One of the strengths of containerization is that you don't have to configure hardware and spend time installing operating systems and software to host a deployment.

Since containers are isolated from each other, they help us improve the security of our application. Multiple containers can run on the same hardware, improving the efficiency of hardware use.

What is Docker?

Docker is the most popular containerization platform. We use it to develop, ship, and run containers.

Docker Architecture

https://docs.microsoft.com/learn/modules/intro-to-docker-containers/media/2-docker-architecture.svg

Docker Host

Docker Engine

The Docker Engine is configured as a client-server implementation where the client and server can run on the same host or on a remote one and communicate via the Docker REST API. Components making up the engine are:

Docker Hub

Docker Hub is a Docker container registry and it’s the default public registry Docker uses for image management. A container registry are repositories that we can use to store and distribute container images we create.

How Docker images work

What is a container image?

A container image is made up of:

This image, when run, becomes a container. An image is immutable, to apply changes to it you would have to create a new image.

A container image is an immutable package that contains all the application code, system packages, binaries, libraries, configuration files, and the operating system running in the container. Docker containers running on Linux share the host OS kernel and don't require a container OS as long as the binary can access the OS kernel directly.

What is the host OS?

The OS on which the Docker engine is running on is the host OS.

What is the container OS?

The container OS is the OS that is part of the container image. We can include different versions of Linux or Windows in a container and this allows us to access specific OS features.

https://docs.microsoft.com/learn/modules/intro-to-docker-containers/media/3-container-ubuntu-host-os.svg

It’s isolated from the host OS and is the environment in which we deploy and run our app. This isolation means the environment for our application running in development is the same as in production.

What is the Stackable Unification File System (Unionfs)

Unionfs: A Stackable Unification File System

Unionfs is a file system that allows you to stack several directories, called branches, in such a way that it appears as if the content is merged. It works on top of other file systems and came into existence because containers need a more efficient way to share physical memory segments than conventional file systems.

UnionFS : A File System of a Container

https://miro.medium.com/max/462/0*BhhgkPFuHnQhOcy0.jpg

Though the content appears merged, it is kept physically separate. Unionfs allows you to add and remove branches as you build out your file system.

Base image and parent image

Create a base image

What is a Dockerfile?

A Dockerfile is a text file that contains the instructions we use to build and run a Docker image. It defines:

# Step 1: Specify the parent image for the new image
FROM ubuntu:18.04

Step 2: Update OS packages and install additional software

RUN apt -y update && apt install -y wget nginx software-properties-common apt-transport-https
&& wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
&& dpkg -i packages-microsoft-prod.deb
&& add-apt-repository universe
&& apt -y update
&& apt install -y dotnet-sdk-3.0

Step 3: Configure Nginx environment

CMD service nginx start

Step 4: Configure Nginx environment

COPY ./default /etc/nginx/sites-available/default

STEP 5: Configure work directory

WORKDIR /app

STEP 6: Copy website code to container

COPY ./website/. .

STEP 7: Configure network requirements

EXPOSE 80:8080

STEP 8: Define the entry point of the process that runs in the container

ENTRYPOINT [“dotnet”, “website.dll”]

Each of these steps creates a cached container image as we build the final container image. These cached container images are layered on top of the previous and presented as a single image once all steps are complete (thanks to unionfs)

The ENTRYPOINT command indicates which process will execute once we run a container from an image.

How to manage Docker images

The Docker CLI allows us to manage images by building, listing, removing, and running them. The CLI sends all queries to the docerkd daemon.

How Docker containers work

How to manage Docker containers

A docker container has a lifecycle that you can manage and track the state of the container.

https://docs.microsoft.com/learn/modules/intro-to-docker-containers/media/4-docker-container-lifecycle.svg

How to view available containers

Why are containers given a name?

Use the --name flag to give a container an explicit name. Names are unique and enable us to run multiple container instances of the same image.

Docker container storage configuration

Always consider containers as temporary when thinking about storing data.

Containers can make use of volumes and bind mounts to persist data.

What is a volume?

What is a bind mound?

Docker container network configuration

Network configuration enables us to build and configure apps that can communicate securely with each other.

What is the bridge network?

The bridge network is the default configuration applied to containers when launched without specifying any additional network configuration.

Host network

The host network enables you to run the container on the host network directly. This effectively removes the isolation between the host and the container at a network level.

The container can use only ports not already used by the host.

host network configuration isn't supported for both Windows and macOS desktops.

None network

To disabled networking for containers, use the none network option.

When to use Docker containers

When not to use Docker containers?