运维知识薄弱,补个知识吧
container(容器)
电脑中区隔开的独立进程,不受外界影响
image(镜像)
装container的地方。类似系统镜像
Dockerfile
containner image的配置文件,高度可定制化,官方最推荐使用的方式
执行dockerfile的命令是:
docker build -t 镜像名称 .
创建好镜像后,启动镜像的命令是:
docker run -dp 3000:3000 镜像名称
获取数据库地址命令:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 容器名称
volumes
如果删除了container,那么其中的文件夹也会被删除,有没有办法使得即使container删除了,其中的数据不丢呢?volumes!
Volumes provide the ability to connect specific filesystem paths of the container back to the host machine. If a directory in the container is mounted, changes in that directory are also seen on the host machine. If we mount that same directory across container restarts, we’d see the same files.
创建volume的命令是:
docker volume create 名称
使用方式是:add the -v flag to specify a volume mount. We will use the named volume and mount it to /etc/todos, which will capture all files created at the path.
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
想知道创建的volume的详细信息,使用:
docker volume inspect 名称
/*output:
[
{
"CreatedAt": "2021-09-08T07:33:23Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/todo-db/_data",
"Name": "todo-db",
"Options": {},
"Scope": "local"
}
]*/
原则:一个容器就做一件事
each container should do one thing and do it well. A few reasons:
-
There’s a good chance you’d have to scale APIs and front-ends differently than databases
-
Separate containers let you version and update versions in isolation
-
While you may use a container for the database locally, you may want to use a managed service for the database in production. You don’t want to ship your database engine with your app then.
-
Running multiple processes will require a process manager (the container only starts one process), which adds complexity to container startup/shutdown
不同容器之间链接
If two containers are on the same network, they can talk to each other. If they aren’t, they can’t.
网友评论