質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

Q&A

2回答

2304閲覧

vagrantを使ってruby on rails で共有ファイルの設定ができない

fdaf_11111

総合スコア16

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

0グッド

0クリップ

投稿2017/02/23 12:41

編集2022/01/12 10:55

ruby on rails で共有ファイルの設定ができません。
共有ファイルとは、
例えばvagrantでつくったディレクトリやファイルが、vagrantを立ち上げていないときmacのfinderなどでは表示されません。これをfinderでもvagrantを立ち上げていなくても表示されるようにしたいです。
finder(vagrantを立ち上げていない状態)でのファイル構造は
-MyVagrant-mycentos-VagrantFile
です。

vagrantでのファイル構造は
/home/vagrant/myapp/
となっていて今回このmyappというディレクトリを共有ファイルにしてvagrantを立ち上げなくても見れるようにしたいです。

cp -r myapp mycentos

としてmycentosにディレクトリをコピーしようとしたり、

mv myapp /vagrant/

としてファイルを移動させようとしましたが、いずれもだめでした。

よろしくお願いします。

vagrantfileの中身は以下のようになっています。

# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.box = "bento/centos-6.7" # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a private network, which allows host-only access to the machine # using a specific IP. config.vm.network :private_network, ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on your network. # config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies # such as FTP and Heroku are also available. See the documentation at # https://docs.vagrantup.com/v2/push/atlas.html for more information. # config.push.define "atlas" do |push| # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" # end # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELL config.vm.network "forwarded_port", guest: 3000, host:3000 end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

Vagrantで仮想マシンを立ち上げると、デフォルトでは以下のような形でホストOS側のディレクトリとVagrant側のGuest OSが同期(共有)されます。

ホストOS側のVagrantfileがあるディレクトリ (vagrant upするディレクトリ)

ゲストOS側の /vagrant というディレクトリ

試しにゲストOSにsshで入って、ls /vagrant とすると、ホストOS側のVagrantfileがあるはずです。
また、/vagrantディレクトリでなにかファイルを作ると、それがホストOS側に反映されますし、vagrant haltしてもホストOSに残ります。

ここまではご周知かと思います。

ゲストOS側の /home/vagrant/myapp/ をホスト側と同期させたいなら、Vagrantfileにsync_folder という設定箇所があるので、そこに定義してあげればいいと思います。(サンプルだとコメントアウトされてますよね)

# config.vm.synced_folder "../data", "/vagrant_data”

config.vm.synced_folder “ホスト上のディレクトリ", “ゲスト上のディレクトリ”

としてreloadするとsyncできます。

念のためですが、この共有フォルダは、
「ホストOSのディレクトリをゲストOSにマウントする」というものであって、
「ゲスト側のディレクトリをホスト側にマウントさせる」のとは違います。

たいていはホスト側のVagrantfileのあるディレクトリにアプリケーションのソースを持って来ておき、それをVagrant起動時にゲストOS側の /vagrant とか、sync_folderで指定した箇所にマウントさせる感じになります。

(まちがっていたり、自己解決されていましたらすみません...)

設定しているのにうまく反映されない、という場合はreloadしてみてください。

投稿2017/02/25 13:01

suama

総合スコア1997

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

VagrantではVagrantfile内でファイル共有の機能(synced_folder)を宣言しなくてはなりません。
掲載いただいているVagrantfile内の以下のコメントアウト部分を有効にしてみてください。

config.vm.synced_folder "../data", "/vagrant_data"

共有ディレクトリの設定は
config.vm.synced_foler "ゲスト側(local)", "ホスト側"
になっているので調整すると良いでしょう。

投稿2017/02/25 12:40

onunu

総合スコア14

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問