Module 3 - Containers Vs Virtualization (Docker)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Module 3:

Containers vs Virtualisation
Introduction to Docker

www.newpharma-development.ro
• Containers vs VMs
• What is Docker
Agenda • How to use Docker
• What is Dockerfile
• Containers Orchestration

www.newpharma-development.ro
www.newpharma-development.ro
• Virtualization is the process in which a system singular
resource like RAM, CPU, Disk, or Networking can be
‘virtualized’ and represented as multiple resources.

www.newpharma-development.ro
Containerization is the packaging of software code with just
the operating system (OS) libraries and dependencies required to
run the code.

Server virtualization is about abstracting hardware and running


an operating system. Containerization is about abstracting an
operating system and running an app.

www.newpharma-development.ro
Bare Metal vs Virtualization vs Containers

www.newpharma-development.ro
What is Docker ?

www.newpharma-development.ro
• Docker is an open-source platform for building, deploying, and
managing containerized applications.

www.newpharma-development.ro
• Dockerfile – simple text file containing instructions for how to
build a docker image
• Docker Image – contains executable application source code, all
the tools, libraries and dependencies that the application code
needs to run as container
• Docker Container – live, running instance of a docker image
• Docker Hub – Docker's official cloud-based registry for docker
images
• Docker Registry – scalable storage and distribution system for
docker images

www.newpharma-development.ro
How to install Docker ?

www.newpharma-development.ro
www.newpharma-development.ro
Docker Usage

www.newpharma-development.ro
Docker Images
• read-only templates with instructions to create a docker container
• can be pulled from a Docker Hub repository
• create your own docker images using a dockerfile

www.newpharma-development.ro
Docker Image Commands

Command Description

docker image build Build an image from a Dockerfile

docker image history Show the history of an image

docker image inspect Display detailed information on one or more images

docker image ls List images

docker image prune Remove unused images

docker image pull Pull an image or a repository from a registry

docker image push Push an image or a repository to a registry

docker image rm Remove one or more images

docker image tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

www.newpharma-development.ro
Docker Containers
• environments in which you run applications
• defined by the image and any additional configuration options provided
on starting the container

www.newpharma-development.ro
Docker Container Commands
Command Description

Attach local standard input, output, and error streams to a


docker container attach
running container

docker container create Create a new container

docker container exec Run a command in a running container

docker container inspect Display detailed information on one or more containers

docker container logs Fetch the logs of a container

docker container ls List containers

docker container restart Restart one or more containers

docker container rm Remove one or more containers

docker container start Start one or more stopped containers

docker container stop Stop one or more running containers

www.newpharma-development.ro
Docker Volumes
• preferred mechanism for persisting data generated by and used by
Docker containers
•docker volume create – create a new volume
•docker volume ls – list all the volumes
•docker volume inspect – returns information about a volume

www.newpharma-development.ro
Start a container with a volume

--mount -v or --volume
docker run -d \ docker run -d \
--name devtest \ --name devtest \
--mount source=myvol2,target=/app \ -v myvol2:/app \
nginx:latest nginx:latest

www.newpharma-development.ro
Docker Network
• Docker networking is primarily used to establish
communication between Docker containers and the outside
world via the host machine where the Docker daemon is
running.
Command Description

docker network connect Connect a container to a network

docker network create Create a network

docker network disconnect Disconnect a container from a network

Display detailed information on one or more


docker network inspect
networks

docker network ls List networks

docker network prune Remove all unused networks

docker network rm Remove one or more networks

www.newpharma-development.ro
Repository refers to the hosted collection of the images creating
the file system for containers.
Registry refers to the host containing the repositories and
providing an HTTP API that helps manage the repositories.
• docker login: To log in to a registry.
• docker logout: To log out from a registry.
• docker search: It will search the registry for the image.
• docker pull: It will pull an image from the registry to the local
machine.
• docker push: It will push an image to the registry from the local
machine.

www.newpharma-development.ro
• What is a Dockerfile?
Usages for a Dockerfile
Dockerfile •
• Basic syntax
• Example of a Dockerfile
• Resources

www.newpharma-development.ro
What is a Dockerfile?

• It’s a text document that can be edited by any


text editor (Sublime, Notepad++, VSCode, etc.)

• It contains commands/instructions that help to create and build an


image

www.newpharma-development.ro
Dockerfile usage

• Ease of use and sharing with other team members

• Customization of pre-build docker images

• Version control of Docker images used in projects

• Automation of building images using CI/CD tools

www.newpharma-development.ro
Basic syntax

There are few Dockerfile instructions that are used more


frequently, such as:
• FROM
• WORKDIR
• RUN
• COPY
• EXPOSE
• VOLUME

www.newpharma-development.ro
Basic syntax

• All Dockerfile files require the <FROM> keyword


• Using the <FROM> keyword we set the base image for subsequent
instructions
• Using the <FROM> keyword we can retrieve
the intended images (Apche, Nginx, Python, PHP, etc.) from the
docker registry (local or from hub.docker.com)

www.newpharma-development.ro
Basic syntax

• The <WORKDIR> instruction sets the working directory for any RUN,
CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the
Dockerfile

www.newpharma-development.ro
Basic syntax

• The <RUN> instruction executes commands within the container at


build time

• All executed commands are committed to the final build image

www.newpharma-development.ro
Basic syntax

• The <COPY> instruction copies new files or directories from <src> and
adds them to the filesystem of the container at the path <dest>

• The <src> path must be inside the directory where the image is build,
executing COPY ../something /something will fail

www.newpharma-development.ro
Basic syntax

• The <EXPOSE> instruction informs Docker that the container listens on


the specified network ports at runtime.

• To publish the port when running the container, use the -p flag
on docker run

www.newpharma-development.ro
Basic syntax

• The <VOLUME> creates a mount point with the specified name


where data from the container is stored

• It can map local folder to folders located in the docker


container

• A data volume is a specially-designated directory within one or more


containers

www.newpharma-development.ro
Example of a basic Dockerfile

www.newpharma-development.ro
Resources

For additional information, consult the following official links:


• Dockerfile reference
• Dockerfile best practices
• Create you own base image

www.newpharma-development.ro
Containers
Orchestration

www.newpharma-development.ro
Container orchestration is all about managing the lifecycles of
containers.

Docker – when you want to deploy a single (network


accessible) container
Docker Compose – when you want to deploy multiple
containers to a single host from within a single YAML file
Docker Swarm – when you want to deploy a cluster of docker
nodes (multiple hosts) for a simple, scalable application
Kubernetes – when you need to manage a large deployment
of scalable, automated containers

www.newpharma-development.ro
Docker Compose is a tool for defining and running multi-container
Docker applications.
Installation:
• Install Docker Desktop
• Install Compose plugin
Ex(Debian/Ubuntu): sudo apt-get install docker-compose-plugin
• Install Compose Standalone
https://docs.docker.com/compose/install/other/

www.newpharma-development.ro
Docker Compose works by applying many rules declared within a
single docker-compose.yml configuration file.

version: "3.9"
services:
...
volumes:
...
networks:
...

www.newpharma-development.ro
Command Description

docker compose build Build or rebuild services

docker compose down Stop and remove containers, networks

docker compose exec Execute a command in a running container.

docker compose logs View output from containers

docker compose ls List running compose projects

docker compose pause Pause services

docker compose ps List containers

docker compose stop Stop services

docker compose up Create and start containers

www.newpharma-development.ro
Resources
• https://docker-curriculum.com/
• https://www.docker.com/101-tutorial/
• https://docs.docker.com/
• https://docs.docker.com/engine/install/

www.newpharma-development.ro
Homework
• Install Docker Engine on Ubuntu 22 Machine
• Create a docker container which will execute the bash
script from previous homework

www.newpharma-development.ro
Slack: https://tinyurl.com/e6hjkjsm

Email: [email protected]

www.newpharma-development.ro
Thank you!

www.newpharma-development.ro

You might also like