takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

Vagrant で CentOS8 + Docker 環境構築

VagrantVirtualBox に CentOS8 の VM を構築したときの手順メモ

検証環境

Windows10 Home Edition
VirtualBox 6.0.16
Vagrant 2.2.5

Vagrant Box ファイル追加

vagrant box add コマンドで CentOS8 の Box ファイルを追加します。
provider は 5) virtualbox を選択します。

C:\>vagrant box add generic/centos8
==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) docker
2) hyperv
3) libvirt
4) parallels
5) virtualbox
6) vmware_desktop

Enter your choice: 5

vagrant box list コマンドで Box ファイルが追加されているか確認可能です。

C:\>vagrant box list
generic/centos8   (virtualbox, 2.0.6)

Vagrantfile の作成

作業用ディレクトリを作成して vagant init を実行します。

D:\>mkdir vagrant\centos8

D:\>cd vagrant\centos8

D:\vagrant\centos8>vagrant init generic/centos8

作成された Vagrantfile を下記の内容に変更します。
仮想マシンのメモリを 2014MB にしています。
プロビジョニングでは Docker 関連のセットアップを行っています。

--- a/Vagrantfile Sat Feb 01 18:11:48 2020
+++ b/Vagrantfile Sun Feb 02 00:27:02 2020
@@ -32,7 +32,7 @@

   # 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"
+  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
@@ -49,13 +49,13 @@
   # 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
+  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 = "2048"
+  end
   #
   # View the documentation for the provider you are using for more
   # information on available options.
@@ -63,8 +63,18 @@
   # 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.provision "shell", inline: <<-SHELL
+    dnf -y update
+
+    dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
+    dnf -y --nobest install docker-ce
+
+    gpasswd -a vagrant docker
+
+    systemctl start docker
+    systemctl enable docker
+
+    curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
+    chmod 755 /usr/local/bin/docker-compose
+  SHELL
 end

仮想マシン作成

vagrant up コマンドで仮想マシンを作成します。

D:\vagrant\centos8>vagrant up

仮想マシンへログイン

仮想マシン起動後、vagrant ssh コマンドでログインできます。

D:\vagrant\centos8>vagrant ssh

exit でログアウトします。

[vagrant@centos8 ~]$ exit
logout
Connection to 127.0.0.1 closed.

仮想マシンの停止

vagrant halt仮想マシンを停止します。

D:\vagrant\centos8>vagrant halt

Docker の動作確認

プロビジョニングが正常に実行されていれば docker コマンドが使用可能になっています。

[vagrant@centos8 ~]$ docker -v
Docker version 19.03.5, build 633a0ea

[vagrant@centos8 ~]$ docker-compose -v
docker-compose version 1.25.3, build d4d1b42b

[vagrant@centos8 ~]$ docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
bdbbaa22dec6: Pull complete
Digest: sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest

[vagrant@centos8 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              6d5fcfe5ff17        5 weeks ago         1.22MB

[vagrant@centos8 ~]$ docker run --rm busybox echo Hello,World
Hello,World

参考サイト

takaya030.hatenablog.com sig9.hatenablog.com qiita.com