Microsoft Developer Network

Your source for technical documentation and learning

Dockerfile Essentials

Welcome to this guide on Dockerfile essentials. Dockerfiles are the backbone of building Docker images. Understanding how to write efficient and effective Dockerfiles is crucial for any developer working with containerization.

What is a Dockerfile?

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Docker uses this file to build an image automatically. Each instruction in the Dockerfile creates a layer in the image.

Key Dockerfile Instructions

Here are some of the most fundamental instructions you'll use in your Dockerfiles:

Best Practices

Follow these guidelines to create efficient and secure Docker images:

Note: The order of instructions in a Dockerfile matters significantly for build cache utilization and image layer efficiency.

Example Dockerfile

Here's a simple Dockerfile for a Node.js application:

# Use an official Node.js runtime as a parent image
FROM node:18-alpine

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install app dependencies
RUN npm install

# Bundle app source
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Define the command to run your app
CMD [ "node", "server.js" ]
Tip: Alpine Linux-based images are often smaller and can lead to more efficient image sizes.

Further Reading