BETA

VagrantでDockerを使うメモ

投稿日:2020-03-19
最終更新:2020-03-19

特に難しいことはしていない。

  • Vagrantで仮想サーバを立ち上げる
    • Vagrant on Windows10
    • CentOS7
  • Dockerインストール
  • docker run
    • nginx:latest

Vagrant 仮想サーバ設定~起動

ごりごりの初期設定、本当は作成済みだったVagrantの仮想サーバでDockerコンテナをrunさせようとしたけど、ネットワーク設定がうまくいかず時間がかかったので新規で立ち上げた。

あと、ファイアウォールはiptablesで設定してたけどCentOS7でデフォルトのFirewalldをいじってみる。(余談)

$ mkdir docker  
$ cd docker  
$ vagrant init  
$ ls  
Vagrantfile  
$ vim Vagrantfile  
# -*- 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|  
  # The most common configuration options are documented and commented below.  
  # For a complete reference, please see the online documentation at  
  # https://docs.vagrantup.com.  

  # Every Vagrant development environment requires a box. You can search for  
  # boxes at https://vagrantcloud.com/search.  
  config.vm.box = "centos/7"  

  # Disable automatic box update checking. If you disable this, then  
  # boxes will only be checked for updates when the user runs  
  # `vagrant box outdated`. This is not recommended.  
  # config.vm.box_check_update = false  

  # Create a forwarded port mapping which allows access to a specific port  
  # within the machine from a port on the host machine. In the example below,  
  # accessing "localhost:8080" will access port 80 on the guest machine.  
  # NOTE: This will enable public access to the opened port  
  # config.vm.network "forwarded_port", guest: 80, host: 8080  

  # Create a forwarded port mapping which allows access to a specific port  
  # within the machine from a port on the host machine and only allow access  
  # via 127.0.0.1 to disable public access  
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"  

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

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

  # Share an additional folder to the guest VM. The first argument is  
  # the path on the host to the actual folder. The second argument is  
  # the path on the guest to mount the folder. And the optional third  
  # argument is a set of non-required options.  
  # config.vm.synced_folder "../data", "/vagrant_data"  
  config.vm.synced_folder ".", "/vagrant", type: "virtualbox"  

  # Provider-specific configuration so you can fine-tune various  
  # 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  
  #  
$ vagrant up # 仮想サーバ起動  
$ vagrant ssh # ログイン  

Docker 導入~コンテナ起動

Get Docker Engine - Community for CentOS

$ su - # ルートユーザでログイン  
# yum update  
# yum install -y yum-utils device-mapper-persistent-data lvm2 # 必要なパッケージの事前インストール  
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo #リポジトリ登録  
# yum install -y docker-ce docker-ce-cli containerd.io # Dockerインストール  
# docker version  
Client: Docker Engine - Community  
 Version:           19.03.8  
 API version:       1.40  
 Go version:        go1.12.17  
 Git commit:        afacb8b  
 Built:             Wed Mar 11 01:27:04 2020  
 OS/Arch:           linux/amd64  
 Experimental:      false  

Server: Docker Engine - Community  
 Engine:  
  Version:          19.03.8  
  API version:      1.40 (minimum version 1.12)  
  Go version:       go1.12.17  
  Git commit:       afacb8b  
  Built:            Wed Mar 11 01:25:42 2020  
  OS/Arch:          linux/amd64  
  Experimental:     false  
 containerd:  
  Version:          1.2.13  
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429  
 runc:  
  Version:          1.0.0-rc10  
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd  
 docker-init:  
  Version:          0.18.0  
  GitCommit:        fec3683  

# docker run --name nginx -d -p 8080:80 nginx:latest # コンテナでNginxサーバ起動、ホストからコンテナまでは8080⇔80で通信する。  

ブラウザからhttp://192.168.33.10:8080 へアクセス

終わり。

技術ブログをはじめよう Qrunch(クランチ)は、プログラマの技術アプトプットに特化したブログサービスです
駆け出しエンジニアからエキスパートまで全ての方々のアウトプットを歓迎しております!
or 外部アカウントで 登録 / ログイン する
クランチについてもっと詳しく

この記事が掲載されているブログ

@itanomaの技術ブログ

よく一緒に読まれる記事

0件のコメント

ブログ開設 or ログイン してコメントを送ってみよう