Dockerfile Cheatsheet: Master Docker Image Creation with Key Commands

Docker Cheatsheet

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

 If you are a beginner or even a professional, “Dockerfile Cheatsheet” is a must-have. The concise summary of any important information is a cheatsheet which can be used as a quick guide or reference. Nowadays, Docker has become a quite fundamental tool for developers to package, ship, and run applications in isolated environments. A text file that has instructions for building a Docker image is called a Dockerfile. 

Docker provides a container to package and run applications in an isolated environment. So, it is necessary to understand Docker for DevOps and freshers. We are lucky to have a Dockerfile cheatsheet as it contains useful information and commands related to Docker.

In this article, we are going to explore the Dockerfile Cheatsheet. Additionally, we shall provide a Dockerfile cheatsheet PDF for your quick reference.

Let’s dive into the sea of Docker!

Quick Overview of Docker

Before going towards the Dockerfile cheatsheet, it is essential to know Docker first. Docker is an open-source platform that simplifies application deployment and scaling with the management of applications in terms of containerization. Containers are very light software units independently which bundle an application’s code together with its dependencies such that it runs consistently in any computing environment.

 Unlike virtual machines, which contain operating systems complete in their setup, the Docker containers share the OS host and hence are faster and more efficient. With Docker, developers can develop, ship, and run applications within isolated environments, which ensures that applications work uniformly across systems, whether in the development environment, testing environment, or production environment. Dedicated to DevOps and cloud-native applications, Docker makes continuous integration and continuous delivery so much easier.

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

Undestanding Dockerfile

A Dockerfile is a script having a series of instructions that tell Docker about building an image. This will indicate the base image, environment settings, libraries that need to be installed, the dependencies needed, and commands required to generate a fully functional image from which a container may be extracted. For each instruction in the new layer of an image, Docker acts.

For example, a very simple Dockerfile for an application in Python might look like this:

Python code

The above Dockerfile does the following:

  • Uses a slim version of Python 3.9 as the base image.
  • Sets the working directory to /app.
  • Copies project files from your local machine into the container.
  • Installs dependencies using the requirements.txt file.
  • Defines the default command to run the Python application.

Essential Dockerfile Commands

Dockerfile Cheatsheet

Now, we are going to discuss some of the most important Dockerfile instructions:

FROM

The ‘FROM” command is the first instruction in a Dockerfile. It will define the base image for subsequent instruction.

FROM COMMAND

This command tells Docker to pull the Ubuntu 20.04 images a the base to build the custom image. Here is the output:

OUTPUT

RUN

This command is used to execute the commands in the container when building the image.

RUN COMMAND

This command updates the package list and installs nginx. Here is the output:

OUTPUT

CMD

This instruction specifies the default command to run while the container is started.

CMD COMMAND

Here is the output when the container starts:

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

OUTPUT

COPY

This command copies files from your local filesystem into the Docker container.

COPY COMMAND

The above command will copy the contents of ‘’myapp’’ directory into the container’s /app directory. Here is the output

OUTPUT

WORKDIR

It sets the working directory for instructions.

WORKDIR COMMAND

Here is the output of the above command. It changes the current directory to app/

OUTPUT

ENV

It sets the environment variables in the container.

ENV COMMAND

This command sets the APP_ENV environment variable to “production”.

OUTPUT

EXPOSE

This command informs Docker that the container listens on a specific port.

EXPOSE COMMAND

This command exposes port 80, commonly used by Nginx and other web servers.

ENTRYPORT

It defines the default command that will always run. This also ensures that “nginx” will be the default command that runs whenever the container starts.

ENTRYPORT COMMAND

VOLUME

This command creates a mount point with a specified path and persist data between container restarts. 

VOLUME COMMAND

This sets up a volume at /app/data.

Best Practices for Writing Dockerfile

  • Use the official images as base images whenever possible.
  • Minimize layer count using && and multi-line syntax for commands
  • Do not write credentials or sensitive data inside the Dockerfile.
  • Use the .dockerignore to exclude unwanted files from the image.
  • Use layers of cache in optimal ways to enhance the speed of builds.
  • Keep the images small for speedy deployments.

Role of CyberPanel in Docker Management

CyberPanel

CyberPanel, a web hosting control panel, manages the Docker by making management much easier. With CyberPanel, you’ll be able to manage Docker containers and images from a graphical interface without running complex commands. Here’s how CyberPanel can help you with all that:

CyberPanel-Services

Container Management:

Start, stop, and monitor containers from a simple dashboard.

One-Click Installation: 

Deploy Docker applications in one click, saving time and effort.

Image Management: 

Easily pull, tag, and push Docker images.

Security:

 Docker container security and networking configurations can be managed with no issues at all.

Why CyberPanel?

For those who want a control panel for web hosting, user-friendly, with Docker management within it, then CyberPanel would be a great solution. It simplifies Docker orchestration for web applications and databases and uses custom environments.

CyberPanel Docker Manager

FAQs : Dockerfile CheatSheet

What is a Dockerfile?

Docker reads a text file with instructions to build an image. A file defines the base image, dependencies, and commands that are needed to prepare a container.

How do I run a Dockerfile?

Build an image from a Dockerfile:
docker build -t <image-name>.
docker run <image-name>.

What is the difference between CMD and ENTRYPOINT?

CMD is used to assign default commands that are run at container launch time. ENTRYPOINT lets you set a container to run as an executable.

What does EXPOSE do?

EXPOSE declares which ports a container exposes at runtime and which can, later on, be mapped to host machine ports by Docker -EXPOSE does not itself map a port to be accessed from the host machine. It merely tells Docker which ports are exposed, and later on, when the container is run, it can then be published to the host by a mapping.

How can CyberPanel help manage Docker?

CyberPanel has an easy interface for managing containers, images, and security settings, hence the operations on Docker are very easy.

Can I copy files from my local machine to a container?

You can use either the COPY or ADD command in the Dockerfile to copy files from your local system into the container.

How can I minimize the size of my Docker image?

They will be using fewer base images, will clean all their cache files after every installation of packages, and combine commands to reduce the number of layers.

Final Remarks: Dockerfile Cheatsheet Is a Blessing For DevOps

In a nutshell, Dockerfile is the most useful tool in the toolkit for any user working with Docker since it depicts how you will construct and configure your images. Understanding the core Dockerfile commands such as FROM, RUN, CMD, and COPY can make you construct powerful yet efficient containers. We also discussed how CyberPanel may be used to manage the Docker environment, therefore helping users handle Docker more easily without resorting to complicated CLI commands.

Download the Dockerfile Cheatsheet PDF so you can have ready access to quickly and easily use the most commonly used Dockerfile instructions.

From all of this insight and guidance, you are now closer to becoming a Docker file master with a streamlined workflow and far more than the powerful ability that comes from using Docker.

Use Dockerfile Cheatsheet and for your ease, CyberPanel is here!

Hasib Iftikhar
I'm Hasib Iftikhar, a dedicated technical writer at CyberPanel, joining the team in July 2024. With three years of extensive experience in content writing, I specialize in copywriting, article writing, guest posting, affiliate content writing, and SEO. My expertise ensures that each piece of content I create is engaging, informative, and optimized for search engines, helping businesses enhance their online presence and reach their target audience effectively.
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!