【Vagrant/Docker】Windows에서도 개발할 수 있도록 Vagrant + Docker의 환경을 만든다

Docker for Windows와 Vagrant / VM VirtualBox가 호환되지 않는 문제



VirtualBox 6.0부터 Hyper-V와 공존할 수 있어야 함 - Qiita
을 보고 드디어 공존할 수 있게 되었는가~~라고 생각해 시험해 보았습니다만,
역시 안 돼서 포기하고 Vagrant 위에서 Docker 움직이기로 했습니다.

Vagrant + Docker 환경을 만듭니다.



지난번Docker for Windows 로 환경 구축했습니다만, 이번은 Vagrant + Docker 로 환경 구축해 갑니다.

실행 환경



OS: Windows 10 Pro 64bit
RAM: 8GB

Vagrantfile 준비



아래 기사를 참고로했습니다
참고: centos7을 시작하자마자 Docker 할 수있는 Vagrantfile - Qiita

다만, 기사에 써 있는 문장의대로라면 Docker Compose의 버젼이 낡기 때문에,
GitHub Releases · docker/compose를 열고 최신 버전으로 다시 작성했습니다.

Vagrantfile
Vagrant.configure("2") do |config|

  config.vm.box = "centos/7"

  config.vm.network "forwarded_port", guest: 80, host: 80
  config.vm.network "private_network", ip:"192.168.77.11"

  config.vm.synced_folder "./", "/var/www"

  config.vm.provision "shell", inline: <<-SHELL
    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum makecache fast
    sudo yum install -y docker-ce
    sudo systemctl enable docker-ce
    sudo systemctl start docker-ce
    sudo curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` > docker-compose
    sudo mv docker-compose /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    sudo gpasswd -a vagrant docker
    sudo systemctl restart docker
  SHELL
end

vagrant up 하면 에러가 발생


vagrant up 그러면 ↓와 같은 오류가 발생합니다.
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. 

This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 var_www_html /var/www/html

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

분명히 vboxsf가없는 것 같습니다.

  • Vagrant 오류(Guest Additions - vboxsf) - Qiita
  • vagrant up시에 「mount: unknown filesystem type 'vboxsf'」가 발생한다 - 언젠가 엔지니어가 되고 싶다

  • 그래서 위의 사이트를 참고로 플러그인 vagrant-vbguest 설치
    $ vagrant plugin install vagrant-vbguest
    Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
    Installed the plugin 'vagrant-vbguest (0.17.2)'!
    

    완료된 것처럼

    계속해서 다시 vagrant up


    $ vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Checking if box 'centos/7' version '1902.01' is up to date...
    ==> default: Clearing any previously set forwarded ports...
    ==> default: Clearing any previously set network interfaces...
    ==> default: Preparing network interfaces based on configuration...
        default: Adapter 1: nat
    ==> default: You are trying to forward to privileged ports (ports <= 1024). Most
    ==> default: operating systems restrict this to only privileged process (typically
    
    【--- 略 ---】
    
        default:                                  Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
      0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
      0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
    100   617    0   617    0     0    197      0 --:--:--  0:00:03 --:--:--   197
      0     0    0     0    0     0      0      0 --:--:--  0:00:03 --:--:--     0
      1 8649k    1  100k    0     0  21775      0  0:06:46  0:00:04  0:06:42 94888
     27 8649k   27 2406k    0     0   430k      0  0:00:20  0:00:05  0:00:15 1233k
     40 8649k   40 3542k    0     0   526k      0  0:00:16  0:00:06  0:00:10 1149k
     51 8649k   51 4470k    0     0   577k      0  0:00:14  0:00:07  0:00:07 1091k
    100 8649k  100 8649k    0     0  1024k      0  0:00:08  0:00:08 --:--:-- 1800k
        default: Adding user vagrant to group docker
    

    완료

    vagrant ssh로 ssh 연결


    $ vagrant ssh
    Last login: Thu Mar 14 02:04:26 2019 from 10.0.2.2
    [vagrant@localhost ~]$ 
    

    시도해 docker run hello-world 실행


    [vagrant@localhost ~]$ docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    1b930d010525: Pull complete
    Digest: sha256:2557e3c07ed1e38f26e389462d0377a99efb77324b0fe53577a99efb77324b0fe535
    Status: Downloaded newer image for hello-world:latest
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
        (amd64)
     3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it
        to your terminal.
    
    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash
    
    Share images, automate workflows, and more with a free Docker ID:
     https://hub.docker.com/
    
    For more examples and ideas, visit:
     https://docs.docker.com/get-started/
    
    [vagrant@localhost ~]$
    

    무사히 움직였다

    【번외편】 docker-compose up 한다



    ※여기에서는 전회, 【Docker】Windows10에서 PHP5.3의 환경을 만들기 위해서 Docker를 사용했을 때의 궤적 - Qiita 로 작성했다docker-compose.yml와 같은 것을 사용합니다.
    docker-compose.yml가있는 디렉토리로 이동 한 후 docker-compose up
    [vagrant@localhost ~]$ cd ../../var/www/html/
    [vagrant@localhost html]$ docker-compose up
    Creating network "html_default" with the default driver
    Creating volume "html_mysql-db" with local driver
    Pulling db (vsamov/mysql-5.1.73:latest)...
    latest: Pulling from vsamov/mysql-5.1.73
    30d541b48fc0: Pull complete
    
    [~~~ 略 ~~~]
    
    db_1   | 190314  3:11:18 [Note] mysqld: ready for connections.
    db_1   | Version: '5.1.73'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)
    www_1  | httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3 for ServerName
    
    

    움직였다

    위에서 만든 Vagrantfile이라면 http://192.168.77.11:8080에서 브라우저에서 확인할 수 있습니다.

    기타 docker 명령



    docker 시작


    [vagrant@localhost ~]$ service docker start
    

    그건 그렇고, 다음에 부팅 할 때 service docker start해야합니다.

    docker가 움직이고 있는지 확인


    [vagrant@localhost html]$ sudo service docker status
    



  • centos7을 시작하자마자 Docker 할 수있는 Vagrantfile - Qiita의 기사가 매우 도움이되었습니다. 고마워요.

  • 참고 URL


  • VirtualBox 6.0부터 Hyper-V와 공존할 수 있어야 함 - Qiita
  • centos7을 시작하자마자 Docker 할 수있는 Vagrantfile - Qiita
  • Releases · docker/compose

  • Vagrant 오류(Guest Additions - vboxsf) - Qiita
  • vagrant up시에 「mount: unknown filesystem type 'vboxsf'」가 발생한다 - 언젠가 엔지니어가 되고 싶다
  • 좋은 웹페이지 즐겨찾기