takaya030の備忘録

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

Vagrant で ubuntu 18.04 + Docker 環境構築

VagrantVirtualBoxubuntu 18.04 の VM を構築したときの手順メモ

検証環境

Windows10 Home Edition
VirtualBox 6.0.18
Vagrant 2.2.9

Vagrant Box ファイル追加

vagrant box add コマンドで ubuntu 18.04 の Box ファイルを追加します。

C:\>vagrant box add ubuntu/bionic64

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

C:\>vagrant box list
ubuntu/bionic64   (virtualbox, 20200916.0.0)

Vagrantfile の作成

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

D:\>mkdir vagrant\ubuntu

D:\>cd vagrant\ubuntu

D:\vagrant\ubuntu>vagrant init ubuntu/bionic64

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

--- a/Vagrantfile Mon Sep 21 18:01:11 2020
+++ b/Vagrantfile Mon Sep 21 18:49:46 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,30 @@
   # Enable provisioning with a shell script. Additional provisioners such as
   # Ansible, Chef, Docker, Puppet and Salt 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
+    apt-get remove -y docker docker-engine docker.io containerd runc
+
+    apt-get update -y
+
+    apt-get install -y \
+    apt-transport-https \
+    ca-certificates \
+    curl \
+    gnupg \
+    lsb-release
+
+    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
+
+    echo \
+  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
+  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
+
+   apt-get update -y
+   apt-get install -y docker-ce docker-ce-cli containerd.io
+
+   gpasswd -a vagrant docker
+
+   curl -L "https://github.com/docker/compose/releases/download/1.29.2/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\ubuntu>vagrant up

vagrant ssh コマンドで仮想マシンにログインします。

D:\vagrant\ubuntu>vagrant ssh

exit でログアウトします。

vagrant@ubuntu-bionic:~$ exit
logout
Connection to 127.0.0.1 closed.

仮想マシンの停止

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

D:\vagrant\ubuntu>vagrant halt

Docker の動作確認

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

vagrant@ubuntu-bionic:~$ docker -v
Docker version 19.03.13, build 4484c46d9d

vagrant@ubuntu-bionic:~$ docker-compose -v
docker-compose version 1.29.2, build 5becea4c

vagrant@ubuntu-bionic:~$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

vagrant@ubuntu-bionic:~$ docker run --rm hello-world

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/

参考サイト

takaya030.hatenablog.com

qiita.com

docs.docker.com

変更履歴

  • 2021-06-26 Docker のインストール手順を更新