mirror of
https://github.com/yeasy/docker_practice.git
synced 2025-08-02 22:11:38 +00:00
use nginx:alpine as demo
Signed-off-by: Kang Huaishuai <khs1994@khs1994.com>
This commit is contained in:
@@ -7,29 +7,27 @@
|
||||
```bash
|
||||
$ docker run -d -P \
|
||||
--name web \
|
||||
# -v /src/webapp:/opt/webapp \
|
||||
--mount type=bind,source=/src/webapp,target=/opt/webapp \
|
||||
training/webapp \
|
||||
python app.py
|
||||
# -v /src/webapp:/usr/share/nginx/html \
|
||||
--mount type=bind,source=/src/webapp,target=/usr/share/nginx/html \
|
||||
nginx:alpine
|
||||
```
|
||||
|
||||
上面的命令加载主机的 `/src/webapp` 目录到容器的 `/opt/webapp`目录。这个功能在进行测试的时候十分方便,比如用户可以放置一些程序到本地目录中,来查看容器是否正常工作。本地目录的路径必须是绝对路径,以前使用 `-v` 参数时如果本地目录不存在 Docker 会自动为你创建一个文件夹,现在使用 `--mount` 参数时如果本地目录不存在,Docker 会报错。
|
||||
上面的命令加载主机的 `/src/webapp` 目录到容器的 `/usr/share/nginx/html`目录。这个功能在进行测试的时候十分方便,比如用户可以放置一些程序到本地目录中,来查看容器是否正常工作。本地目录的路径必须是绝对路径,以前使用 `-v` 参数时如果本地目录不存在 Docker 会自动为你创建一个文件夹,现在使用 `--mount` 参数时如果本地目录不存在,Docker 会报错。
|
||||
|
||||
Docker 挂载主机目录的默认权限是 `读写`,用户也可以通过增加 `readonly` 指定为 `只读`。
|
||||
|
||||
```bash
|
||||
$ docker run -d -P \
|
||||
--name web \
|
||||
# -v /src/webapp:/opt/webapp:ro \
|
||||
--mount type=bind,source=/src/webapp,target=/opt/webapp,readonly \
|
||||
training/webapp \
|
||||
python app.py
|
||||
# -v /src/webapp:/usr/share/nginx/html:ro \
|
||||
--mount type=bind,source=/src/webapp,target=/usr/share/nginx/html,readonly \
|
||||
nginx:alpine
|
||||
```
|
||||
|
||||
加了 `readonly` 之后,就挂载为 `只读` 了。如果你在容器内 `/opt/webapp` 目录新建文件,会显示如下错误
|
||||
加了 `readonly` 之后,就挂载为 `只读` 了。如果你在容器内 `/usr/share/nginx/html` 目录新建文件,会显示如下错误
|
||||
|
||||
```bash
|
||||
/opt/webapp # touch new.txt
|
||||
/usr/share/nginx/html # touch new.txt
|
||||
touch: new.txt: Read-only file system
|
||||
```
|
||||
|
||||
@@ -48,7 +46,7 @@ $ docker inspect web
|
||||
{
|
||||
"Type": "bind",
|
||||
"Source": "/src/webapp",
|
||||
"Destination": "/opt/webapp",
|
||||
"Destination": "/usr/share/nginx/html",
|
||||
"Mode": "",
|
||||
"RW": true,
|
||||
"Propagation": "rprivate"
|
||||
|
@@ -10,7 +10,7 @@
|
||||
|
||||
* `数据卷` 默认会一直存在,即使容器被删除
|
||||
|
||||
>注意:`数据卷` 的使用,类似于 Linux 下对目录或文件进行 mount,镜像中的被指定为挂载点的目录中的文件会隐藏掉,能显示看到的是挂载的 `数据卷`。
|
||||
>注意:`数据卷` 的使用,类似于 Linux 下对目录或文件进行 mount,镜像中的被指定为挂载点的目录中的文件会复制到数据卷中(仅数据卷为空时会复制)。
|
||||
|
||||
## 创建一个数据卷
|
||||
|
||||
@@ -23,6 +23,7 @@ $ docker volume create my-vol
|
||||
```bash
|
||||
$ docker volume ls
|
||||
|
||||
DRIVER VOLUME NAME
|
||||
local my-vol
|
||||
```
|
||||
|
||||
@@ -46,15 +47,14 @@ $ docker volume inspect my-vol
|
||||
|
||||
在用 `docker run` 命令的时候,使用 `--mount` 标记来将 `数据卷` 挂载到容器里。在一次 `docker run` 中可以挂载多个 `数据卷`。
|
||||
|
||||
下面创建一个名为 `web` 的容器,并加载一个 `数据卷` 到容器的 `/webapp` 目录。
|
||||
下面创建一个名为 `web` 的容器,并加载一个 `数据卷` 到容器的 `/usr/share/nginx/html` 目录。
|
||||
|
||||
```bash
|
||||
$ docker run -d -P \
|
||||
--name web \
|
||||
# -v my-vol:/wepapp \
|
||||
--mount source=my-vol,target=/webapp \
|
||||
training/webapp \
|
||||
python app.py
|
||||
# -v my-vol:/usr/share/nginx/html \
|
||||
--mount source=my-vol,target=/usr/share/nginx/html \
|
||||
nginx:alpine
|
||||
```
|
||||
|
||||
## 查看数据卷的具体信息
|
||||
@@ -73,7 +73,7 @@ $ docker inspect web
|
||||
"Type": "volume",
|
||||
"Name": "my-vol",
|
||||
"Source": "/var/lib/docker/volumes/my-vol/_data",
|
||||
"Destination": "/webapp",
|
||||
"Destination": "/usr/share/nginx/html",
|
||||
"Driver": "local",
|
||||
"Mode": "",
|
||||
"RW": true,
|
||||
|
Reference in New Issue
Block a user