Terraform is a widely used Infrastructure as Code tool for provisioning infrastructure. WitCentre is taking some steps into delivering useful contents on Terraform from the beginning. With that vision, this particular blog will simply explain how to do some deployments through Terraform. Similar to all other places, 'Hello World!' sample will be a good start and will be quite different from other situations. Make sure to read all steps to have some joy with it!
Prerequisites
If you are practicing the content of this blog, make sure to meet following prerequisites beforehand. Trust me, this will be a fun of leaning!
- Docker
- Terraform
NOTE: Following steps are dedicated for a Ubunutu environment. Make sure to adjust them for your environment.
What will be doing?
Step 1: Build a Docker image locally
mkdir witcentre-terraform-1
cd witcentre-terraform-1 && mkdir docker
# Dockerfile
FROM nginx
COPY index.html /usr/share/nginx/witcentre/index.html
COPY witcentre.com.conf /etc/nginx/conf.d/witcentre.com.conf
# index.html
<!DOCTYPE html>
<html>
<head>
<h1>Hello World!</h1>
</head>
</html>
# witcentre.com.config
server {
listen 8003;
location / {
root /usr/share/nginx/witcentre;
index index.html index.html;
}
}
4. Build the docker image using the following command. Use it inside the same 'docker' directory.
docker build -t hello-world-nginx .
Step 2: Create a docker container through Terraform
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.1"
}
}
}
data "docker_image" "nginx" {
name = "hello-world-nginx"
}
resource "docker_container" "nginx" {
image = data.docker_image.nginx.id
name = "witcentre-tutorial"
ports {
internal = 8003
external = 8003
}
}
terraform init
3. Excute following command to continue with creating the required container.
terraform apply
Terraform will perform the following actions:# docker_container.nginx will be created+ resource "docker_container" "nginx" {+ attach = false+ bridge = (known after apply)+ command = (known after apply)+ container_logs = (known after apply)+ container_read_refresh_timeout_milliseconds = 15000+ entrypoint = (known after apply)+ env = (known after apply)+ exit_code = (known after apply)+ hostname = (known after apply)+ id = (known after apply)+ image = "sha256:bb986769a8f8b7c7d72a0aaba3894c74265a031fb81e6504debedc0ee14f8619"+ init = (known after apply)+ ipc_mode = (known after apply)+ log_driver = (known after apply)+ logs = false+ must_run = true+ name = "witcentre-tutorial"+ network_data = (known after apply)+ read_only = false+ remove_volumes = true+ restart = "no"+ rm = false+ runtime = (known after apply)+ security_opts = (known after apply)+ shm_size = (known after apply)+ start = true+ stdin_open = false+ stop_signal = (known after apply)+ stop_timeout = (known after apply)+ tty = false+ wait = false+ wait_timeout = 60+ ports {+ external = 8003+ internal = 8003+ ip = "0.0.0.0"+ protocol = "tcp"}}Plan: 1 to add, 0 to change, 0 to destroy.Do you want to perform these actions?Terraform will perform the actions described above.Only 'yes' will be accepted to approve.Enter a value:
Post A Comment:
0 comments: