How to create and run a 'Hello World' Docker container?

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful containerization platform that has revolutionized the way developers build, deploy, and manage applications. In this tutorial, we will guide you through the process of creating and running a simple 'Hello World' Docker container, which is a great starting point for learning Docker.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/ImageOperationsGroup(["`Image Operations`"]) docker(("`Docker`")) -.-> docker/SystemManagementGroup(["`System Management`"]) 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/ImageOperationsGroup -.-> docker/images("`List Images`") docker/SystemManagementGroup -.-> docker/version("`Show Docker Version`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/create -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} docker/ps -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} docker/run -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} docker/start -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} docker/stop -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} docker/pull -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} docker/images -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} docker/version -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} docker/ls -.-> lab-411528{{"`How to create and run a 'Hello World' Docker container?`"}} end

Introduction to Docker

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, such as the code, runtime, system tools, and libraries.

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Docker Architecture

The Docker architecture consists of the following key components:

graph TD A[Docker Client] --> B[Docker Daemon] B --> C[Docker Images] B --> D[Docker Containers] B --> E[Docker Registries]
  • Docker Client: The user interface for interacting with the Docker system.
  • Docker Daemon: The background process that manages Docker objects, such as images, containers, networks, and volumes.
  • Docker Images: Blueprints for creating Docker containers.
  • Docker Containers: Runnable instances of Docker images.
  • Docker Registries: Repositories for storing and distributing Docker images.

Benefits of Using Docker

  • Consistency: Docker ensures that applications run the same way, regardless of the underlying infrastructure.
  • Scalability: Docker containers can be easily scaled up or down based on the application's resource requirements.
  • Portability: Docker containers can run on any machine that has Docker installed, regardless of the underlying operating system.
  • Efficiency: Docker containers are lightweight and use fewer resources compared to traditional virtual machines.

Getting Started with Docker

To get started with Docker, you'll need to install the Docker engine on your system. You can download and install Docker from the official Docker website (https://www.docker.com/get-started). Once installed, you can start using Docker to build and run your applications.

Building a 'Hello World' Docker Container

In this section, we will learn how to build a simple "Hello World" Docker container.

Creating a Dockerfile

A Dockerfile is a text file that contains the instructions for building a Docker image. Let's create a simple Dockerfile:

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

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

## Copy the "hello.sh" script to the container
COPY hello.sh .

## Make the script executable
RUN chmod +x hello.sh

## Define the command to run the script when the container starts
CMD ["./hello.sh"]

In this Dockerfile, we:

  1. Use the official Ubuntu 22.04 image as the base image.
  2. Set the working directory to /app.
  3. Copy the "hello.sh" script to the container.
  4. Make the script executable.
  5. Define the command to run the script when the container starts.

Building the Docker Image

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

docker build -t hello-world .

This command will build the Docker image with the tag "hello-world".

Verifying the Image

You can list all the Docker images on your system by running:

docker images

You should see the "hello-world" image in the list.

Running and Exploring the 'Hello World' Container

Now that we have built the "hello-world" Docker image, let's run and explore the container.

Running the Container

To run the "hello-world" container, use the following command:

docker run hello-world

This command will start the container and execute the command defined in the Dockerfile (i.e., running the "hello.sh" script).

You should see the output:

Hello, LabEx!

Exploring the Container

You can explore the running container using various Docker commands:

  1. List running containers:

    docker ps

    This will show the running containers on your system.

  2. Inspect the container:

    docker inspect hello-world

    This will provide detailed information about the container, such as its configuration, network settings, and more.

  3. View the container's logs:

    docker logs hello-world

    This will display the output of the "hello.sh" script that was executed in the container.

  4. Execute a command in the running container:

    docker exec -it hello-world /bin/bash

    This will start a new interactive session inside the running container, allowing you to explore the container's file system and run additional commands.

Stopping and Removing the Container

To stop the running container, use the following command:

docker stop hello-world

To remove the container, use the following command:

docker rm hello-world

This will stop and remove the "hello-world" container from your system.

Summary

In this tutorial, you have learned how to create and run a 'Hello World' Docker container. By understanding the basic concepts of Docker containers, you have taken the first step towards mastering this powerful technology. Docker allows you to package your applications and their dependencies into portable, self-contained units, making it easier to develop, deploy, and manage your software across different environments. With the knowledge gained from this tutorial, you can now explore more advanced Docker concepts and start building your own containerized applications.

Other Docker Tutorials you may like