0
0
Dockerdevops~15 mins

ARG and ENV instructions in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Using ARG and ENV Instructions in Dockerfiles
📖 Scenario: You are creating a Docker image for a simple web application. You want to use build-time variables and environment variables inside the Dockerfile to customize the image.
🎯 Goal: Build a Dockerfile that uses ARG to accept a build-time variable and ENV to set an environment variable inside the container.
📋 What You'll Learn
Create a build argument named APP_VERSION with default value 1.0
Set an environment variable named APP_ENV with value production
Use the APP_VERSION argument to label the image
Print the values of APP_VERSION and APP_ENV inside the container
💡 Why This Matters
🌍 Real World
Dockerfiles often need to accept variables at build time and set environment variables for the running container. This project shows how to do both simply.
💼 Career
Understanding ARG and ENV is essential for DevOps roles that involve containerization and building Docker images for deployment.
Progress0 / 4 steps
1
Create a Dockerfile with base image
Create a Dockerfile starting with the base image alpine:latest.
Docker
Need a hint?

The first line in a Dockerfile usually specifies the base image using FROM.

2
Add ARG instruction for build-time variable
Add an ARG instruction named APP_VERSION with default value 1.0.
Docker
Need a hint?

Use ARG APP_VERSION=1.0 to define a build argument with a default value.

3
Use ARG to label image and add ENV instructions
Add a LABEL instruction using APP_VERSION to label the image, ENV APP_VERSION=${APP_VERSION} to make it available at runtime, and ENV APP_ENV=production.
Docker
Need a hint?

Use LABEL version=${APP_VERSION} to label with the ARG, ENV APP_VERSION=${APP_VERSION} to expose ARG at runtime, and ENV APP_ENV=production.

4
Print ARG and ENV values in container
Add a CMD instruction to print the values of APP_VERSION and APP_ENV using echo.
Docker
Need a hint?

Use CMD echo "App version is $APP_VERSION and environment is $APP_ENV" to print the variables.