9.1 Microservice Architecture
Docker has become an essential tool for modern development. It helps to create, run, and deploy applications in isolated containers, making them convenient, stable, and easy to work with. In this lecture, we'll look at examples where Docker significantly simplifies life for developers and administrators.
The Problem
Developing monolithic applications often brings challenges with maintenance and scaling. Any change in one part can affect others, making updates and testing more complicated.
The Solution with Docker
Docker allows the implementation of a microservice architecture by packaging each service into a separate container. This provides independence and isolation of microservices.
Example
Let's say you have an app with three microservices: users, orders, and payment. With Docker, you can create three containers:
docker run -d --name users-service users-image
docker run -d --name orders-service orders-image
docker run -d --name payment-service payment-image
9.2 Continuous Integration and Delivery (CI/CD)
The Problem
Development, testing, and deployment often take a lot of time, especially if environments differ from each other.
The Solution with Docker
Docker helps create identical environments for all development stages. This ensures the application works the same everywhere — from development to production.
Example
Setting up CI/CD with Docker allows automating build, testing, and deployment:
Building the image:
docker build -t my-app .
Running tests:
docker run my-app ./run-tests.sh
Deployment:
docker push my-app:latest
ssh user@production-server "docker pull my-app:latest && docker run -d my-app"
9.3 Portability and Consistency
Problem
Sometimes an app runs on one machine but refuses to start on another due to differences in configuration or dependency versions.
Solution with Docker
Docker lets you package an app together with all its dependencies into a container that will work consistently in any environment.
Example
With a Dockerfile, you can describe the dependencies and commands to run the app:
FROM node:14
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "start"]
Now the app will run steadily on a local machine, server, or in the cloud.
9.4 Security Assurance
Problem
Deploying apps can be vulnerable, especially if there are third-party libraries and tools involved.
Solution with Docker
Docker provides container isolation, which reduces risks. Containers work independently, limiting their impact on the system.
Example
Use parameters to limit resources and enhance security:
docker run -d --name secure-app --memory="512m" --cpus="1" --security-opt=no-new-privileges my-secure-app
9.5 Testing and Automation
The Problem
Testing in different configurations takes time and can affect the main environment.
Solution with Docker
Docker lets you create isolated test environments without impacting the main system.
Example
Example of running a database and an application in a test environment:
docker run -d --name test-db -e POSTGRES_PASSWORD=mysecretpassword postgres
docker run -d --name test-app --link test-db:db my-app
9.6 Cloud Computing and Scaling
The Problem
Scaling applications in the cloud can be tricky and expensive.
Solution with Docker
Docker makes scaling easier thanks to its integration with cloud platforms like AWS, Azure, and Google Cloud.
Example
Using Docker and Kubernetes, you can easily set up scaling. Here's an example of a yaml file. It might not make sense now, but we'll work through it over time.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:latest
ports:
- containerPort: 80
GO TO FULL VERSION