1 of 67

Introduction to Containers

2 of 67

Intro and Basics of Containerization

​

● What is a Container?

● Containerization vs. Virtual Machine

● Docker Installation

● Main Docker Commands

● Building your own Containerized Image for Flask Application

Push image to Docker Hub

Learn IBM CR

Push image to IBM container registry

​

3 of 67

Applications are transforming

4 of 67

Application Modernization

Developer Issues:

  • Minor code changes require full re-compile and re-test
  • Application becomes single point of failure
  • Application is difficult to scale

​

  • Microservices: Break application into

separate operations

  • 12-Factor Apps: Make the app Independently scalable, stateless, highly

available by design

​

5 of 67

Traditional Deployment

Early on, organizations ran applications on physical servers.

​

6 of 67

Demerits of Traditional Deployment

  • Consider multiple applications running on one server

​

  • There can be a time where one application may take up entire resource allocation

​

  • It may result in underutilization of resources and increase the cost

7 of 67

Overview of deployments

​

8 of 67

How virtualization works

Virtualization is a process whereby software is used to create an abstraction layer over computer hardware that allows the hardware elements of a single computer to be divided into multiple virtual computers.

​

9 of 67

Hypervisors

A hypervisor is the software layer that coordinates VMs. It serves as an interface between the VM and the underlying physical hardware, ensuring that each has access to the physical resources it needs to execute. It

​

10 of 67

11 of 67

12 of 67

13 of 67

14 of 67

15 of 67

Virtual Machine vs Containerization

  • VM’s are preferred as they have better resource utilization

​

  • Reduce the production cost and helps in better scalability compared to Traditional deployment
  • Containers are similar to VMs, but they have relaxed isolation properties to share the OS among the applications

​

  • They are easily portable and could work on any platform or environment

​

  • It can automatically allocate resources to the nodes that are free and saves much time

16 of 67

17 of 67

What is a container

  • Containers are lightweight software packages that contain all the dependencies required to execute the contained software application.

​

  • A way to package application with all necessary dependencies and configurations
  • Portable artifact ,easily shared and moved around
  • Share the same OS kernel

18 of 67

Before Containers

​

  • Installation process was different

​

  • OS environment.

​

​

  • Many Steps were something could go wrong.

Developer

Developer

Node js

Mongo Express

Node js

Mongo Express

19 of 67

After Containers

​

  • Own isolated environment

​

  • Packed with all needed packages

​

  • One command to install the App

​

  • Run same app with two different versions

Node Container

Node Container

20 of 67

21 of 67

22 of 67

23 of 67

24 of 67

Where do containers images live ?

​

Container Registry

​

Private Repositories

​

​

Public Repository for Docker (Docker Hub)

​

​

https://razorops.com/blog/best-docker-image-hosting-platforms/

25 of 67

Key Benefits of Docker Containers

26 of 67

What is Docker

The Docker platform it provides the ability to package and run an application in a loosely isolated environment called a container.

​

https://www.docker.com/

https://hub.docker.com/

​

​

27 of 67

What is Docker?

Docker is popular virtualization software that helps its users in developing, deploying, monitoring, and running applications in a Docker Container with all their dependencies.

Docker containers include all dependencies (frameworks, libraries, etc.) to run an application in an efficient and bug-free manner.

Docker Containers have the following benefits:

  • Light-weight
  • Applications run in isolation
  • Occupies less space
  • Easily portable and highly secure
  • Short boot-up time

​

28 of 67

Docker architecture

29 of 67

Docker Installation

  • Docker currently separates its product lines into two segments.
    • Community Edition (CE), which is closed-source yet completely free, and
    • Enterprise Edition (EE), which is also closed-source and needs to be licensed on a yearly basis
  • If you are using macOS or have Windows 10 Professional installed on your laptop, it is recommended that you install Docker for Desktop.
  • Docker for Desktop doesn’t support linux. You need to install it differently.

30 of 67

Steps to install Docker

  1. No matter what OS you're using, navigate to the Docker start page at https://www.docker.com/get-started.
  2. On the right-hand side of the loaded page, you'll find a big blue button saying Download Desktop and Take a Tutorial. Click this button and follow the instructions.
  3. You will be redirected to Docker Hub. If you don't have an account on Docker Hub yet, then create one. It is absolutely free, but you need an account to download the software. Otherwise, just log in.Once you're logged in, look out for the below image on the page:

31 of 67

Steps to install Docker

  1. Click the blue Download Docker Desktop button
  2. Note that if you're on a Windows PC, the blue button will say Download Docker Desktop for Windows instead.
  3. Once you have successfully installed Docker for Desktop for macOS, please open a Terminal window and execute the following command to check whether Docker is properly installed or not:

$ docker version

​

32 of 67

33 of 67

34 of 67

35 of 67

36 of 67

37 of 67

38 of 67

39 of 67

40 of 67

$ docker run -d -p 80:80 docker/getting-started

41 of 67

42 of 67

43 of 67

44 of 67

Docker main commands

  • To create and a run a demo docker container in your system. Type the below commands.

$ docker run -d -p 80:80 docker/getting-started

  • This command contains multiple parts. First and foremost, we have the word docker.
  • This is the name of the Docker Command-Line Interface (CLI) tool, which we are using to interact with the Docker engine that is responsible to run containers.
  • Next, we have the word container, which indicates the context we are working with. As we want to run a container, our context is the word container.
  • Next is the actual command we want to execute in the given context, which is run.

45 of 67

Starting and Stopping Containers

  • A container can be started again using the docker container start command

$ docker container start <CONTAINER_NAME>

  • To stop a container, then we can do so by issuing this command

$ docker container stop <CONTAINER_NAME>

  • Now, instead of using the container name, we can use the CONTAINER_ID in our expression:

$ docker container stop <CONTAINER_ID>

​

46 of 67

Listing Docker Containers

  • To find out what is currently running on our host, we can use the container ls command, as follows

$ docker container ls

  • By default, Docker outputs seven columns with the following meanings:
    • Container ID : This is the unique ID of the container. It is an SHA-256.
    • Image: This is the name of the container image from which this container is instantiated.
    • Command: This is the command that is used to run the main process in the container.
    • Created : This is the date and time when the container was created.
    • Status: This is the status of the container (created, restarting, running, removing, paused, exited, or dead).
    • Ports: This is the list of container ports that have been mapped to the host.
    • Names: This is the name assigned to this container (multiple names are possible).

47 of 67

Docker Commands

  • If we want to list not only the currently running containers but all containers that are defined on our system, then we can use the command-line parameter -a or --all, as follows:

$ docker container ls -a

  • This will list containers in any state, such as created, running, or exited.Sometimes, we want to just list the IDs of all containers. For this, we have the -q parameter:

$ docker container ls -q

  • You can invoke help for the list command as follows:

$ docker container ls -h

​

48 of 67

Inspecting and Running Containers

​

  • Containers are runtime instances of an image and have a lot of associated data that characterizes their behavior.
  • To get more information about a specific container, we can use the inspect command

$ docker container inspect <CONTAINER_NAME>

  • To run a docker container

$ docker container exec -t <CONTAINER_NAME> /bin/sh

​

49 of 67

Removing Containers

​

  • The command to remove a container is as follows:

$ docker container rm <container ID>

  • Another command to remove a container is the following:

$ docker container rm <container name>

​

50 of 67

Docker Images

​

  • In Linux, everything is a file. The whole operating system is basically a filesystem with files and folders stored on the local disk
  • An image is basically a file system with layers.
  • The layers of a container image are all immutable.
  • Immutable means that once generated, the layer cannot ever be changed

51 of 67

How to create a Docker Image

Docker Image is created using Dockerfile

52 of 67

Dockerfile

The specific commands you can use in a dockerfile are:

FROM, PULL, RUN, and CMD

  • FROM - Creates a layer from the OS ( like parent image )
  • PULL - Adds files from your Docker repository
  • RUN - runs command
  • CMD - Specifies what command to run within the container
  • ENTRYPOINT allows specifying a command along with the parameters
  • ADD command helps in copying data into a Docker image
  • ENV provides default values for variables that can be accessed within the container
  • EXPOSE instruction exposes a particular port with a specified protocol inside a Docker Container.

53 of 67

Dockerfile

A Dockerfile is simply a text-based script of instructions that is used to create a container image.

​

​

FROM python:3.10.6

WORKDIR /app

COPY requirements.txt ./

RUN pip install -r requirements.txt

COPY . .

EXPOSE 5000

CMD ["python","./app.py"]

​

54 of 67

From Flask app to docker flask

  1. Add Dockerfile

​

  • Add following to app.py

if __name__ == '__main__':

app.run(host='0.0.0.0', port=5000)

​

  • Requirements.txt

​

55 of 67

Command : docker build -t <image_name> .

Example : docker build -t hello-world .

Next docker image was created with name

For finding images of docker we use command

Command : docker image ls -a

For Delete image

Command : docker rmi <image_name>

​

56 of 67

Docker Commands

57 of 67

IBM CLOUD CLI

https://cloud.ibm.com/docs/cli?topic=cli-install-ibmcloud-cli

​

Install, Set Up, and Log In

1. Install the IBM Cloud CLI.

2. Install the Docker CLI.

3. Install the Container Registry plug-in.

​

58 of 67

59 of 67

60 of 67

61 of 67

62 of 67

63 of 67

64 of 67

65 of 67

Working with Docker hub —----------------------------------------

​

$ docker tag hello-world kshyam/hello-world:latest

$ docker push kshyam/hello-world:latest

$ docker pull kshyam/hello-world:latest

​

Working with IBM CR —-----------------------------------------------------------

ibmcloud login

ibmcloud plugin install container-registry

ibmcloud cr login

​

$ docker tag hello-world jp.icr.io/ibmtrainning/hello-world:latest

$ docker push jp.icr.io/ibmtrainning/hello-world:latest

$ docker pull jp.icr.io/ibmtrainning/hello-world:latest

66 of 67

Docker Issues over Windows

67 of 67

Take away

​

  1. Run Docker Desktop
  2. Able to run docker/getting-started locally

$ docker run -d -p 80:80 docker/getting-started

  • Able to build docker image for flask app
  • Able to run docker image or docker container form flask app
  • Able to push flask app docker image to docker hub
  • Able to push flask app image to IBM container registry

​

Next

  1. Should able to use these images for kubernetes deployment
  2. Able to run on public domain or ip

​

​