Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Dockerfile: Instructions, Best Practices, and Gotchas
31%

The Dockerfile

The Dockerfile is a text file that contains various instructions and arguments. These commands and arguments describe how your base image will look at the end of the build process.

While it's possible to run an image directly without building it, such as a public or private image pulled from Docker Hub or another registry, creating a Dockerfile for your application is a good starting point if you want to customize and build your own containers. It also allows you to organize your deployments and distribute the same image to different environments and teams.

Also, even if you load an existing image on your disk or from a remote registry, or even if you lose the directory where Docker stores its images, you can always recreate the same image by using the Dockerfile that describes how to build it. This is why it's important to version control your Dockerfiles along with your application code.

Creating a Dockerfile follows a simple, explicit syntax.

For example, if you need to execute commands beyond what the Docker instructions permit, you can use the RUN command or CMD and ENTRYPOINT to run scripts, executables, binaries, or any other command.

We will examine the different instructions and their uses in the following sections. By using these instructions, you should be able to customize and create your own Docker images.

This is a basic example of a container that launches an Apache web server with a simple "Hello, World!" message.

Start by creating a folder named apache2 and navigate into it:

mkdir -p $HOME/apache2 && cd apache2

Create a file named Dockerfile with the following content:

cat < Dockerfile
FROM ubuntu:latest
MAINTAINER Aymen EL Amri - @eon01
RUN apt-get update && apt-get install -y apache2 && apt-get clean
RUN echo "Hello, World!"

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More