- 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
1. Review & Preface
After the previous explanation, you must be able to start Nginx container and Node container and understand some simple concepts and instructions.
So this article will explain some instructions and operations that newcomers in Docker need to know.
- Image command
- Container directive
- Shell commands
- Host and container code synchronization
- Isolate Hosts
- Modify container time
2. Image command
- Create image:
docker image build ./ -t hello-docker:1.0.0
- Check the mirror list:
docker image ls
- Delete a single image:
docker rmi <image id>
- Delete all mirrors:
docker rmi $(docker images -q)
- Force delete all mirrors:
docker rmi -f $(docker images -q)
Third, the container (Container) instruction
- Create container:
docker container create -p 2333:80 hello-docker:1.0.0
- Run the container (that long string of characters is the result of creating the container):
docker container start 4a4d2ac6bf3977ee7c5b4a15562bab17d59b4c61531908fd52632fb0c989f430
- Create and run instant container (close and delete container on exit):
docker run -it --rm --name hello-docker -p 2333:80 hello-docker:1.0.0
- View container list:
docker ps -a
Enter the designated container:
docker exec -it 7961990657ef bash
bash
: change according to different images, for example,alpine
needs to become/bin/sh
After entering the container (Container), switch the shell command to use (essentially the same as the VS Code editor terminal, Windows cmd, etc.)
- Exit container:
exit
- Start/stop/restart container:
docker start/stop/restart CONTAINER_ID
- To delete a single container:
docker rm CONTAINER_ID
- Force deletion of a single container:
docker rm -f CONTAINER_ID
- Stop all containers:
docker stop $(docker ps -a -q)
ordocker stop $(docker ps -aq)
- Delete all containers:
docker rm $(docker ps -a -q)
ordocker rm $(docker ps -aq)
3. Shell instructions
- View current folder:
ls
- Open the specified folder:
cd html
- View the content of the specified file (with line number):
cat -n index.html
Fourth, code synchronization
When we started the local Docker service, it was easy to modify the code.
but! The damn code will not be synchronized into the container. At this time, if you delete the container, it is too low, so you need to synchronize the code~
Let's take the Node.js Demo as an example to illustrate how to synchronize the code.
Code repository address: https://github.com/LiangJunrong/all-for-one/tree/master/036-Docker%20for%20Node
- Copy the host code to the container:
docker cp "E:/MyWeb/036-Docker for Node/README.md" 8e1910aa2a12:/usr/src/app/README.md
docker cp 宿主机的路径 容器名|容器 ID:容器中的文件路径
Note 1: If it is Windows and the directory contains spaces, it is best to wrap it by "path"
Note 2: It is best to step back one level in the directory, such as docker cp E:/MyWeb/036-Docker for Node/src /usr/src/app/
Note 3: cp
is copy
, meaning copy
- Copy the container code to the host:
docker cp 8e1910aa2a12:/usr/src/app/tsconfig.json E:\MyWeb\all-for-one
docker cp 容器名 :容器中的文件路径 宿主机的路径
- Let the container code synchronize the host code in real time:
docker run -d -v E:\MyWeb\all-for-one\src:/usr/src/app/src docker-node:1.0.0
docker run -d -v 容器中文件路径 :宿主机文件路径 容器名 :版本号
What we need to know is:
-d
: run in the background-v A:B
: Mount the file/folder of the path of host A to the path of container B (both are absolute paths)docker-node:1.0.0
:REPOSITORY
anddocker image ls
viewed byTAG
5. Isolate Hosts
After entering the container, we can view the Hosts
cat /etc/hosts
By the way, we changed the local Hosts file, changed Baidu to 127.127.127.127
, and in the container ping baidu.com
, we can see that Hosts has changed with the host.
So, how can you let it have its own thoughts and not move around?
First , we find the project directory and add 2 files:
hosts
127.0.0.1 localhost
resolv.conf
nameserver 114.114.114.114
These two files, one is to modify the Hosts, the other is to modify the DNS port, that is, we think that when the container is opened, the independent Hosts and DNS ports are used.
followed by , we run the command:
docker run -d -v E:\MyWeb\all-for-one\hosts:/etc/hosts -v E:\MyWeb\all-for-one\resolv.conf:/etc/resolv.conf docker-node:1.0.0
The container created in this way can achieve the effect of isolation.
Finally , we enter the new container and ping baidu.com
:
It can be found that we successfully modified the hosts of the container!
6. Modify the container time
Under normal circumstances, the time of the Docker container is different from that of our machine because of the time zone set by default:
- Query machine time:
date
It can be seen that the time of the container above is UTC, which is completely inconsistent with the time of the host below.
However, in our code, some need to store the correct time, which has to be modified.
And modifying them is very simple, just set Dockerfile
on it:
Dockerfile
# 设置 Docker 容器时区
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo Asia/Shanghai > /etc/timezone
Let's take the Node.js service as an example to try to unify the time between the host and the container.
Code repository address: https://github.com/LiangJunrong/all-for-one/tree/master/036-Docker%20for%20Node
Let's create an image and run the container according to the repository's README.md:
View date
after entering the container:
You can see that the time is normal!
7. Summary
In this chapter, we have sorted out some common content. The little friends may be a little confused when they see so many instructions for the first time. In fact, jsliang is also a little bit more proficient.
So these can only be more familiar with more typing. I tried it for a few days before, but I forgot it when I looked back, so I organized it into this article for myself and everyone to review.
Stay tuned for the next part: 06 - Getting Started & Puppeteer Services
What is the difference between the no-fuss front end and the 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 slash programmer who is full of exploration, likes tossing, and is willing to expand his knowledge.
jsliang's documentation repository is licensed by Liang the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License . <br/>Created based on the work on https://github.com/LiangJunrong/document-library . <br/>Use rights other than authorized by this license agreement may be obtained from https://creativecommons.org/licenses/by-nc-sa/2.5/cn/1622a9f1bef328.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。