Skip to main content

Dockerfile

  

Understanding Dockerfile: A Complete Guide with Example and Cheat Sheet

Introduction to Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Why Use a Dockerfile?

Creating a Dockerfile allows you to automate the installation of software in a new Docker image. This ensures that the image gets built the same way every time you run the command, making it repeatable and more reliable than manual builds.

Components of a Dockerfile

Here are some of the key instructions you can use in a Dockerfile:

  • FROM: Sets the Base Image for subsequent instructions.
  • RUN: Executes commands in a new layer on top of the current image.
  • CMD: Provides defaults for an executing container.
  • LABEL: Adds metadata to an image.
  • EXPOSE: Informs Docker that the container listens on the specified network ports.
  • ENV: Sets the environment variable.
  • ADD: Copies new files, directories, or remote file URLs and adds them to the filesystem of the image.
  • COPY: Copies new files or directories and adds them to the filesystem of the container.
  • ENTRYPOINT: Allows you to configure a container that will run as an executable.
  • VOLUME: Creates a mount point with the specified name and marks it as holding externally mounted volumes from the native host or other containers.
  • USER: Sets the user name or UID to use when running the image and for any RUNCMD, and ENTRYPOINT instructions that follow it.
  • WORKDIR: Sets the working directory.
  • ARG: Defines a variable that users can pass at build-time to the builder with the docker build command.
  • ONBUILD: Adds a trigger instruction when the image is used as the base for another build.
  • STOPSIGNAL: Sets the system call signal that will be sent to the container to exit.
  • HEALTHCHECK: Tells Docker how to test a container to check that it is still working.
  • SHELL: Allows the shell to be overridden.

Example Dockerfile

Let's consider a Dockerfile for a simple Python Flask application:

# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory to /app
WORKDIR /app 
# Copy the current directory contents into the container at /app
COPY . /app 
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt 
# Make port 80 available to the world outside this container
EXPOSE 80 
# Define environment variable
ENV NAME World 
# Run app.py when the container launches
CMD ["python", "app.py"]



This Dockerfile creates a Docker image that includes a Python environment with Flask installed, sets up a working directory, copies in the application code, installs dependencies, and specifies how to run the application.

Dockerfile Commands Cheat Sheet

Here's a cheat sheet with common Dockerfile commands and their usage:

CommandUsageExample
FROMInitializes a new build stage and sets the Base ImageFROM ubuntu:18.04
RUNExecutes commands in a new layerRUN apt-get update && apt-get install -y git
CMDProvides defaults for executing containerCMD ["python", "app.py"]
LABELAdds metadata to an imageLABEL maintainer="john.doe@example.com"
EXPOSEIndicates the ports on which a container listensEXPOSE 80
ENVSets environment variablesENV API_KEY="SECRET_GIVEN"
ADDCopies files, directories, or remote file URLsADD ./localdir /containerdir
COPYCopies new files or directoriesCOPY ./localdir /containerdir
ENTRYPOINTConfigures a container that will run as an executableENTRYPOINT ["executable", "param1", "param2"]
VOLUMECreates a mount pointVOLUME /data
USERSets the UID or usernameUSER 1001
WORKDIRSets the working directoryWORKDIR /path/to/workdir
ARGDefines a variable to pass to the builderARG version=1.0
ONBUILDAdds a trigger instructionONBUILD RUN /usr/local/bin/python-build --dir /app
STOPSIGNALSets the system call signal that will be sent to the

Comments

Popular posts from this blog

DevOps Vs DevSecOps

   DevOps and DevSecOps are two methodologies that have gained traction in the IT industry for streamlining software development and deployment. However, their approach to security and operations differs, making each suitable for different types of projects and organizational needs. Let's explore DevOps versus DevSecOps with a real-time example, focusing on their distinctions, integration, and practical applications. DevOps: The Foundation DevOps is a cultural and professional movement that emphasizes collaboration and communication between software developers and other IT professionals while automating the process of software delivery and infrastructure changes. It aims to shorten the development life cycle and provide continuous delivery with high software quality. Core Principles: Continuous Integration and Continuous Deployment (CI/CD): Automate building, testing, and deployment of applications. Collaboration: Breaking down silos between teams (developers, IT operations...

Deploying a Node.js project to Azure App Services using Azure DevOps pipelines

Deploying a Node.js project to Azure App Services using Azure DevOps pipelines is a robust way to automate deployment processes and integrate continuous integration and deployment (CI/CD) practices into your workflow. This guide will walk you through the setup of an Azure DevOps pipeline to deploy a Node.js application from GitHub or Azure Repos to Azure App Services. Prerequisites Before you begin, ensure you have the following: An Azure account. You can sign up for a free account here . A GitHub or Azure Repos account with your Node.js project. An Azure DevOps account. Create one here if you don't have it. Step 1: Prepare Your Node.js Application Make sure your Node.js application is ready and includes a package.json file in the root. This file is crucial as it contains dependency information and scripts needed for your application. Step 2: Create an Azure Web App Log into Azure Portal: Visit https://portal.azure.com . Create a Web App: Click on "Create a resource". ...

Git Cheat Sheet

  Git Cheat Sheet Category Command Description Setup git config --global user.name "[name]" Set a name that will be attached to your commits and tags. git config --global user.email "[email]" Set an email that will be attached to your commits and tags. git init Initialize a new git repository in the current directory. git clone [url] Clone a repository into a new directory. Stage & Snapshot git status Show modified files in the working directory, staged for your next commit. git add [file] Add a file as it looks now to your next commit (stage). git reset [file] Unstage a file while retaining the changes in the working directory. git diff Show diff of what is changed but not staged. git diff --staged Diff of what is staged but not yet committed. git commit -m "[message]" Commit your staged content as a new commit snapshot. Branch & Merge git branch List all of the branches in your repo. git branch [name] Create a new branch at the current commit. gi...