Skip to content

Instantly share code, notes, and snippets.

@timfanda35
Last active December 27, 2018 12:36
Show Gist options
  • Save timfanda35/13bd264951ab002649d69dfaf3a72a07 to your computer and use it in GitHub Desktop.
Save timfanda35/13bd264951ab002649d69dfaf3a72a07 to your computer and use it in GitHub Desktop.
IND 虛擬化與 Docker 入門

Vagrant Live Demo

Create a Nginx VM

mkdir vagrant-test & cd vagrant-test
vagrant init hashicorp/precise64
vi Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.network "forwarded_port", guest: 80, host: 8088

  config.vm.provider "virtualbox" do |v|
    v.name = "vagrant-test"
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y nginx
    /etc/init.d/nginx start
  SHELL
end
vagrant up
curl localhost:8088

Crate a Nginx VM base on vagrant-test

vagrant box list
vagrant package --output vagrant-test.box
vagrant box add vagrant-test vagrant-test.box
vagrant box list
cd ..
mkdir vagrant-nginx-1 & cd vagrant-nginx-1
vagrant init vagrant-test
vi Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "vagrant-test"
  config.vm.network "forwarded_port", guest: 80, host: 9088

  config.vm.provider "virtualbox" do |v|
    v.name = "vagrant-nginx"
  end

  config.vm.provision "shell", inline: <<-SHELL
    echo "Happy New Year!!!" > /usr/share/nginx/www/index.html
  SHELL
end
vagrant up
curl localhost:9088

Clean

vagrant box remove vagrant-test

Docker Live Demo

CLI & RESTful API

docker images

curl -s \
  --unix-socket /var/run/docker.sock \
  http://localhost/images/json \
  | jq

Run a Nginx Container

docker run --rm --name=nginx -p 8088:80 nginx
curl localhost:8088

Create a Customize Nginx images

docker exec nginx bash -c 'echo "Happy New Year!" > /usr/share/nginx/html/index.html'
docker commit nginx my-nginx

docker run --rm --name=my-nginx -p 9088:80 my-nginx
curl localhost:9088

Run a Nginx Container with volume

mkdir docker-test && cd docker-test
echo "Happy New Year!" > index.html

docker run --rm --name=nginx-v \
  -p 10088:80 \
  -v $(pwd)/index.html:/usr/share/nginx/html/index.html \
  nginx
curl localhost:10088

Create a Customize Nginx images with Dockerfile

vi Dockerfile
FROM nginx

RUN echo "Happy New Year!" > /usr/share/nginx/html/index.html
docker build -t my-nginx2 .
docker run --rm --name=my-nginx2 -p 20088:80 my-nginx2
curl localhost:20088
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment