Dockerfile creation Tutorial - Dockerfile Instructions Explained with example!

Dockerfile creation Tutorial - Dockerfile Instructions Explained with example!

How to Create a Dockerfile: A Hands-On Guide

Introduction to Dockerfiles

  • Docker images are constructed using instructions from a text file called a Dockerfile. This video aims to teach the proper syntax for creating a Dockerfile and explain various instructions through a hands-on demonstration.
  • The speaker references a previous tutorial on building a JavaScript application, emphasizing the necessity of having a correctly named Dockerfile for image creation.

Naming and Syntax of Dockerfiles

  • The official name for the file must be "Dockerfile" with an uppercase 'D'. Following this naming convention is recommended by the official documentation.
  • Comments in the Dockerfile can be added using the hash symbol (#), similar to Python or other programming languages.

Structure and Instructions in Dockerfiles

  • The format of a Dockerfile consists of an instruction followed by its argument. While not case-sensitive, it is advised to use capital letters for instructions for clarity.
  • Various instructions are available in Dockerfiles that dictate how to create the desired image, with "FROM" being one of the first and most crucial commands used.

Specifying Base Images

  • The "FROM" instruction defines which base image will be utilized when creating your own image. Users can find different images on platforms like Docker Hub.
  • For example, if an application requires Python, you can specify it as your base image by using FROM python. Specific versions can also be indicated (e.g., FROM python:3.11).

Exploring Base Images on Docker Hub

  • Each base image is created using its own set of instructions within another Dockerfile. Users can explore these details on their respective pages on Docker Hub.
  • In this demonstration, Alpine 3.18 is chosen as the base image with FROM alpine:3.18, showcasing how users can select lightweight images.

Building and Verifying Images

  • After defining the base image in your Dockerfile, you build your custom image using the command docker build -t my_image ..
  • Upon successful execution, this command creates an image consisting of layers based on provided instructions; currently only one layer exists due to one instruction.

Checking Operating System Inside Image

  • To verify what operating system is running inside your newly created containerized environment, you run cat /etc/os-release.
  • Executing this command within your container confirms that Alpine Linux 3.18 was successfully used as specified in the FROM instruction.

Dockerfile Instructions Overview

Base Image Specification

  • The FROM instruction allows you to specify the base image for your Docker image, which can be pulled from various container registries like Docker Hub, GCR, or ECR.
  • You can use multiple FROM instructions in a single Dockerfile to create either multiple images or a multistage Docker image. Each stage can be named using the AS keyword.

Running Commands in Docker

  • The RUN instruction executes commands during the image build process. For example, you can install packages on an Alpine image using this command.
  • There are two formats for the RUN command: shell form and exec form. Shell form does not require specifying the shell, while exec form does (e.g., /bin/sh for Linux).
  • To install curl on an Alpine image, you would use RUN apk add curl, similar to how you'd use apt install curl in Ubuntu.

Layer Management

  • After executing a command with RUN, it creates a new layer in the Docker image. You can view these layers by running docker history my_image.
  • The output shows all executed layers; each command contributes to building up the final image.

Setting Working Directory

  • The WORKDIR instruction sets the working directory for subsequent commands like ADD, COPY, CMD, and ENTRYPOINT.
  • If you set a working directory that doesn't exist (e.g., /downloads), it will automatically create that directory when building the image.

User Configuration

  • The USER instruction specifies which user should be used for subsequent commands within the Dockerfile. By default, this is set to root.

Creating and Managing Users in Docker Containers

Adding a User to a Container

  • The RUN command is utilized to execute commands on top of the image, allowing for user creation within the container.
  • A new user named "cloud champ" is created, and this user will be set as the default for subsequent instructions using the USER instruction.
  • In Windows, users can be created with the net user add command, demonstrating cross-platform compatibility in Docker.

Building a Windows Image

  • The base image is set to PowerShell from Microsoft, which can be found on Docker Hub.
  • Two RUN commands are included: one creates a folder named /demo, while another writes "Hello World" into a text file inside that folder.
  • An error occurs during the build process due to an attempt to use Linux commands in a Windows environment.

Changing Shell Instructions

  • To resolve shell-related errors, the SHELL instruction can specify which shell (e.g., PowerShell or CMD) should be used for executing commands.
  • The default shell (/bin/sh) is replaced with PowerShell by adding a SHELL instruction before running subsequent commands.
  • After correcting the shell type to "pwsh," no further errors occur during the build process.

Verifying Command Execution

  • Upon successful execution of previous commands, checking the contents of "demo/message.txt" confirms that "Hello World" was written correctly.

Setting Environment Variables

  • Environment variables are crucial for application configuration and can be set using the ENV instruction in Dockerfiles.
  • Multiple environment variables can be defined either individually or combined into single instructions using backslashes for line continuation.

Testing Environment Variables

  • After building an image with three environment variables (app host, app port, ABC), running it allows verification of these variables through an ENV command.

How to Use Docker Instructions for File Management

Copying Files into Docker Containers

  • The process of setting environment variables in Docker containers is discussed, emphasizing that a Docker container includes code, dependencies, and libraries.
  • To copy files from the local machine to a Docker container, the COPY instruction is used. The source is specified as the local file path and the destination as a directory within the container.
  • After building the image with Docker build, verification can be done by running the container and listing files in the target directory to confirm successful copying of app.py.

Using ADD Instruction for Remote Files

  • The ADD instruction serves a similar purpose to COPY, but it also allows copying files from remote locations (e.g., S3 buckets).
  • An example demonstrates using ADD to retrieve a file named token.txt from an S3 bucket into the container during image creation.
  • After building with ADD, checking the contents of /c/code confirms both app.py and token.txt are present.

Exposing Ports in Docker

  • The EXPOSE instruction is introduced, which defines network ports that will be open for inbound traffic. For instance, exposing port 5000 allows external access to services running on that port.
  • Building an image with an exposed port can be verified using the command docker inspect, which shows that port 5000 is indeed open.

Running Applications with ENTRYPOINT and CMD

  • The importance of defining either an ENTRYPOINT or a CMD in a Dockerfile is highlighted; these instructions dictate what processes run inside the container.
  • A practical example illustrates how both commands can be utilized: while one specifies executable parameters directly, another can define them separately through CMD.

Testing Container Functionality

  • A demonstration runs through creating an image based on defined instructions and executing it on specific ports (e.g., localhost:880).

Understanding Docker CMD and ENTRYPOINT

Overview of CMD and ENTRYPOINT

  • The speaker explains how to override the default sleep duration in a Docker container. By specifying a different value (e.g., 10 seconds instead of 5), users can control the execution time when running the container.
  • To clarify the differences between CMD and ENTRYPOINT, viewers are encouraged to consult official documentation for deeper insights. Additional resources include Stack Overflow answers and YouTube videos for further understanding.

Importance of Labels in Docker Images

  • Labels provide metadata about a Docker image, such as versioning or creator information. This helps maintain clarity regarding who created the image and its purpose.
Video description

How to write dockerfile | Dockerfile explained | Dockerfile tutorial for beginners | What is dockerfile and how to build it This dockerfile tutorial explains all dockerfile instructions with hands on demo and example. To create a docker image you need to create dockerfile and in this tutorial you will learn how to create a dockerfile for your application #docker #dockerfile #devops Dockerfile instructions page: https://docs.docker.com/reference/dockerfile/ This tutorial on Dockerfile will help you learn how to create a docker file using Docker container and Docker Image. Here, we will see what is a Docker Container, the benefits of Docker, the syntax of the docker file, how to build docker images using the docker file, and a simple way to create DockerFile. Now, with further ado, let us get started with the Docker file tutorial video. Timestamps What is dockerfile 00:00 How to write dockerfile 00:47 Dockerfile format 01:19 Dockerfile FROM instruction 02:09 Dockerfile RUN instruction 06:20 Dockerfile WORKDIR instruction 08:35 Dockerfile USER instruction 10:36 Dockerfile SHELL instruction 12:27 Dockerfile ENV instruction 15:40 Dockerfile COPY instruction 18:00 Dockerfile ADD instruction 19:12 Dockerfile EXPOSE instruction 20:30 Dockerfile ENTRYPOINT and CMD instruction 21:35 Dockerfile LABEL instruction 24:52 Connect with me on LinkedIn: https://www.linkedin.com/in/nasiullha-chaudhari/ 🐳 DOCKER - Container concept - Why docker? (image vs. traditional DevOps) - Install docker on different operating systems - 8 basic commands you need to know - Docker vs. Virtual Machine - Docker in Practice: Overview of whole development process with Docker (development, continuous delivery, deployment) - Develop an application with Docker - Docker Compose - Dockerfile - Private Repository - Deploying your containerized application - Docker Volumes from theory to practice Interested to learn more, check out: Docker tutorial for beginners: https://youtu.be/q5S14cfOWfE Docker init & Docker scout Explained: https://youtu.be/rqEcheJgquA Complete Terraform Course: https://youtu.be/XgwV2HnBSws Gitlab CICD Course: https://youtu.be/JWXVijJfnHc What is Kubernetes and How it works? https://youtu.be/al9R-yfP4UA How I became CKA certified: https://youtu.be/dHXgg9fbP8E How I would learn DevOps from scratch https://youtu.be/EAXdnPWUCcc DevSecOps Project to deploy Netflix : https://youtu.be/g8X5AoqCJHc DevOps Interview Questions and Answers: https://youtu.be/GX6fOvaS0Xs After watching this dockerfile tutorial, I hope now you understood all the dockerfile instructions along with command and know how to write and create dockerfile for your application. Subscribe for more. Join this channel to get access to perks: https://www.youtube.com/channel/UCbg9O0JF3rVKev6wpI5_u5g/join