Skip to content

Commit

Permalink
Day 60 - Docker Containers, Provisioners & Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelCade committed Feb 28, 2022
1 parent 4d14207 commit e2f0167
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 11 deletions.
57 changes: 57 additions & 0 deletions Days/IaC/Docker-Wordpress/docker-wordpress.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "2.16.0"
}
}
}

provider "docker" {}

variable wordpress_port {
default = "8080"
}

resource "docker_volume" "db_data" {
name = "db_data"
}

resource "docker_network" "wordpress_net" {
name = "wordpress_net"
}

resource "docker_container" "db" {
name = "db"
image = "mysql:5.7"
restart = "always"
network_mode = "wordpress_net"
env = [
"MYSQL_ROOT_PASSWORD=wordpress",
"MYSQL_PASSWORD=wordpress",
"MYSQL_USER=wordpress",
"MYSQL_DATABASE=wordpress"
]
mounts {
type = "volume"
target = "/var/lib/mysql"
source = "db_data"
}
}

resource "docker_container" "wordpress" {
name = "wordpress"
image = "wordpress:latest"
restart = "always"
network_mode = "wordpress_net"
env = [
"WORDPRESS_DB_HOST=db:3306",
"WORDPRESS_DB_USER=wordpress",
"WORDPRESS_DB_NAME=wordpress",
"WORDPRESS_DB_PASSWORD=wordpress"
]
ports {
internal = "80"
external = "${var.wordpress_port}"
}
}
Binary file added Days/Images/Day60_IAC2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Days/Images/Day60_IAC3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Days/Images/Day60_IAC4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Days/Images/Day60_IAC5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Days/Images/Day60_IAC6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Days/day58.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Before we start making stuff with Terraform we have to dive a little into HashiCorp Configuration Language (HCL). So far during our challenge we have looked at a few different scripting and programming languages and here is another one. We touched on the [Go programming language](Days/day07.md) then [bash scripts](Days/day19.md) we even touched on a little python when it came to [network automation](Days/day27.md)

Now we must cover HashiCorp Configuration Language (HCL) if this is the first time you are seeing the language it might look a little daunting but its quite simple and ver powerful.
Now we must cover HashiCorp Configuration Language (HCL) if this is the first time you are seeing the language it might look a little daunting but its quite simple and very powerful.

As we move through this section, we are going to be using examples that we can run locally on our system regardless of what OS you are using, we will be using virtualbox albeit not the infrastructure platform you would be using with Terraform it is free and will allow us to achieve what we are looking to do we can also do this with Docker and Kubernetes.

Expand Down
2 changes: 1 addition & 1 deletion Days/day59.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ I have listed a lot of resources down below and I think this topic has been cove
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)

See you on [Day 57](day57.md)
See you on [Day 60](day60.md)
145 changes: 138 additions & 7 deletions Days/day60.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,151 @@

On [Day 59](day59.md) we provisioned a virtual machine using Terraform to our local FREE virtualbox environment. In this section we are going to be deploy a Docker container with some configuration to our local Docker environment.

### Docker Demo

First up we are going to use the code block below, the outcome of the below is that we would like a simple web app to be deployed into docker and to publish this so that it is available to our network. We will be using nginx and we will make this available externally on our laptop over localhost and port 8000. We are using a docker provider from the community and you can see the docker image we are using also stated in our configuration.

```
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "2.16.0"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
```

The first task is to use `terraform init` command to download the provider to our local machine.

![](Images/Day60_IAC1.png)

We then run our `terraform apply` followed by `docker ps` and you can see we have a running container.

Docker demo
![](Images/Day60_IAC2.png)

`terraform init`
If we then open a browser we can navigate to http://localhost:8000/ and you will see we have access to our NGINX container.

![](Images/Day60_IAC3.png)

You can find out more information on the [Docker Provider](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/container)

The above is a very simple demo of what can be done with Terraform plus Docker and how we can now manage this under the Terraform state. We covered docker compose in the containers section and there is a little crossover in a way between this, infrastructure as code as well as then Kubernetes.

For the purpose of showing this and how Terraform can handle a little more complexity, we are going to take the docker compose file for wordpress and mysql that we created with docker compose and we will put this to Terraform. You can find the [docker-wordpress.tf](/Days/IaC/Docker-Wordpress/docker-wordpress.tf)

```
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "2.16.0"
}
}
}
provider "docker" {}
variable wordpress_port {
default = "8080"
}
resource "docker_volume" "db_data" {
name = "db_data"
}
resource "docker_network" "wordpress_net" {
name = "wordpress_net"
}
resource "docker_container" "db" {
name = "db"
image = "mysql:5.7"
restart = "always"
network_mode = "wordpress_net"
env = [
"MYSQL_ROOT_PASSWORD=wordpress",
"MYSQL_PASSWORD=wordpress",
"MYSQL_USER=wordpress",
"MYSQL_DATABASE=wordpress"
]
mounts {
type = "volume"
target = "/var/lib/mysql"
source = "db_data"
}
}
resource "docker_container" "wordpress" {
name = "wordpress"
image = "wordpress:latest"
restart = "always"
network_mode = "wordpress_net"
env = [
"WORDPRESS_DB_HOST=db:3306",
"WORDPRESS_DB_USER=wordpress",
"WORDPRESS_DB_NAME=wordpress",
"WORDPRESS_DB_PASSWORD=wordpress"
]
ports {
internal = "80"
external = "${var.wordpress_port}"
}
}
```

We again put this is in a new folder and then run our `terraform init` command to pull down our provisioners required.

![](Images/Day60_IAC4.png)

We then run our `terraform apply` command and then take a look at our docker ps output we should see our newly created containers.

![](Images/Day60_IAC5.png)

We can then also navigate to our WordPress front end. Much like when we went through this process with docker-compose in the containers section we can now run through the setup and our wordpress posts would be living in our MySQL database.

![](Images/Day60_IAC6.png)

Obviously now we have covered containers and Kubernetes in some detail, we probably know that this is ok for testing but if you were really going to be running a website you would not do this with containers alone and you would look at using Kubernetes to achieve this, Next up we are going to take a look using Terraform with Kubernetes.

![](Images/Day60_IAC1.png)

### Provisioners

Provisioners are there so that if something cannot be declartive we have a way in which to parse this to our deployment.

If you have no other alternative and adding this complexity to your code is the place to go then you can do this by running something similar to the following block of code.

```
resource "docker_container" "db" {
# ...
provisioner "local-exec" {
command = "echo The server's IP address is ${self.private_ip}"
}
}
```

The remote-exec provisioner invokes a script on a remote resource after it is created. This could be used for something OS specific or it could be used to wrap in a configuration management tool. Although notice that we have some of these covered in their own provisioners.

[More details on provisioners](https://www.terraform.io/language/resources/provisioners/syntax)

- file
- local-exec
- remote-exec
Expand All @@ -33,9 +167,6 @@ Another benefit to modules is that you can take these modules and use them on ot

We are breaking down our infrastructure into components, components are known here as modules.




## Resources
I have listed a lot of resources down below and I think this topic has been covered so many times out there, If you have additional resources be sure to raise a PR with your resources and I will be happy to review and add them to the list.

Expand All @@ -50,4 +181,4 @@ I have listed a lot of resources down below and I think this topic has been cove
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)

See you on [Day 57](day57.md)
See you on [Day 61](day61.md)
2 changes: 1 addition & 1 deletion Days/day61.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ I have listed a lot of resources down below and I think this topic has been cove
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)

See you on [Day 57](day57.md)
See you on [Day 62](day62.md)
2 changes: 1 addition & 1 deletion Days/day62.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ I have listed a lot of resources down below and I think this topic has been cove
- [Terraform Tutorial - The Best Project Ideas](https://www.youtube.com/watch?v=oA-pPa0vfks)
- [Awesome Terraform](https://github.com/shuaibiyy/awesome-terraform)

See you on [Day 57](day57.md)
See you on [Day 63](day63.md)

0 comments on commit e2f0167

Please sign in to comment.