DevOps / Containerization and Docker

Building and Running Docker Images

In this tutorial, you'll delve into the process of building and managing Docker images. We'll cover everything from creating a Dockerfile to running your image as a container.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Focuses on containerizing applications to ensure consistency across development and production environments.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Favicon Generator

Create favicons from images.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help