Building Docker Images with Dockerfile

Tutorial 2 of 5

Building Docker Images with Dockerfile

1. Introduction

This tutorial aims to guide you through the process of building Docker images using Dockerfiles. We will explore the docker build command and its various options. By the end of this tutorial, you will have a solid understanding of how to create Docker images that can be used to deploy your application in containers.

Goals:
- Understand Dockerfile and its structure
- Learn how to use the docker build command
- Build Docker images from Dockerfiles

Prerequisites:
- Basic knowledge of Docker
- Docker installed on your machine

2. Step-by-Step Guide

A Dockerfile is a text file that Docker reads to build an image. It consists of various instructions, each creating a new layer in the image. Let's break down the main components:

  • FROM: This instruction sets the base image for subsequent instructions.
  • RUN: This instruction will execute any commands in a new layer on top of the current image.
  • CMD: This instruction provides defaults for an executing container.
  • COPY: This instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

Building a Docker Image

To build a Docker image, use the docker build command followed by . if your Dockerfile is in the current directory.

docker build .

This command tells Docker to build an image using the Dockerfile in the current directory.

You can also tag your image using the -t option:

docker build -t my-app:1.0 .

This command creates an image named "my-app" with the tag "1.0".

Best Practices

  • Always use the most specific base image.
  • Utilize the build cache by adding files or running commands that change frequently at the end of your Dockerfile.
  • Keep your Dockerfiles readable and maintainable by using comments and line breaks.

3. Code Examples

Let's create a Dockerfile for a Node.js app:

# Use the official Node.js runtime as the base image
FROM node:14

# Set the working directory in the Docker image
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the Docker image
COPY package*.json ./

# Install app dependencies in the Docker image
RUN npm install

# Copy the app source code to the Docker image
COPY . .

# Expose port 8080 to have it mapped by Docker daemon
EXPOSE 8080

# Define the command to run the app
CMD [ "node", "app.js" ]

Running docker build -t my-node-app . in the same directory as the Dockerfile will create an image named "my-node-app".

4. Summary

In this tutorial, we've learned how to build Docker images using Dockerfiles. We've looked at the structure of a Dockerfile, the docker build command, and some best practices when creating Dockerfiles.

Next Steps:
- Learn about Docker Compose to manage multi-container applications
- Learn how to push your Docker images to Docker Hub

Additional Resources:
- Docker Documentation
- Best practices for writing Dockerfiles

5. Practice Exercises

Exercise 1: Create a Dockerfile for a Python Flask application.

Exercise 2: Build the Docker image using the Dockerfile you created.

Exercise 3: Run a container from the Docker image and access the Flask application.

Solutions:

  • Exercise 1:
FROM python:3.7
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
CMD [ "flask", "run" ]
  • Exercise 2:
docker build -t my-flask-app .
  • Exercise 3:
docker run -p 5000:5000 my-flask-app

You can then access your Flask app by visiting localhost:5000 in your web browser.

Remember to practice regularly and build Dockerfiles for your projects to get the hang of it.