How to run a Docker container based on a custom image?

DockerDockerBeginner
Practice Now

Introduction

Docker is a popular containerization platform that allows developers to package and deploy applications in a consistent and isolated environment. In this tutorial, you will learn how to build a custom Docker image and run a Docker container based on that image. By the end of this guide, you will have a solid understanding of the process to create, manage, and run Docker containers for your applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/ImageOperationsGroup(["`Image Operations`"]) docker(("`Docker`")) -.-> docker/DockerfileGroup(["`Dockerfile`"]) docker/ContainerOperationsGroup -.-> docker/create("`Create Container`") docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/start("`Start Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/ImageOperationsGroup -.-> docker/pull("`Pull Image from Repository`") docker/DockerfileGroup -.-> docker/build("`Build Image from Dockerfile`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/create -.-> lab-411592{{"`How to run a Docker container based on a custom image?`"}} docker/ps -.-> lab-411592{{"`How to run a Docker container based on a custom image?`"}} docker/run -.-> lab-411592{{"`How to run a Docker container based on a custom image?`"}} docker/start -.-> lab-411592{{"`How to run a Docker container based on a custom image?`"}} docker/stop -.-> lab-411592{{"`How to run a Docker container based on a custom image?`"}} docker/pull -.-> lab-411592{{"`How to run a Docker container based on a custom image?`"}} docker/build -.-> lab-411592{{"`How to run a Docker container based on a custom image?`"}} docker/ls -.-> lab-411592{{"`How to run a Docker container based on a custom image?`"}} end

Understanding Docker Containers

Docker is a popular open-source platform that enables developers to build, deploy, and run applications in a containerized environment. Containers are lightweight, standalone, and executable software packages that include everything needed to run an application, including the code, runtime, system tools, and libraries.

What is a Docker Container?

A Docker container is a standardized unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. Containers are created from Docker images, which are the blueprints for creating containers.

graph LR A[Docker Image] --> B[Docker Container] B[Docker Container] --> C[Running Application]

Benefits of Docker Containers

Docker containers offer several benefits over traditional virtualization approaches:

Benefit Description
Portability Containers can run consistently on any machine, regardless of the underlying infrastructure.
Efficiency Containers share the host operating system, making them more lightweight and efficient than virtual machines.
Consistency Containers ensure that applications will always run the same, regardless of the environment.
Scalability Containers can be easily scaled up or down to meet changing demands.

Docker Container Lifecycle

The lifecycle of a Docker container includes the following stages:

  1. Build: Creating a Docker image from a Dockerfile.
  2. Ship: Storing the Docker image in a registry, such as Docker Hub.
  3. Run: Launching a new container from a Docker image.
  4. Manage: Monitoring, scaling, and maintaining running containers.

By understanding the basics of Docker containers, you can now move on to building a custom Docker image.

Building a Custom Docker Image

To build a custom Docker image, you need to create a Dockerfile, which is a text file that contains instructions for building the image.

Creating a Dockerfile

Here's an example Dockerfile that builds a custom image based on the Ubuntu 22.04 base image and installs the Apache web server:

## Use the Ubuntu 22.04 base image
FROM ubuntu:22.04

## Update the package index and install Apache
RUN apt-get update && apt-get install -y apache2

## Set the default command to run when the container starts
CMD ["apache2ctl", "-D", "FOREGROUND"]

Building the Docker Image

To build the Docker image from the Dockerfile, run the following command in the same directory as the Dockerfile:

docker build -t my-apache-image .

This command will create a new Docker image named my-apache-image based on the instructions in the Dockerfile.

Inspecting the Docker Image

After building the image, you can list all the images on your system using the following command:

docker images

This will show you the list of Docker images, including the my-apache-image you just created.

You can also inspect the details of the image using the following command:

docker inspect my-apache-image

This will display the metadata and configuration of the Docker image.

Now that you have a custom Docker image, you can proceed to running a container based on this image.

Running a Docker Container from a Custom Image

Now that you have a custom Docker image, you can run a container based on that image.

Starting a Docker Container

To start a new container from the my-apache-image image, use the following command:

docker run -d -p 80:80 --name my-apache-container my-apache-image

Here's what each part of the command does:

  • docker run: Starts a new Docker container.
  • -d: Runs the container in detached mode, which means it runs in the background.
  • -p 80:80: Maps port 80 on the host to port 80 in the container, allowing you to access the web server from the host.
  • --name my-apache-container: Assigns the name "my-apache-container" to the running container.
  • my-apache-image: The name of the Docker image to use for the container.

Verifying the Running Container

After starting the container, you can verify that it's running using the following commands:

## List all running containers
docker ps

## Inspect the container
docker inspect my-apache-container

The docker ps command will show you the list of running containers, including the my-apache-container you just started.

The docker inspect command will provide detailed information about the running container, including its IP address, port mappings, and other configuration details.

Accessing the Web Server

Once the container is running, you can access the Apache web server by opening a web browser and navigating to http://localhost. You should see the default Apache welcome page.

By understanding how to run a Docker container based on a custom image, you can now deploy your own applications in a containerized environment.

Summary

In this tutorial, you have learned how to build a custom Docker image and run a Docker container based on that image. You have explored the steps to create a Docker image, including writing a Dockerfile and building the image. Additionally, you have learned how to run a Docker container using the custom image, manage the container's lifecycle, and access the running container. With these skills, you can now confidently package and deploy your applications using Docker containers.

Other Docker Tutorials you may like