Synchronized Directory in Vagrant Manual - Basic Usage

Original Address - Overview
Original Address - Basic Usage

The Synced folder supports sharing directories between the host and client, allowing you to work on the host's project files, but compile and run on the client.

By default, Vagrant will share the project directory (the directory containing the Vagrantfile) to /vagrant.

1. Configuration

Configure the sync directory via the config.vm.synced_foldermethod . The usage of the configuration directive is very simple:

Vagrant.configure("2") do |config|
  # other config here

  config.vm.synced_folder "src/", "/srv/website"
end

The first parameter is the directory path of the host machine. If a relative path, it is relative to the root directory of the project. The second parameter must be the absolute path in the host, this directory will share the directory of the host. If the directory for the second argument does not exist (recursive creation is supported), it will be created automatically.

2. Options

Additional options parameters must be specified when configuring the sync directory. These parameters are listed below, and usage is demonstrated below, where the owner/group example provides two additional options, separated by both.

In addition to these options, specific sync directory types may allow more options. For more information, see the documentation for the specific sync directory type. The built-in synchronized directory types are NFS, SMB, VirtualBox, RSync.

  • create (boolean): The default is false. If true, the host path will be created automatically if it does not exist.
  • disabled (boolean): If true, this sync directory will be disabled and not set. Can be used to disable previously defined synchronization directories or to conditionally disable definitions based on some external factor.
  • group (string): The group that will own the sync directory. By default, this will be the SSH user. Some sync directory types do not support modifying groups.
  • mount_options (array): List of additional mount options passed to the mount command.
  • owner (string): The user corresponding to the owner of this sync directory. The default is the SSH user. Some sync directory types do not support changing the owner.
  • type (string): The type of the sync directory. If not specified, Vagrant will automatically choose the best sync directory type option. Otherwise, you can specify a specific type like "nfs".
  • id (string): The name of the mount point for this synced directory in the client machine. This happens when mount is run on the client.

3. Turn on

The sync vagrant updirectory vagrant reloadis automatically set when or .

4. Close

Synchronizing directories can be turned off using the disabledoption :

Vagrant.configure("2") do |config|
  config.vm.synced_folder "src/", "/srv/website", disabled: true
end

The following example turns off the default /vagrantsharing :

config.vm.synced_folder ".", "/vagrant", disabled: true

5. Modify Owner/Group

By default, Vagrant mounts the synced folders with the owner/group set to the SSH user. Sometimes it is better to mount directories to different users and groups. Options can be set like this:

config.vm.synced_folder "src/", "/srv/website",
  owner: "root", group: "root"

Note: The owner and group IDs defined in mount_options will take precedence over other owner and group options.

For example, given the following configuration:

config.vm.synced_folder ".", "/vagrant", owner: "vagrant",
  group: "vagrant", mount_options: ["uid=1234", "gid=1234"]

The mounted sync directory will be owned by user ID 1234 and group ID 1234. The owner and group options are ignored.

6. Symlinks

Vagrant configures various hypervisors (such as VirtualBox) to ensure that symlinks work, but some host/guest combinations still don't work. This may affect some development environments that rely on symlinks.

If you need to use symlinks, it is recommended to test first.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324820371&siteId=291194637