generated from cotes2020/chirpy-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2505808
commit 1845ef8
Showing
1 changed file
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
--- | ||
layout: post | ||
title: Docker | ||
date: 2024-11-10 21:50:44 +08000 | ||
categories: blog | ||
tags: [Docker] | ||
--- | ||
|
||
# Docker | ||
|
||
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. | ||
|
||
--- | ||
|
||
## What is Docker? | ||
|
||
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. | ||
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. | ||
|
||
## Key Benefits of Docker | ||
|
||
- Consistency Across Environments | ||
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. | ||
|
||
- Portability | ||
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. | ||
|
||
- Efficiency and Performance | ||
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. | ||
|
||
- Scalability | ||
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. | ||
|
||
- Simplified Dependency Management | ||
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. | ||
|
||
--- | ||
|
||
### Key Docker Concepts | ||
1. Docker Image: A lightweight, standalone, and executable package that includes everything needed to run a piece of software. | ||
|
||
2. Docker Container: A runnable instance of a Docker image. Containers can be started, stopped, moved, and deleted. | ||
|
||
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. | ||
|
||
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. | ||
|
||
--- | ||
|
||
### Use Cases for Docker | ||
|
||
1. Microservices Architecture | ||
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. | ||
|
||
2. Continuous Integration and Continuous Deployment (CI/CD) | ||
Docker enables seamless integration with CI/CD pipelines. Automated testing, deployment, and rollback are easier because Docker containers ensure consistent environments across stages. | ||
|
||
3. Development Environments | ||
Developers can create isolated environments to avoid dependency conflicts, test in various setups, and spin up containers quickly to test different configurations. | ||
|
||
4. Cloud Migration and Hybrid Cloud | ||
Containers make it easier to move applications to the cloud or between different cloud providers, which is useful for hybrid and multi-cloud strategies. | ||
|
||
--- | ||
|
||
# A Quick Guide | ||
|
||
1. Install Docker | ||
|
||
```bash | ||
sudo apt-get update | ||
sudo apt-get install ca-certificates curl | ||
sudo install -m 0755 -d /etc/apt/keyrings | ||
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc | ||
sudo chmod a+r /etc/apt/keyrings/docker.asc | ||
|
||
echo \ | ||
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ | ||
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ | ||
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null | ||
sudo apt-get update | ||
``` | ||
|
||
2. Install Docker packages | ||
|
||
```bash | ||
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin | ||
``` | ||
|
||
3. Pull an Image from Docker Hub | ||
|
||
```bash | ||
docker run hello-world | ||
``` | ||
|
||
4. Create Dockerfile | ||
|
||
- Make a new directory: | ||
|
||
```bash | ||
mkdir my-node-app && cd my-node-app | ||
``` | ||
|
||
- Inside the directory, create a new Dockerfile: | ||
|
||
```Dockerfile | ||
# Use an official Node.js runtime as the base image | ||
FROM node:14 | ||
|
||
# Create and set the working directory | ||
WORKDIR /usr/src/app | ||
|
||
# Copy the current directory contents into the container | ||
COPY . . | ||
|
||
# Install dependencies | ||
RUN npm install | ||
|
||
# Expose the port the app runs on | ||
EXPOSE 3000 | ||
|
||
# Run the app | ||
CMD ["node", "app.js"] | ||
``` | ||
|
||
- Place your application code in the same directory. For example, create a simple app.js file: | ||
|
||
```javascript | ||
const http = require('http'); | ||
const server = http.createServer((req, res) => { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.end('Hello, Docker!\n'); | ||
}); | ||
server.listen(3000, () => { | ||
console.log('Server running at http://localhost:3000/'); | ||
}); | ||
``` | ||
|
||
5. Build and Run Your Docker Image | ||
|
||
- To build your Docker image, use: | ||
|
||
```bash | ||
docker build -t my-node-app . | ||
``` | ||
|
||
- Once built, you can run the container: | ||
|
||
```bash | ||
docker run -p 3000:3000 my-node-app | ||
``` | ||
## Your app should now be accessible at http://localhost:3000! |