Day 04:Docker Guide: Dockerfile, Build ARGs, ENV Variables & Portainer Installation
Overview
This document provides step-by-step instructions for creating a Docker environment using a Dockerfile, managing environment variables, and installing Portainer for Docker container management.
Dockerfile for Terraform & Packer Setup
Purpose
This Dockerfile sets up an Ubuntu-based environment with:
Terraform & Packer installation
Required tools:
jq
,net-tools
,curl
,wget
,unzip
,nginx
,iputils-ping
Nginx running in the foreground
Dockerfile Contents
FROM ubuntu:latest
LABEL name="kiran"
ENV AWS_ACCESS_KEY_ID=SDFSDFSDFSDFSDFSDFSDFSDF \
AWS_SECRET_KEY_ID=SDSDSDSDSDSDSDSDSDSDSDSD \
AWS_DEFAULT_REGION=AP-SOUTH-1A
ARG TERRAFORM_VERSION='1.6.6' \
PACKER_VERSION='1.8.0'
RUN apt update && apt install -y jq net-tools curl wget unzip \
&& apt install -y nginx iputils-ping
RUN wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
&& wget https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip \
&& unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip && unzip packer_${PACKER_VERSION}_linux_amd64.zip \
&& chmod 777 terraform && chmod 777 packer \
&& ./terraform version && ./packer version
CMD ["nginx", "-g", "daemon off;"]
Building and Running the Docker Image
Building the Docker Image
Build a Docker image with a specific tag:
docker build -f dockerfile.dev .
docker build -t imkiran13/custom:v1 -f dockerfile.dev .
Build the image without using cache:
docker build -t imkiran13/custom:v1 -f dockerfile.dev . --no-cache
verify terraform and packer version
Passing build arguments:
docker build -t imkiran13/custom:v1 --build-arg TERRAFORM_VERSION=1.4.0 --build-arg PACKER_VERSION=1.5.0 --no-cache -f dockerfile.dev .
Running the Container
Run the container go inside and check Terraform & Packer versions:
docker run --rm -d --name app1 -p 8000:80 imkiran13/custom:v1
docker exec -it app1 bash
./terraform version
./packer version
Building with plain progress output:
docker build --progress=plain -t imkiran13/custom:v1 -f dockerfile.dev .
Setting Environment Variables
Pass environment variables when running the container:
docker run --rm -d --name app2 -p 8001:80 -e AWS_ACCESS_KEY_ID=hidden \
-e AWS_SECRET_KEY_ID=hidden \
-e AWS_DEFAULT_REGION=hidden \
imkiran13/custom:v1
Useful Docker Commands
Adding a Tag
Tag an existing image:
docker tag imkiran13/custom:v1 imkiran13/custom:latest
Check Docker Image History
docker history imkiran13/custom:v1
Cleanup Unused Docker Objects
Remove unused containers, networks, images, and optionally volumes:
docker system prune
Understanding Dockerfile Instructions
ADD: Adds files from the host to the container. Useful for archives that need extraction.
COPY: Copies files from the host system without extraction.
ENTRYPOINT: Configures a container to run as an executable.
WORKDIR: Sets the working directory for subsequent commands.
Conclusion
By following this guide, you can:
Build a Docker image for Terraform & Packer
Manage environment variables and build arguments in a Dockerfile