Building and Running Docker Images

Tutorial 2 of 5

1. Introduction

In this tutorial, we'll delve into the world of Docker, a popular platform used to simplify the process of deploying applications. Specifically, we'll focus on building and running Docker images.

By the end of this tutorial, you'll be able to:

  • Understand what Docker images are
  • Create a Dockerfile
  • Build a Docker image from a Dockerfile
  • Run a Docker image as a container

Prerequisites

To get the most out of this tutorial, you should have:

  • Basic knowledge of command line interfaces
  • Docker installed on your machine

2. Step-by-Step Guide

2.1 Docker Images and Dockerfile

A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software. A Dockerfile is a text file that contains a series of instructions used to create a Docker image.

2.2 Building Docker Images

To build a Docker image, navigate to the directory that contains your Dockerfile and run the docker build command:

docker build -t my-app .

Here, -t my-app assigns the name my-app to your image, and . specifies that Docker should look for the Dockerfile in the current directory.

2.3 Running Docker Images

Once you've built your image, run it as a container using the docker run command:

docker run -d -p 8080:8080 my-app

Here, -d runs the container in detached mode (in the background), and -p 8080:8080 maps port 8080 of the container to port 8080 on the host machine.

3. Code Examples

Example 1: Creating a Dockerfile

Here's a simple Dockerfile for a Node.js application:

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

# Set the working directory in the container to /app
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install the app dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Expose port 8080
EXPOSE 8080

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

After building this image with docker build -t my-node-app . and running it with docker run -d -p 8080:8080 my-node-app, your app will be accessible at http://localhost:8080.

Example 2: Using Dockerfile to Install Additional Software

# Use an official Python runtime as a base image
FROM python:3.8

# Set the working directory
WORKDIR /app

# Install the 'requests' package using pip
RUN pip install requests

# Copy a Python script into the container
COPY script.py .

# Run the script when the container launches
CMD [ "python", "./script.py" ]

4. Summary

In this tutorial, we've covered the basics of Docker images and Dockerfiles, including how to build and run Docker images. The next steps in your Docker journey could include learning how to use Docker Compose to manage multi-container applications, or how to use Docker with continuous integration/continuous deployment (CI/CD) systems.

5. Practice Exercises

Exercise 1: Create a Dockerfile for a simple Python script that prints "Hello, Docker!" and build and run the image.

Solution:

# Use an official Python runtime as a base image
FROM python:3.8

# Set the working directory
WORKDIR /app

# Copy a Python script into the container
COPY script.py .

# Run the script when the container launches
CMD [ "python", "./script.py" ]

Where script.py contains:

print("Hello, Docker!")

Then build and run the image:

docker build -t my-python-app .
docker run -it --rm my-python-app

Exercise 2: Modify the Dockerfile from Exercise 1 to install the 'requests' Python package and use it in script.py.

The solution follows a similar pattern to Example 2 in the "Code Examples" section.

Remember, practice is key when learning new concepts, so don't be afraid to experiment with different Dockerfiles and applications. Happy coding!