- The code corresponding to this document: https://github.com/LiangJunrong/all-for-one/tree/master/035-Docker%20for%20Nginx
- Docker series documents: https://github.com/LiangJunrong/document-library/tree/master/%E7%B3%BB%E5%88%97-%E5%89%8D%E7%AB%AF%E8 %B5%84%E6%96%99/Node/Node%20%E5%BA%94%E7%94%A8%E9%83%A8%E7%BD%B2
After the previous operation, after the installation is complete, let's start tossing it:
- Deploy an Nginx service on Docker with a simple index.html page on
- Explains the concepts of Dockerfile, image, container, etc. in Docker, and arranges and modifies the image source
Well, here we go~
1. Command operation
First , we create a new folder of demo-01
, and then enter the console:
- Jump to this folder:
cd demo-01
And store 2 files in it:
Dockerfile
# 本镜像拷贝自 Nginx 镜像
FROM nginx
# 将当前目录下的 index.html 挪到 Nginx 的 html 目录中
COPY ./index.html /usr/share/nginx/html/index.html
# 对外暴露本镜像的 80 端口
EXPOSE 80
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hello Docker</title>
<style>
/* 设置页面居中显示 Hello Docker! */
body {
margin: 0 auto;
width: 1000px;
}
h1 {
text-align: center;
}
</style>
</head>
<body>
<h1>Hello Docker!</h1>
</body>
</html>
and then , we write the instruction to create the image (Image):
docker image build ./ -t docker-nginx:1.0.0
followed by , we create the container:
docker container create -p 2222:80 docker-nginx:1.0.0
At this point, the console returns a string of characters, that is, , the result obtained by creating the container
and finally , we run this container (Container):
docker container start 312b8278e11373cb3211faeab53b7f0b47e996b5e84fc58
At this time, if you open the interface: http://127.0.0.1:2222/
You will see:
congratulations! Get started with Docker~
If you have a local area network or WIFI, you can modify the ip address ( http://ip address: 2222/ ) to see, under normal circumstances, you can access it successfully.
2. Summary
So, after this series of operations, the Nginx service is started, so what are some of the contents here: Dockerfile, image, and container?
So, we have a few concepts to understand:
- What is a Dockerfile?
- What is Image?
- What is a container?
Below we will explain them one by one, and list the instructions that newbies like jsliang should know when getting started.
3. What is Dockerfile?
A Dockerfile is a text file used to build an image. The text contains instructions and instructions for building an image.
Docker automatically generates images by reading the instructions in the Dockerfile.
Dockerfile can be used to invoke any command on the command line.
Dockerfile is generally divided into four parts: basic image information, maintainer information, image operation instructions, and execution instructions when the container starts.
To put it simply: You can tell Docker what to pay attention to when compiling through package.json
, just like you tell npm
what packages I need to install through 0622158fcd9988.
Reference: HUAWEI CLOUD - What is Dockerfile
4. What is Image?
An image is a file system that provides files and parameter configurations that the container needs to run.
It is equivalent to the installation package that needs to be downloaded when using a certain software, and it is also equivalent to the ISO file that needs to be used when installing the operating system.
To put it simply: you can think of an image as a Windows 11 system CD, and then which computer needs to install a new system, you can install it based on this CD.
There are many images on Docker Hub, interested partners can go to view and download: https://hub.docker.com/
It is worth mentioning that:
- The download speed of the domestic official source will be relatively slow, so it is a good choice to switch a domestic source address like npm, such as University of Science and Technology of China (University of Science and Technology of China), Alibaba Cloud, NetEase, Docker official China, etc.
mirror repository name | Link |
---|---|
Docker Official China Region | https://registry.docker-cn.com |
NetEase 163 | http://hub-mirror.c.163.com |
University of Science and Technology of China | https://docker.mirrors.ustc.edu.cn |
Ali Cloud | https://<your ID>.mirror.aliyuncs.com |
Mac modification method :
- Modify the Docker image repository configuration:
sudo vim /etc/docker/daemon.json
- Modify the configuration file:
daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
}
- Validate the configuration file:
sudo systemctl daemon-reload
- Restart Docker:
sudo service docker restart
Windows Modification :
After the modification is completed, view it through the command docker info
:
Of course, it should be noted that after switching the source and restarting, the current container will be suspended, and it will be fine to restart it.
References: Knowing - Energy of Testing - Getting Started with Docker: Mirror image
5. What is a container?
Container (Container) is a lightweight virtualization technology. The purpose is the same as that of virtual machines, which is to create an "isolated environment".
The relationship between image (Image) and container (Container) is like the class and instance in programming-oriented design. The image is a static definition, and the container is the entity of the image runtime.
Combined with our previous small example, we will see:
- Create image (Image):
docker image build ./ -t hello-docker:1.0.0
- Create Container (Container):
docker container create -p 2222:80 hello-docker:1.0.0
Here, when we create a container, hello-docker:1.0.0
is the REPOSITORY and TAG of the image. Based on this image, we create a container and specify its port.
At the same time, when talking about ports, we talked about host , the host is the host relative to the container, that is, the Windows 10 system of the portable notebook of jsliang .
Hit the point! There will be host later, don't miss the knowledge!
6. Summary
After understanding these knowledge points, it is time for us to operate again and review the content!
So in the next part, we will explain; 04 - Getting Started & Node Services
Stay tuned!
What is the difference between the non-torn front end and salted fish!
Friends who think the article is good are welcome to like/star.
If you need to contact jsliang :
Personal contact information is stored on the Github homepage, welcome to toss together~
Strive to build yourself into a lifelong learning bar programmer who is full of exploration, likes tossing, and is willing to expand his knowledge.
jsliang's documentation repository is licensed the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License by Liang . <br/>Created based on the works on https://github.com/LiangJunrong/document-library . <br/>Use rights other than authorized by this license agreement can be obtained from https://creativecommons.org/licenses/by-nc-sa/2.5/cn/1622158fcd9e9f.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。