Skip to main content

Command Palette

Search for a command to run...

Day 04:Docker Guide: Dockerfile, Build ARGs, ENV Variables & Portainer Installation

Updated
โ€ข2 min read
Day 04:Docker Guide: Dockerfile, Build ARGs, ENV Variables & Portainer Installation
K

"Hello, I'm Kiran Pawar, a passionate Cloud and Devops Engineer with a strong background in cloud automation, configuration, and deployment. My journey in the world of technology has been a thrilling adventure, where I've had the privilege to work with cutting-edge tools and practices.

๐Ÿš€ As a DevOps Engineer:

I specialize in automating, configuring, and deploying instances in cloud environments and data centers. My expertise extends to DevOps, GitOps, CI/CD pipeline management, HashiCorp Terraform, and containerization. I'm proficient in AWS and Linux/Unix administration, ensuring robust infrastructure and application performance.

๐Ÿ”ง My Tech Stack:

Front-end skills: HTML, CSS, SCSS, Tailwind CSS, Bootstrap, React, Material-UI, JavaScript DevOps toolbox: GIT, OWASP,Nexus,Trivy, Github, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus, Grafana, Argo CD, AWS EKS.

๐ŸŒ My Cloud Expertise:

I have hands-on experience managing AWS services, including EC2, S3, EBS, VPC, ELB, RDS, IAM, Route53, and more.

๐Ÿ”’ Networking and Security:

My skills include managing networking concepts such as TCP/IP protocols, security policies, and subnet interfacing. I have a strong understanding of infrastructure and networking, covering topics like firewalls, IP addressing, DNS, and more.

๐Ÿ’ก What Sets Me Apart:

I bring a positive attitude, a strong work ethic, and a collaborative spirit to every project. I'm a self-starter, a fast learner, and an effective team player with strong interpersonal skills. In addition to my DevOps skills, I've developed shell scripts (Bash) for automating tasks and have proficiency in Python scripting. My ability to communicate and manage projects, along with a track record of resolving client issues, adds value to every team I work with. If you're looking for a DevOps engineer who is also well-versed in front-end technologies, feel free to connect with me. Let's explore new possibilities and create exceptional technical solutions together!"

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

More from this blog

Kiran Pawar's Blog

122 posts