Vagrant で ubuntu 20.04 + Docker 環境構築
Vagrant で VirtualBox に ubuntu 20.04 の VM を構築したときの手順メモ
検証環境
Windows10 Home Edition (version 21H1)
chocolatey のインストール
こちらのサイトからインストールします
自分は PowerShell のインストールコマンドでインストールしました
Windows のソフトのインストール
PowerShell を管理者として実行
chocolatey で VirtualBox、vagrant、docker-cli、docker-compose をインストールする
PS D:\> choco install -y virtualbox PS D:\> choco install -y vagrant PS D:\> choco install -y docker-cli PS D:\> choco install -y docker-compose
インストールが完了したら各ツールの動作確認
D:\>vagrant version Installed Version: 2.2.18 Latest Version: 2.2.18 You're running an up-to-date version of Vagrant! D:\>docker -v Docker version 19.03.12, build 0ed913b8- D:\>docker-compose -v docker-compose version 1.29.2, build 5becea4c
vagrant のプラグインのインストール
D:\>vagrant plugin install vagrant-disksize D:\>vagrant plugin install vagrant-vbguest D:\>vagrant plugin install vagrant-docker-compose
インストールされたか確認
D:\>vagrant plugin list vagrant-disksize (0.1.3, global) vagrant-docker-compose (1.5.1, global) vagrant-vbguest (0.30.0, global)
Vagrantfile
の作成
作業用ディレクトリを作成して vagant init
を実行します
D:\>mkdir vagrant\focal64 D:\>cd vagrant\focal64 D:\vagrant\focal64>vagrant init ubuntu/focal64
作成された Vagrantfile を下記の内容に変更します。
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "ubuntu/focal64" config.vm.box_check_update = false config.vm.network "private_network", ip: "192.168.33.11" 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 config.vm.provision :docker config.vm.provision :docker_compose, compose_version: "1.29.2" config.vm.provision "shell", inline: <<-SHELL apt-get update -y gpasswd -a vagrant docker mkdir -p /etc/systemd/system/docker.service.d touch /etc/systemd/system/docker.service.d/options.conf echo '[Service]' > /etc/systemd/system/docker.service.d/options.conf echo 'ExecStart=' >> /etc/systemd/system/docker.service.d/options.conf echo 'ExecStart=/usr/bin/dockerd -H unix://' >> /etc/systemd/system/docker.service.d/options.conf touch /etc/docker/daemon.json echo '{"log-driver":"json-file","log-opts":{"max-size":"10m","max-file":"3"}}' > /etc/docker/daemon.json chmod 600 /etc/docker/daemon.json systemctl daemon-reload systemctl restart docker if [ ! -d "/home/vagrant/google-cloud-sdk" ]; then curl https://sdk.cloud.google.com > /tmp/install.sh sudo -u vagrant -i bash /tmp/install.sh --disable-prompts --install-dir=/home/vagrant sudo -u vagrant -i echo '# The next line updates PATH for the Google Cloud SDK.' >> /home/vagrant/.bashrc sudo -u vagrant -i echo 'if [ -f '\\''/home/vagrant/google-cloud-sdk/path.bash.inc'\\'' ]; then . '\\''/home/vagrant/google-cloud-sdk/path.bash.inc'\\''; fi' >> /home/vagrant/.bashrc sudo -u vagrant -i echo '# The next line enables shell command completion for gcloud.' >> /home/vagrant/.bashrc sudo -u vagrant -i echo 'if [ -f '\\''/home/vagrant/google-cloud-sdk/completion.bash.inc'\\'' ]; then . '\\''/home/vagrant/google-cloud-sdk/completion.bash.inc'\\''; fi' >> /home/vagrant/.bashrc rm -vf /tmp/install.sh fi SHELL end
仮想マシンの起動
vagrant up
で仮想マシンを起動します
D:\vagrant\focal64>vagrant up
vagrant ssh
で仮想マシンにログインします。
D:\vagrant\focal64>vagrant ssh
Docker
の動作確認
プロビジョニングが正常に実行されていれば docker
コマンドが使用可能になっています
vagrant@ubuntu-focal:~$ docker -v Docker version 20.10.8, build 3967b7d vagrant@ubuntu-focal:~$ docker-compose -v docker-compose version 1.29.2, build 5becea4c vagrant@ubuntu-focal:~$ docker run --rm hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world b8dfde127a29: Pull complete Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1 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/
Windows からのリモート接続の確認
Windows の docker コマンドが ubuntu の docker エンジンにリモート接続可能か確認します
D:\>set DOCKER_HOST=tcp://192.168.33.11:2375 D:\>docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest d1165f221234 6 months ago 13.3kB D:\>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/