본문 바로가기

윈도우 개발환경

VirtualBox와 Vagrant로 윈도우에 우분투와 Docker설치하기

0. WSL 우분투 distro의 한계

앞에서 설명한 바와 같이 윈도우에는 WSL(Windows Subsystem for Linux)이 있으며, 그 위에 공식적으로 우분투 distro를 제공하고 있다. Docker엔진과 docker-desktop distro 또한 WSL위에서 동작할 뿐 아니라, 마이크로소프트의 공식으로 지원 해주는, 윈도우에서 우분투를 사용할 수 있는 가장 쉬운 방법이다.

 

하지만, NAT 이외의 네트워크를 지원하지 않기 때문에,  WSL에서 동작하는 우분투 distro에 설치한 서비스를 내 윈도우 컴퓨터 밖에서 접속하기 위해서는 아래 스크립을 PowerShell에서 실행해 포트포워딩 해 줘야 한다.

 

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet ' | awk '{print $2}'"
$ports = @(22)  # 포워딩할 포트를 여기에 추가합니다.
foreach ($port in $ports) {
    netsh interface portproxy add v4tov4 listenport=$port listenaddress=0.0.0.0 connectport=$port connectaddress=$remoteport
}

 

또, 외부 IP를 할당할 수 없기 때문에, 분리된 하나의 컴퓨터나 서버로 테스트 하는 것도 힘들고, 여러 개의 distro를 동시에 실행하는 것도 어렵다. 실행에 성공하더라도 매우 느리게 동작하는 것을 확인할 수 있다.

 

이때 선택할 수 있는 것이 가상머쉰을 만들 수 있는 하이퍼바이저(Hypervisor)이다. 

1. VirtualBox

윈도우에서 무료로 사용할 수 있는 하이퍼바이저 중 가장 널리 사용되고 있는 것이 VirtualBox일 것이다. 물론 윈도우가 자체적으로 제공하는 Hyper-V가 있지만, VirtualBox에 비해 많이 사용하고 있지 않다.

 

VirtualBox는 홈페이지에서 다운로드 받아 간단하게 설치할 수 있다.

 

Downloads – Oracle VM VirtualBox

Download VirtualBox Here you will find links to VirtualBox binaries and its source code. VirtualBox binaries By downloading, you agree to the terms and conditions of the respective license. VirtualBox 7.0.20 platform packages The binaries are released unde

www.virtualbox.org

네트워크를 설치하는 부분이 붉은 색으로 경고가 나오지만, 처음부터 기본값만 선택하고 Next를 누르면 설치된다.

 

 

또 다른 방법으로는 winget으로 다음과 같이 설치한다.

 

winget install virtualbox

2. Vagrant

Vagrant는 VirtualBox를 이용해서 다양한 개발환경을 자동으로 쉽게 생성해 주는 도구이다. Teraform으로 유명한 Hashcorp의 최초 작품이기도 하다. Vagrant는 가상서버의 설정을 조정하는 부분에서부터, 가상서버가 생성되고 실행되어야 할 명령어까지 기록할 수 있다. docker에서 Dockerfile이 하는 역할과 매우 유사하다.

 

Vagrant도 홈페이지를 통해 다운로드 받아 쉽게 설치할 수 있다.

 

Install | Vagrant | HashiCorp Developer

Explore Vagrant product documentation, tutorials, and examples.

developer.hashicorp.com

VirtualBox에서와 같이 아래와 같이 winget명령을 사용해서 설치할 수도 있다.

 

winget install vagrant

 

Vagrant는 PATH환경변수에 내가 설치한 실행파일의 위치(C:\Program Files\Vagrant\bin)가 등록되지 않기 때문에, Win+R을 눌러 나오는 입력창에 "sysdm.cpl"을 넣어 실행하면 나오는 "시스템 속성"창에서 "고급"을 선택하고 아래의 "환경변수(N).."을 선택한 다음 PATH에 추가하면 된다.

 

 

환경변수를 적용하기 위해서는 Explorer를 다시 시작해야 하는데, 로그아웃해서 다시 로그인하거나, 컴퓨터를 다시 시작해서 적용한다.

3. Vagrant와 VirtualBox로 우분투 설치

vagrant로 가상머쉰을 만들려면 먼저 Vagrant파일을 만들어야 한다. Vagrant파일을 만들려면 

 

vagrant init

 

명령을 사용해서 만든다. 파일을 편집해서 아래 내용으로 변경하거나, 추가하면

  • config.vm.box = "ubuntu/bionic64"                        # 우분투 18.04(bionic) box 이미지
  • vb.memory = "4096"                                              # 4G의 메모리
  • config.vm.disk: disk, size: "80GB", primary: true   # 80G의 디스크
  • config.vm.network "public network"                       # 퍼블릭 IP 할당

개별서버처럼 동작하는 가상서버를 만들수 있는 Vagrant파일이 생긴다. 아래는 Vagrant파일의 내용인데, Vagrant파일이 만들어지면 자동으로 생성되는 부분 중 필요 없는 부분은 쉽게 볼 수 있도록 # ...로 줄여 놓았다.

 

# -*- 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|
  config.vm.box = "ubuntu/bionic64"

  # ...
  # config.vm.box_check_update = false

  # ...
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # ...
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # ...
  # 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"

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

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

   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 = "4096"
   end

   config.vm.disk :disk, size: '80GB', primary: true

   # ...
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

 

이제, 

 

vagrant up

 

명령을 사용하면, 가상머쉰이 생기고

 

 

vagrant ssh

 

명령을 사용하거나, VirtualBox의 콘솔에서 userid: vagrant, password: vagrant를 입력하면 로그인 할 수 있다.

4. 우분투에 Docker설치

먼저, docker의 apt리포지토리를 등록하고,

 

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get -y install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

 

Docker를 설치하면 된다.

 

 sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

 

만일, Vagrant파일로 docker까지 설치된 가상머쉰을 만들고 싶다면 다음과 같이 수정하면 된다.

 

# -*- 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|
  config.vm.box = "ubuntu/bionic64"

  # ...
  # config.vm.box_check_update = false

  # ...
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # ...
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # ...
  # 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"

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

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

   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 = "4096"
   end

   config.vm.disk :disk, size: '80GB', primary: true

   # ...
   config.vm.provision "shell", inline: <<-SHELL
    sudo apt-get update
    sudo apt-get -y install ca-certificates curl
    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc

    # Add the repository to Apt sources:
    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
        $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
          sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
   SHELL
end