Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Creating Your Own Image | Working with Docker Images
Docker Essentials

bookCreating Your Own Image

Introduction to Dockerfile Syntax and Structure

When you want to create your own Docker image, you use a special text file called a Dockerfile. This file contains step-by-step instructions that tell Docker how to build your image. Each instruction in a Dockerfile starts with a keyword, written in uppercase, and is followed by its arguments. The most common instructions you will use include:

  • FROM: Sets the base image for your new image;
  • RUN: Executes commands inside the image during the build process;
  • COPY: Copies files or directories from your local machine into the image;
  • ADD: Similar to COPY, but also supports remote URLs and file extraction;
  • CMD: Sets the default command that runs when a container starts;
  • EXPOSE: Documents which ports the container will listen on at runtime;
  • WORKDIR: Sets the working directory for following instructions.

Docker reads the Dockerfile from top to bottom, executing each instruction in order. This simple, readable structure makes it easy to automate the setup of your application environment.

Dockerfile

Dockerfile

copy
Note
Note

Download the app.py file and place it in the same directory as your Dockerfile so that Docker can locate it during the build process.

Building an Image from a Dockerfile

After writing your Dockerfile, you can build your own image using the docker build command. Follow these steps to create your custom image:

  1. Place your Dockerfile and all required application files (such as app.py and requirements.txt) in the same directory;

  2. Open a terminal window and navigate to the directory containing your Dockerfile;

  3. Build your Docker image by running the following command, replacing my-python-app with your chosen image name:

    docker build -t my-python-app .
    

    The -t flag assigns a name (or tag) to your image. The . specifies the current directory as the build context.

As Docker executes each instruction in your Dockerfile, you will see output in the terminal showing the build progress. When the process finishes, your new image appears in the list returned by docker images. You can now use this image to start containers that run your application.

question mark

What is the purpose of a Dockerfile?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what each Dockerfile instruction does in more detail?

What is the difference between COPY and ADD in a Dockerfile?

How do I write a basic Dockerfile for a Python Flask app?

Awesome!

Completion rate improved to 7.14

bookCreating Your Own Image

Swipe to show menu

Introduction to Dockerfile Syntax and Structure

When you want to create your own Docker image, you use a special text file called a Dockerfile. This file contains step-by-step instructions that tell Docker how to build your image. Each instruction in a Dockerfile starts with a keyword, written in uppercase, and is followed by its arguments. The most common instructions you will use include:

  • FROM: Sets the base image for your new image;
  • RUN: Executes commands inside the image during the build process;
  • COPY: Copies files or directories from your local machine into the image;
  • ADD: Similar to COPY, but also supports remote URLs and file extraction;
  • CMD: Sets the default command that runs when a container starts;
  • EXPOSE: Documents which ports the container will listen on at runtime;
  • WORKDIR: Sets the working directory for following instructions.

Docker reads the Dockerfile from top to bottom, executing each instruction in order. This simple, readable structure makes it easy to automate the setup of your application environment.

Dockerfile

Dockerfile

copy
Note
Note

Download the app.py file and place it in the same directory as your Dockerfile so that Docker can locate it during the build process.

Building an Image from a Dockerfile

After writing your Dockerfile, you can build your own image using the docker build command. Follow these steps to create your custom image:

  1. Place your Dockerfile and all required application files (such as app.py and requirements.txt) in the same directory;

  2. Open a terminal window and navigate to the directory containing your Dockerfile;

  3. Build your Docker image by running the following command, replacing my-python-app with your chosen image name:

    docker build -t my-python-app .
    

    The -t flag assigns a name (or tag) to your image. The . specifies the current directory as the build context.

As Docker executes each instruction in your Dockerfile, you will see output in the terminal showing the build progress. When the process finishes, your new image appears in the list returned by docker images. You can now use this image to start containers that run your application.

question mark

What is the purpose of a Dockerfile?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt