How to Use Dockerfiles

Updated by Linode Contributed by Jack Wallen

Contribute on GitHub

Report an Issue | View File | Edit File

How to Use Dockerfiles

Docker images make it easy to deploy multiple containers without having to maintain the same across multiple virtual machines.

You can use a Dockerfile to automate installation and configuration of an image and its dependencies.

Dockerfile Basics

A dockerfile is a text file that contains the necessary commands to assemble an image. Once a Dockerfile is created, the administrator uses the docker build command to create an image based on the commands within the file. The commands and information within the dockerfile can be configured to use specific software versions and dependencies to ensure consistent and stable deployments.

Note

Do not store the Dockerfile in your root directory.

Create a separate directory for the Dockerfile and place all necessary files within the same directory as the Dockerfile.

A Dockerfile uses the following commands for building the images:

  • ADD: Copy files from a source on the host to the container’s own filesystem at the set destination.
  • CMD: Execute a specific command within the container.
  • ENTRYPOINT: Set a default application to be used every time a container is created with the image.
  • ENV: Set environment variables.
  • EXPOSE: Expose a specific port to enable networking between the container and the outside world.
  • FROM: Define the base image used to start the build process.
  • MAINTAINER: Define the full name and email address of the image creator.
  • RUN: Central executing directive for Dockerfiles.
  • USER: Set the UID (the username) that will run the container.
  • VOLUME: Enable access from the container to a directory on the host machine.
  • WORKDIR: Set the path where the command, defined with CMD, is to be executed.

Not every command must be used. You will create a working Dockerfile example in the following section.

Create a Dockerfile

Dockerfiles require specific setup and format with no extra spaces.

Docker reuses cache from the previous step. This may result in commands not running properly or not running at all. To avoid this caching issue, combine apt commands into a single RUN statement. To avoid other caching issues, combine multiple RUN statements for commands like apt-get update/upgrade/install.

Note that in the example below, multiple packages are installed on separate lines. This optional step is recommended by the Dockerfile Best Practices in order to ease future updates and maintenance.

  1. Create and change to a new directory and create a Dockerfile file:

    mkdir ~/mydockerbuild && cd ~/mydockerbuild
    touch Dockerfile
    
  2. Open Dockerfile using a text editor and enter the following example to create a Dockerfile that installs build-essential, curl, and make onto a Ubuntu image:

    Dockerfile
    1
    2
    3
    4
    5
    6
    7
    
    FROM ubuntu
    MAINTAINER NAME EMAIL
    RUN apt-get update && apt-get install -y \
        build-essential \
        gcc \
        curl \
        make
    In this example:
    
    *  **NAME**: Full name of the creator.
    *  **EMAIL**: Email address of the creator.
    
  3. Save and close Dockerfile.

Build a Docker Image from the Dockerfile

Build the Dockerfile using the docker build command within the same directory. Substitute NAME in the following example with the name of the image to be created:

docker build --tag=“Build-Essential:Dockerfile” /path/to/file .

To build three images using the same Dockerfile, give each image a new name. In this example, webdev1, webdev2, webdev3:

docker build -t “webdev1:Dockerfile” .
docker build -t “webdev2:Dockerfile” .
docker build -t “webdev3:Dockerfile” .

Each image created will be tagged Dockerfile. To change the tag during build, change Dockerfile:

docker build -t “debian-webdev3:dev” .

Just the basics

This guide covered the basics of using Dockerfiles to build images. For more information on Dockerfiles, visit the official Dockerfile Best Practices documentation.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Join our Community

Find answers, ask questions, and help others.

This guide is published under a CC BY-ND 4.0 license.