ADVENTIST
UNIVERSITY OF
CENTRAL AFRICA
BEST PROGRAMMING AND DESIGN PATTERN
DOCKER and CONTAINER
What is a Docker
• Docker is a tool for running application in isolated environment.
• It is similar to virtual machine
• App run in same environment
• Docker is a platform and a tool used for containerization, which is a
method of packaging, distributing, and running applications and their
dependencies in a consistent environment.
• In Docker, containers are lightweight, standalone, executable
packages that include everything needed to run a piece of software,
such as the code, runtime, system tools, system libraries, and settings.
Cont’d
• Docker containers isolate software from its surroundings, ensuring
that it will run consistently regardless of the environment in which it is
deployed.
• Docker provides a way to package and distribute applications along
with their dependencies into a container image, which can then be
deployed and run on any Docker-enabled system.
• This makes it easier to build, ship, and run applications across
different environments, from development to testing to production.
Cont’d
• Overall, Docker simplifies the process of building, shipping, and
running applications by providing a standardized way to package and
deploy software in containers.
Containerization
• Containerization is a method of packaging, distributing, and running
applications and their dependencies in isolated environments called
containers.
• Containers encapsulate an application and all its dependencies,
including libraries, frameworks, and configuration files, into a single
package.
• This package can then be easily deployed across different computing
environments, such as development, testing, and production, without
worrying about differences in underlying infrastructure.
Cont’d
• One popular technology for containerization is Docker, which allows
developers to create, deploy, and manage containers efficiently.
• Containers provide several benefits, including consistency across
environments, faster deployment times, scalability, and resource
efficiency.
Dcoker vs VM
Benefits of using Docker
• Run container in seconds instead of minutes
• Less resources results less disk space
• Uses less Memory
• Does not need full OS
• Deployment
• Testing
What are the components of
container ?
• The content of containerization typically includes:
1. Application codes
2. Dependences
3. Operating system
4. Configuration files
Cont’d
• Application Code: The primary content of a container is the
application code itself. This includes all the source code files, scripts,
binaries, and other assets required to run the application.
• Dependencies: Containers encapsulate all the dependencies required
by the application to run, such as libraries, frameworks, runtime
environments, and configuration files. These dependencies are often
packaged together with the application code to ensure portability and
consistency across different environments
Cont’d
• Operating System Components: While containers share the host
operating system's kernel, they may include certain operating system
components or configurations specific to the application's runtime
environment. For example, a containerized application may require
specific versions of system libraries or runtime environments.
• Configuration Files: Containers often include configuration files
necessary for the application to run correctly.
This may include environment variables, database connection strings,
network configurations, and other settings that define the behavior of the
application.
Why Docker
• Standardization of Development Environments: Before Docker,
developers often faced challenges when deploying applications to
different environments due to differences in underlying infrastructure
and configurations.
• Docker standardized the process by packaging applications and their
dependencies into portable containers, ensuring consistency across
development, testing, and production environments.
Cont’d
• solation and Efficiency: Docker containers provide lightweight and
isolated runtime environments for applications.
• They share the host operating system's kernel but run as isolated
processes, which allows for efficient resource utilization and
eliminates the need for running multiple virtual machines for each
application.
Cont’d
• Simplified Deployment: Docker simplifies the deployment process by
encapsulating the application and its dependencies into a single,
portable container.
• This container can be easily distributed and deployed across different
environments, making the deployment process more reliable and
scalable.
Cont’d
• Microservices Architecture: Docker played a significant role in
popularizing microservices architecture. By containerizing individual
components of an application as microservices, developers can
independently develop, deploy, and scale each component, leading to
increased agility and scalability.
Cont’d
• Community and Ecosystem: Docker has a vibrant community and a
rich ecosystem of tools and resources that support various aspects of
the container lifecycle, including orchestration, monitoring, and
security.
• This ecosystem has contributed to Docker's popularity and adoption.
Container vs virtual machine
Container Virtual machine
Share host OS kernel; run as Emulate complete hardware
Architecture
isolated processes environment
Require dedicated resources for
Resource Utilization Lightweight; share host resources
each instance
Isolation Process-level isolation Hardware-level isolation
Highly portable; encapsulate Less portable; require complete OS
Portability
dependencies image
Boot Time Faster startup Longer startup time
Management Overhead Lower management overhead Higher management overhead
Scalability Easily scale; lightweight Can be resource-intensive to scale
Limited isolation; potential security
Security Strong isolation; enhanced security
risks
More traditional development
Development Workflow Promotes DevOps practices
workflows
Legacy applications, diverse OS
Use Cases Microservices, cloud-native apps
requirements
Basic command with Docker
COMMAND DESCRIPTION
docker run <image> Run a container based on a specified image
docker ps List running containers
docker ps -a List all containers (including stopped ones)
docker images List available images
docker pull <image> Download an image from a registry
docker build <path_to_Dockerfile> Build an image from a Dockerfile
docker stop <container> Stop a running container
docker start <container> Start a stopped container
docker rm <container> Remove a stopped container
docker rmi <image> Remove an image
Execute a command inside a running container in
docker exec -it <container> <command>
interactive mode
How to create a Docker image
1. Write a Dockerfile: Create a text file named Dockerfile (no file
extension) in your project directory
This file contains a series of instructions for building your Docker
image
FROM linux:latest
COPY . /app
WORKDIR /app
CMD node /app/app.js
Cont’d
2. Build the Docker image:
Open a terminal or command prompt, navigate to the directory
containing your Dockerfile, and run the docker build command.
Commands:
docker build -t <image_name>:
docker build -t myapp:latest .
3. Verify the image: After the build process completes, you can verify
that the image was created successfully by running:
Command: Docker images