Sitemap

[LAB] Linux, Docker Desktop, IaaS, Terraform and Ansible

3 min read2 days ago

--

Well, when I started writing this article, i was prearing my dinner , so my first thought was: let’s make a recipe. And actually, it kinda will be something like that, since we will have our ingredients, flavors (versions), preparation and cooking time, taste (or literally test), and serve/deliver.

But first, Mise en place:

We will need:

  1. One Windows or Linux instance. I recommend WSL with Ubuntu, but if you are already on Ubuntu, you can skip this part
wsl --install
wsl --status
wsl --list --verbose
wsl --list --online
wsl --install -d Ubuntu

2. On our Ubuntu system, we need to prepare the environment. We will need some ingredients here:

sudo apt update
sudo apt upgrade -y
sudo apt install -y git curl unzip python3 python3-pip python3-venv
mkdir -p ~/lab/{terraform,ansible,python}
cd ~/lab

3. Docker desktop

Don’t forget to enable WSL in Settings → Resources → WSL Integration and enable Ubuntu.

docker version
docker run --rm hello-world

And voilà, we have our base to start! But until now we haven’t done much, right? Just chopped a few onions. So let’s add a few more components here.

On the HashiCorp site, we can use the official environment setup for our workspace:

sudo apt-get update
sudo apt-get install -y gnupg software-properties-common wget

wget -O- https://apt.releases.hashicorp.com/gpg \
| gpg --dearmor \
| sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" \
| sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update
sudo apt install -y terraform
terraform version

4. Ansible

Let’s use pipx so we don’t mix it with other Python packages. I will explain this later.

sudo apt install -y pipx
pipx ensurepath

#close and open again the ubutnu terminal after the install
pipx install --include-deps ansible
ansible --version
ansible-playbook --version

Taste… I mean, test everything to see if everything was installed successfully.

git --version
python3 --version
terraform version
ansible --version
docker version
docker run --rm hello-world

Okay, we can turn on the oven and start with the basics!

Terraform: provision a test environment

Objective: create, modify, and destroy a resource in a predictable way.

Get Victor S. Paiva’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s start building a container.

cd ~/lab/terraform
nano main.tf

If you want more details about how to use a specific version with a provider, you can check the links below for the official HashiCorp Tutorial:

Take a few minutes to read [the documentation], but here is my version. You can use vim or nano main.tf, as I said before:

terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 4.2.0"
}
}
}

provider "docker" {}

resource "docker_image" "test" {
name = "nginx:alpine"
keep_locally = true
}

resource "docker_container" "test" {
name = "bosch-test-web"
image = docker_image.test.image_id

ports {
internal = 80
external = 8080
}
}

Ctrl+O, Enter e exit with Ctrl+X

Now taste..I mean, test it to see each resource we have:

terraform fmt
terraform init
terraform validate
terraform plan
terraform apply

In our browser, we will be able to see our environment already at http://localhost:8080 (make sure port 8080 is available, or set it to 8081 if you are already using 8080 for another purpose).

Lab 2

Lyfecicle

terraform show
terraform state list
docker ps

#modify the port 8080 to 8081 and them
terraform plan
terraform apply

#last but not least delete it
terraform destroy

Credits:

Many thanks to @Pranav Bakar which gave me some insights to follow this lab with his article

--

--

Victor S. Paiva
Victor S. Paiva

Hello There! I'm Victor, An Senior Application Support Engineer, here you can find some tutorials i'm sharing with the community