Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39b14db

Browse files
committed
reinit
1 parent 117b208 commit 39b14db

File tree

5 files changed

+171
-43
lines changed

5 files changed

+171
-43
lines changed

README.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,64 @@
1-
Vagrant Sandbox
1+
Vagrant Terraform Sandbox
22
=========
33

44

55
----------------
66

7-
Vagrant Terraform Docker Nginx Sandbox
7+
Vagrant Terraform Docker Nginx Sandbox.
8+
Locally running terraform all-in-one.
9+
810
----------------
911

12+
~~~~
13+
Commands:
14+
15+
- vagrant ssh
16+
17+
cd /vagrant
18+
- sudo terraform init
19+
- sudo terraform plan -out nginx.tfplan
20+
- sudo terraform apply nginx.tfplan
21+
- sudo terraform show
22+
- sudo terraform destroy
23+
24+
$ sudo docker image ls
25+
REPOSITORY TAG IMAGE ID CREATED SIZE
26+
nginx latest 98ebf73aba75 4 days ago 109MB
27+
$ sudo docker container ls
28+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29+
7cdc5352bdaf 98ebf73aba75 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-server
30+
$ sudo docker exec -it 7cdc5352bdaf bash
31+
root@7cdc5352bdaf:/#
32+
33+
cd /vagrant/terraform-nginx
34+
- sudo terraform init
35+
- sudo terraform plan
36+
- sudo terraform apply
37+
- sudo terraform show
38+
- sudo terraform destroy
39+
40+
cd /vagrant/terraform-nginx-multiple
41+
- sudo terraform init
42+
- sudo terraform plan
43+
- sudo terraform apply -auto-approve
44+
- sudo terraform show
45+
- sudo terraform destroy -auto-approve
46+
$ terraform show | grep external
47+
external = 32769
48+
external = 32768
49+
50+
Browse
51+
http://192.168.45.10:32769/
52+
http://192.168.45.10:32768/
1053
11-
Command:
54+
$ sudo docker container ls
55+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56+
566b78760244 98ebf73aba75 "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 0.0.0.0:32768->80/tcp nginx-terraform-01
57+
98b5431eaa20 98ebf73aba75 "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 0.0.0.0:32769->80/tcp nginx-terraform-02
1258
13-
- terraform init
14-
- terraform plan -out nginx.tfplan
15-
- terraform apply "nginx.tfplan"
16-
- terraform destroy
1759
60+
Browse http://192.168.45.10
61+
~~~~
1862

1963
License
2064
-------

Vagrantfile

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
11
# -*- mode: ruby -*-
22
# vi: set ft=ruby :
33
$script = <<SCRIPT
4-
# Install Docker
5-
sudo apt-get update
6-
sudo apt-get remove docker docker-engine docker.io
7-
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y
8-
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
9-
sudo apt-key fingerprint 0EBFCD88
10-
sudo add-apt-repository \
11-
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
12-
$(lsb_release -cs) \
13-
stable"
14-
sudo apt-get update
15-
sudo apt-get install docker-ce -y
16-
docker --version
17-
# Install Terraform
18-
sudo apt-get install unzip -y
19-
sudo wget -q -nc https://releases.hashicorp.com/terraform/0.11.10/terraform_0.11.10_linux_amd64.zip
20-
sudo unzip terraform_0.11.10_linux_amd64.zip
21-
sudo mv terraform /usr/local/bin/
22-
sudo terraform version
23-
# Run terraform templates
24-
cd /vagrant
25-
# Initialize the working directory.
26-
sudo terraform init
27-
# Create a plan and save it to the local file tfplan.
28-
sudo terraform plan -out nginx.tfplan
29-
# Apply the plan stored in the file tfplan.
30-
sudo terraform apply "nginx.tfplan"
31-
sudo docker ps -a
32-
# sudo terraform destroy
4+
# Install Docker
5+
sudo apt-get update
6+
sudo apt-get remove docker docker-engine docker.io
7+
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y
8+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
9+
apt-key fingerprint 0EBFCD88
10+
sudo add-apt-repository \
11+
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
12+
$(lsb_release -cs) \
13+
stable"
14+
sudo apt-get update
15+
sudo apt-get install docker-ce -y
16+
docker --version
17+
# Install Terraform
18+
sudo apt-get install unzip -y
19+
wget -q -nc https://releases.hashicorp.com/terraform/0.12.5/terraform_0.12.5_linux_amd64.zip
20+
unzip terraform_0.12.5_linux_amd64.zip
21+
sudo mv terraform /usr/local/bin/
22+
terraform version
23+
# Run terraform templates
24+
cd /vagrant && sudo terraform init
25+
# Create a plan and save it to the local file tfplan.
26+
sudo terraform plan -out nginx.tfplan
27+
# Apply the plan stored in the file tfplan.
28+
sudo terraform apply -auto-approve nginx.tfplan
29+
sudo docker ps -a
3330
SCRIPT
3431
Vagrant.configure(2) do |config|
3532
config.vm.box_check_update = false
3633
config.vm.box = "bento/ubuntu-18.04"
3734
config.vm.hostname = "terraform-sandbox"
3835
config.vm.network "private_network", ip: "192.168.45.10"
36+
# config.vm.network "forwarded_port", guest: 80, host: 80
3937
config.vm.provider "virtualbox" do |vb|
4038
vb.gui = false
4139
vb.name = "terraform-sandbox"
42-
vb.memory = "1024"
40+
vb.memory = "512"
4341
vb.cpus = 1
4442
end
4543
config.vm.provision "shell", inline: $script, privileged: false

nginx.tf

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# Configure Docker provider and connect to the local Docker socket
22
provider "docker" {
3+
version = "~> 2.0"
34
host = "unix:///var/run/docker.sock"
5+
alias = "nginx"
46
}
57

8+
# Configure nginx image
9+
resource "docker_image" "nginx" {
10+
name = "nginx:latest"
11+
}
12+
13+
614
# Create an Nginx container
715
resource "docker_container" "nginx" {
816
image = "${docker_image.nginx.latest}"
9-
count = 2
10-
name = "nginx-server-${count.index+1}"
17+
name = "nginx-server"
1118
ports {
1219
internal = 80
1320
external = 80
1421
}
1522
}
16-
17-
resource "docker_image" "nginx" {
18-
name = "nginx:latest"
19-
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Configure Docker provider and connect to the local Docker socket
2+
provider "docker" {
3+
version = "~> 2.0"
4+
host = "unix:///var/run/docker.sock"
5+
alias = "nginx-v1"
6+
}
7+
8+
# Configure nginx image
9+
resource "docker_image" "nginx" {
10+
name = "nginx:latest"
11+
}
12+
13+
14+
# Create an Nginx container
15+
resource "docker_container" "nginx" {
16+
image = "${docker_image.nginx.latest}"
17+
# name = "nginx"
18+
count = 2
19+
name = "nginx-terraform-${format("%02d", count.index+1)}"
20+
21+
env = ["env=test", "role=test"]
22+
restart = "always"
23+
memory = 128
24+
25+
26+
ports {
27+
internal = 80
28+
}
29+
30+
volumes {
31+
container_path = "/usr/share/nginx/html"
32+
host_path = "/home/scrapbook/tutorial/www"
33+
read_only = true
34+
}
35+
36+
log_driver = "json-file"
37+
log_opts = {
38+
max-size = "1m"
39+
max-file = 2
40+
}
41+
42+
}

terraform-nginx/nginx_single.tf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Configure Docker provider and connect to the local Docker socket
2+
provider "docker" {
3+
version = "~> 2.0"
4+
host = "unix:///var/run/docker.sock"
5+
alias = "nginx-v1"
6+
}
7+
8+
# Configure nginx image
9+
resource "docker_image" "nginx" {
10+
name = "nginx:latest"
11+
}
12+
13+
14+
# Create an Nginx container
15+
resource "docker_container" "nginx" {
16+
image = "${docker_image.nginx.latest}"
17+
name = "nginx"
18+
19+
env = ["env=test", "role=test"]
20+
restart = "always"
21+
memory = 128
22+
23+
24+
ports {
25+
internal = 80
26+
external = 80
27+
}
28+
29+
volumes {
30+
container_path = "/usr/share/nginx/html"
31+
host_path = "/home/scrapbook/tutorial/www"
32+
read_only = true
33+
}
34+
35+
log_driver = "json-file"
36+
log_opts = {
37+
max-size = "1m"
38+
max-file = 2
39+
}
40+
41+
}

0 commit comments

Comments
 (0)