Skip to content

Commit 1845ef8

Browse files
docker
1 parent 2505808 commit 1845ef8

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

_posts/2024-11-10-docker.md

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
layout: post
3+
title: Docker
4+
date: 2024-11-10 21:50:44 +08000
5+
categories: blog
6+
tags: [Docker]
7+
---
8+
9+
# Docker
10+
11+
In today’s fast-paced tech landscape, containerization has become an essential part of how software is developed, shipped, and deployed. Docker, a leading containerization platform, has revolutionized the way applications are managed across environments, from development to production. Let’s explore what Docker is, why it’s popular, and how you can start using it.
12+
13+
---
14+
15+
## What is Docker?
16+
17+
Docker is an open-source platform that enables developers to automate the deployment of applications as lightweight, portable, self-sufficient containers. Containers are isolated environments that package an application and all its dependencies, libraries, and configuration files, ensuring it runs consistently regardless of where it’s deployed.
18+
Unlike traditional virtual machines, Docker containers are highly efficient because they share the host system’s operating system kernel, making them faster to start, smaller in size, and easier to manage.
19+
20+
## Key Benefits of Docker
21+
22+
- Consistency Across Environments
23+
Docker ensures that an application behaves the same in development, testing, and production. By creating a “container image” with all dependencies included, you eliminate the classic “it works on my machine” issue.
24+
25+
- Portability
26+
Docker containers can run on any system that supports Docker, making it easy to move applications from local development to the cloud or between different servers.
27+
28+
- Efficiency and Performance
29+
Unlike virtual machines, Docker containers don’t need a full OS. They use fewer resources and start up faster, which is ideal for microservices and high-performance applications.
30+
31+
- Scalability
32+
Docker makes it easier to scale applications. Containers can be added or removed quickly, and they work well with orchestration tools like Kubernetes to handle complex deployments.
33+
34+
- Simplified Dependency Management
35+
Docker lets you package application dependencies within the container. This makes it easy to maintain consistency, avoids version conflicts, and ensures that the app runs as expected on any platform.
36+
37+
---
38+
39+
### Key Docker Concepts
40+
1. Docker Image: A lightweight, standalone, and executable package that includes everything needed to run a piece of software.
41+
42+
2. Docker Container: A runnable instance of a Docker image. Containers can be started, stopped, moved, and deleted.
43+
44+
3. Dockerfile: A text file with instructions for building a Docker image. Each line in a Dockerfile represents a layer in the image, allowing you to configure everything from the base OS to application code and dependencies.
45+
46+
4. Docker Hub: A cloud-based registry where you can find, share, and download Docker images. Docker Hub offers thousands of pre-built images, so you don’t always have to start from scratch.
47+
48+
---
49+
50+
### Use Cases for Docker
51+
52+
1. Microservices Architecture
53+
Docker is popular for deploying microservices, where each service can run in its own container, making it easier to update, scale, and manage services independently.
54+
55+
2. Continuous Integration and Continuous Deployment (CI/CD)
56+
Docker enables seamless integration with CI/CD pipelines. Automated testing, deployment, and rollback are easier because Docker containers ensure consistent environments across stages.
57+
58+
3. Development Environments
59+
Developers can create isolated environments to avoid dependency conflicts, test in various setups, and spin up containers quickly to test different configurations.
60+
61+
4. Cloud Migration and Hybrid Cloud
62+
Containers make it easier to move applications to the cloud or between different cloud providers, which is useful for hybrid and multi-cloud strategies.
63+
64+
---
65+
66+
# A Quick Guide
67+
68+
1. Install Docker
69+
70+
```bash
71+
sudo apt-get update
72+
sudo apt-get install ca-certificates curl
73+
sudo install -m 0755 -d /etc/apt/keyrings
74+
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
75+
sudo chmod a+r /etc/apt/keyrings/docker.asc
76+
77+
echo \
78+
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
79+
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
80+
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
81+
sudo apt-get update
82+
```
83+
84+
2. Install Docker packages
85+
86+
```bash
87+
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
88+
```
89+
90+
3. Pull an Image from Docker Hub
91+
92+
```bash
93+
docker run hello-world
94+
```
95+
96+
4. Create Dockerfile
97+
98+
- Make a new directory:
99+
100+
```bash
101+
mkdir my-node-app && cd my-node-app
102+
```
103+
104+
- Inside the directory, create a new Dockerfile:
105+
106+
```Dockerfile
107+
# Use an official Node.js runtime as the base image
108+
FROM node:14
109+
110+
# Create and set the working directory
111+
WORKDIR /usr/src/app
112+
113+
# Copy the current directory contents into the container
114+
COPY . .
115+
116+
# Install dependencies
117+
RUN npm install
118+
119+
# Expose the port the app runs on
120+
EXPOSE 3000
121+
122+
# Run the app
123+
CMD ["node", "app.js"]
124+
```
125+
126+
- Place your application code in the same directory. For example, create a simple app.js file:
127+
128+
```javascript
129+
const http = require('http');
130+
const server = http.createServer((req, res) => {
131+
res.writeHead(200, { 'Content-Type': 'text/plain' });
132+
res.end('Hello, Docker!\n');
133+
});
134+
server.listen(3000, () => {
135+
console.log('Server running at http://localhost:3000/');
136+
});
137+
```
138+
139+
5. Build and Run Your Docker Image
140+
141+
- To build your Docker image, use:
142+
143+
```bash
144+
docker build -t my-node-app .
145+
```
146+
147+
- Once built, you can run the container:
148+
149+
```bash
150+
docker run -p 3000:3000 my-node-app
151+
```
152+
## Your app should now be accessible at http://localhost:3000!

0 commit comments

Comments
 (0)