了解docker -v命令 [英] Understanding docker -v command

查看:45
本文介绍了了解docker -v命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是通过 此教程 在YouTube上,试图了解 -v 命令的用法.

I was just going through this tutorial on Youtube, trying to understand the use of the -v command.

为什么作者使用 -v 命令?他使用命令,如下所示:

Why is the author using the -v command? He uses the command, like so:

docker run -v /var/lib/mysql --name=my_datastore -d busybox echo "my datastore"

现在我在某种程度上理解了上面的命令:

Now I understand the above command to an extent:

  • -name = my_datastore 为容器指定一个特定的名称.
  • -d busybox 基于 busybox 映像以分离模式启动容器.
  • --name=my_datastore gives the container a specific name.
  • -d busybox starts a container in detached mode, based on the busybox image.

执行命令后,以下行将回显到控制台.

After the command is executed the below line is echoed to the console.

my datastore 

现在,我不了解的部分如下:

Now, the part I don't understand is the following:

-v /var/lib/mysql

为什么在这里使用 -v 命令,为什么还要指定路径/var/lib/mysql ?

Why is the -v command used here, and why the path /var/lib/mysql specified?

我很难理解为什么使用上面的代码行以及在什么情况下使用.有人可以解释吗?

I am having difficulty understanding why the above line is used and in what context. Can anybody explain?

推荐答案

docker run -v (或-volume )参数>用于在与容器文件系统其余部分分开的容器内部创建存储空间.该命令有两种形式.

The -v (or --volume) argument to docker run is for creating storage space inside a container that is separate from the rest of the container filesystem. There are two forms of the command.

给定单个参数(例如 -v/var/lib/mysql )时,它将从Docker分配空间并将其安装在给定位置.这主要是一种从Docker分配存储的方法,该存储与您的服务容器不同.例如,您可能想运行数据库应用程序的较新版本,其中涉及拆除现有的MySQL容器并启动一个新的容器.您希望您的数据能够在此过程中幸存下来,因此将其存储在可以由游览数据库容器访问的卷中.

When given a single argument, like -v /var/lib/mysql, this allocates space from Docker and mounts it at the given location. This is primarily a way of allocating storage from Docker that is distinct from your service container. For example, you may want to run a newer version of a database application, which involves tearing down your existing MySQL container and starting a new one. You want your data to survive this process, so you store it in a volume that can be accessed by tour database container.

给定两个参数( host_path:container_path )时,例如 -v/data/mysql:/var/lib/mysql ,这会将指定目录安装在主机上在指定路径的容器内(准确地说,这也可以用于暴露容器内的主机文件;例如, -v/etc/localtime:/etc/localtime将使主机上的/etc/localtime 在容器内作为/etc/localtime 可用).这是一种将信息输入到您的容器中的方法,或者是为容器提供一种使文件可被主机访问的方法.

When given two arguments (host_path:container_path), like -v /data/mysql:/var/lib/mysql, this mounts the specified directory on the host inside the container at the specified path (and, to be accurate, this can also be used to expose host files inside the container; for example -v /etc/localtime:/etc/localtime would make /etc/localtime on the host available as /etc/localtime inside the container). This is a way of either feeding information into your container, or providing a way for your container to make files accessible to the host.

如果容器具有可用的卷(通过使用 -v 命令行参数或通过Dockerfile中的 VOLUME 指令),则可以从以下位置访问这些卷使用-volumes-from 选项的另一个容器:

If a container has volumes available, either through the use of the -v command line argument or via the VOLUME directive in a Dockerfile, these volumes can be accessed from another container using the --volumes-from option:

docker run --volumes-from my_datastore ...

这将使源容器中定义的任何卷在以-volumes-from 开头的容器中可用.

This will make any volumes defined in the source container available in the container you're starting with --volumes-from.

Docker卷文档中对此进行了详细讨论.

This is discussed in more detail in the Docker Volumes documentation.

这篇关于了解docker -v命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆