Photo by Danist Soh / Unsplash
Photo by Danist Soh / Unsplash

Build Docker Images Using Ansible and Packer

Build Image from Ansible code and persist them on local or in AWS ECR

Guillaume Vincent
Guillaume Vincent

In a previous article, we have seen how to create a testable Ansible role with Molecule and Docker. This role installs and configures WordPress in a Docker image. This part was only covered tests using Molecule. Here we are going to go further to reuse this work to build the Docker image with Packer.


We will see how to configure Packer through manifests. There will be two manifests, one to build the image locally and the other remotely. We will persist the remote image in AWS Elastic Registry (ECR). For both manifests, we will use the Ansible provisioner and Docker post-processors. The provisioner will reuse the WordPress role. The post-processors will build persist the image in the Docker registry.


We will use Terraform to deploy and configure AWS ECR. Then we will create a Packer manifest to build the image on local.  A docker-compose file will run the image to ensure all is working well. Finally, we will create the second Packer manifest to tag and push the image to AWS ECR.

Install The Prerequisites

The prerequisites presented here are for MacOSX. Adapt versions to your platform architecture.

Install Packer:

$ wget https://releases.hashicorp.com/packer/1.7.2/packer_1.7.2_darwin_amd64.zip -P $HOME/.local/bin
$ unzip $HOME/.local/bin/packer_1.7.2_darwin_amd64.zip -d $HOME/.local/bin
Installation of the Packer binary

Install Terraform :

$ wget https://releases.hashicorp.com/terraform/0.15.4/terraform_0.15.4_darwin_amd64.zip -P $HOME/.local/bin
$ unzip $HOME/.local/bin/terraform_0.15.4_darwin_amd64.zip -d $HOME/.local/bin/
Installation of the Terraform binary

Assume you have python and pip installed and install Ansible :

$ pip install --user ansible==2.9.0
Installation of Ansible using pip

Install docker-compose:

$ pip install --user docker-compose
Installation of Docker-Compose using pip

For Docker, I use Docker for Desktop

Deploy AWS Elastic Container Registry (ECR)

At the project root, create the terraform structure and the aws-ecr module:

$ mkdir -p terraform/environments terraform/modules
$ mkdir -p terraform/environments/dev/aws-ecr
Creation of the aws-ecr layer for development environment

The module state is stored in AWS S3. You have to create your own S3 bucket and replace the value in terraform/environments/dev/aws-ecr/main.tf:

Just below, you have the content of the module aws-ecr files:

Initialize and apply the layer:

$ cd terraform/environments/dev/aws-ecr
$ terraform init
$ terraform apply
Initialization and apply of the aws-ecr layer

Connect to AWS web console and ensure the registry is ready:

The AWS ECR in the web console
The AWS ECR in the web console

Create The Ansible File Hierarchy

At the root of the project, create the ansible directory:

$ mkdir -p ansible/playbooks ansible/roles
Creation of the Ansible roles and playbooks directories

Create the WordPress playbook in ansible/playbooks/wordpress.yml:

The WordPress ansible of the previous article needs to be put into ansible/roles .

Persist The Docker Image Locally

Create the packer directory:

$ mkdir packer
Creation of the packer directory

We create a script to install ansible in the container before running the Ansible provisioned:

$ mkdir scripts
Creation of the scripts directory

Here the scripts/install-ansible.sh :

There is also another script scripts/cleanup.sh to clean up the container after Ansible has finished :

Here is the Packer manifest to build the image:

Build the image on local docker. The image version is passed as a variable :

$ IMAGE_VERSION=latest packer build packer/wordpress-local.json
Build the docker image on local with Packer

You can check the image is present:

$ docker images | grep -i guivin/wordpress
List the Docker image built by Packer

Test Local Image With Docker-Compose

$ docker-compose up
Deploy the docker-compose stack

Once WordPress is ready, you can access it on localhost:

The WordPress home page on localhost:80
The WordPress home page on localhost:80

Persist The Image In AWS ECR

Comparing to the previous manifest, the one for AWS ECR adds docker-push post-processor :

Build the image. The docker repository URL is taken from the outputs of the aws-ecr module and passed as a variable :

$ DOCKER_REPOSITORY=`terraform -chdir=terraform/environments/dev/aws-ecr output -json | jq -r .repository_url.value` IMAGE_VERSION=${VERSION} packer build packer/wordpress.json
Build and push the Docker image to the AWS ECR
The persisted docker image in AWS ECR
The persisted docker image in AWS ECR

Conclusion

We have seen how to reuse an existing Ansible role to create Docker images with Packer. Here we have worked with a local Docker registry and a remote one in AWS ECR deployed with Terraform. The two methods have their own Packer manifest. We have checked the generated image was working with docker-compose.

Now all is in place to keep working on AWS. In the next article of this series, we will see how to deploy the WordPress image in AWS. We will use managed services like AWS Relational Database Service (RDS) and AWS Elastic Container Service (ECS).

Resources

Ansible Local - Provisioners | Packer by HashiCorp
The ansible-local Packer provisioner will run ansible in ansible’s “local” mode on the remote/guest VM using Playbook and Role files that exist on the guest VM. This means Ansible must be installed on the remote/guest VM. Playbooks and Roles can be uploaded from your build machine (the one running P…
Docker - Builders | Packer by HashiCorp
The docker Packer builder builds Docker images using Docker. The builder startsa Docker container, runs provisioners within this container, then exports thecontainer for reuse or commits the image.
Docker Tag - Post-Processors | Packer by HashiCorp
The Packer Docker Tag post-processor takes an artifact from the docker builderthat was committed and tags it into a repository. This allows you to use theother Docker post-processors such as docker-push to push the image to aregistry.
Docker Push - Post-Processors | Packer by HashiCorp
The Packer Docker push post-processor takes an artifact from the docker-importpost-processor and pushes it to a Docker registry.
Cloud

Guillaume Vincent

DevOps Engineer & AWS Certified Solution Architect. Cloud enthusiast and automation addict