Day 14 of 100 Days : Mastering the Dockerfileš
Today, weāre diving into one of Docker's most essential components: the Dockerfile. A Dockerfile is a script-like text document that contains instructions to build a Docker image. Think of it as a blueprint or recipe for creating an environment where your application can run with all its dependencies.
š What is a Dockerfile?
A Dockerfile is a text file that defines the steps needed to assemble a Docker image. It includes commands to specify a base image, install necessary software, copy files, expose ports, and set the applicationās entry point. When Docker reads this file, it runs each instruction to build a custom image, which can then be deployed as a container.
The beauty of a Dockerfile lies in its ability to standardize environmentsāno more "it works on my machine" issues!
š§© Basic Structure of a Dockerfile
Hereās a quick look at the primary instructions you'll often use in a Dockerfile:
FROM: Defines the base image (e.g.,
FROM ubuntu:latest
).WORKDIR: Sets the working directory inside the container.
COPY/ADD: Copies files from the host to the container.
RUN: Executes commands in the container during build time (e.g., installing software).
EXPOSE: Informs Docker that the container listens on a specific network port.
CMD: Specifies the default command to run when a container starts.
ENTRYPOINT: Sets the main command for the container (can be combined with
CMD
for arguments).
š Sample Dockerfiles
Letās look at five examples of Dockerfiles for various use cases. Each one showcases different Dockerfile instructions to solve unique scenarios.
1. Dockerfile for a Basic Python Web Application (Flask)
Suppose you have a simple web app in Flask and want to containerize it.
# Use Python as the base image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the requirements file and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code
COPY . .
# Expose the port Flask will use
EXPOSE 5000
# Define the default command to run the app
CMD ["python", "app.py"]
This Dockerfile:
Uses a lightweight Python base image.
Installs dependencies listed in
requirements.txt
.Sets the working directory and starts the app on port 5000.
2. Dockerfile for a Node.js Application
Imagine youāre working on a Node.js project, and you want to create a Docker environment for it.
# Start with a Node.js image
FROM node:14
# Create and set the app directory
WORKDIR /usr/src/app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the app's port
EXPOSE 3000
# Run the application
CMD ["node", "server.js"]
This Dockerfile:
Utilizes a Node.js base image.
Installs dependencies with
npm install
.Runs the app on port 3000.
3. Dockerfile for Serving Static Files with Nginx
This Dockerfile sets up an Nginx server to serve static files like HTML, CSS, and images.
# Use the official Nginx image
FROM nginx:alpine
# Copy static files to the default nginx public directory
COPY . /usr/share/nginx/html
# Expose port 80 for HTTP access
EXPOSE 80
# Start Nginx server
CMD ["nginx", "-g", "daemon off;"]
This Dockerfile:
Starts with an Nginx base image optimized for size.
Copies the static files to Nginxās default directory.
Exposes port 80 for web traffic.
4. Dockerfile for a Java Application
Hereās a Dockerfile for a Java application built with Maven. This example assumes youāre building a Spring Boot application.
# Use Maven image to build the app
FROM maven:3.8.1-openjdk-11 AS build
WORKDIR /app
# Copy pom.xml and install dependencies
COPY pom.xml .
RUN mvn dependency:go-offline
# Copy the source code and build the application
COPY src ./src
RUN mvn package
# Use a lightweight JRE for running the app
FROM openjdk:11-jre-slim
# Copy the compiled jar file
COPY --from=build /app/target/app.jar /app.jar
# Expose the app port
EXPOSE 8080
# Run the jar file
CMD ["java", "-jar", "/app.jar"]
This Dockerfile:
Uses a multi-stage build to compile the application with Maven and then packages it in a lightweight image for production.
Runs the Spring Boot app on port 8080.
5. Dockerfile for a MySQL Database
For setting up a MySQL database container with a custom configuration.
# Start with MySQL base image
FROM mysql:8.0
# Copy custom configuration file
COPY my.cnf /etc/mysql/my.cnf
# Set root password and database environment variables
ENV MYSQL_ROOT_PASSWORD=my-secret-pw
ENV MYSQL_DATABASE=mydatabase
# Expose port for MySQL
EXPOSE 3306
# Run MySQL
CMD ["mysqld"]
This Dockerfile:
Uses the official MySQL image.
Configures a custom
.cnf
file for MySQL settings.Sets environment variables for the root password and database name.
š§āš« Summary
Each Dockerfile above solves a different scenario by leveraging the flexibility of Docker to create isolated, reliable, and reproducible environments. With Dockerfiles, you can:
Ensure your applications run consistently across different environments.
Minimize dependencies and conflicts.
Streamline your development workflow.
Experiment with these examples and try creating Dockerfiles tailored to your projects!
Subscribe to my newsletter
Read articles from Munilakshmi G J directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Munilakshmi G J
Munilakshmi G J
"Aspiring DevOps Engineer on a 100-day journey to master the principles, tools, and practices of DevOps. Sharing daily insights, practical lessons, and hands-on projects to document my path from beginner to proficient. Passionate about continuous learning, automation, and bridging the gap between development and operations. Join me as I explore the world of DevOps, one day at a time!"