ITや趣味など気軽に投稿しています。

【Docker】 From Dockerfile Creation to Image Creation and Container Launch

Docker is a virtualization program that uses container technology.

What is Docker? What is a container?

We have previously explained what containers are. But how do you actually create and run one?

This article will show you how to start the container.

The relationship between Dockerfiles, images, and containers

First, create the container and organize the necessary files.

Dockerfile

A Dockerfile is a file used to create containers in Docker. It defines the operating system, software, and programs used by the container.

A Dockerfile is often regarded as a “design document” for a container.

Docker Image

A Docker image is generated from a Dockerfile, which can be built and executed by the Docker Engine.

Docker Container

A container is generated from a Docker image.

Containers are processes that execute, or launch, the image. Since containers are processes, multiple containers can be started from a single image.

Thus, stopping or deleting the process stops or deletes the container itself.

Organize the relationship between the Dockerfile, images, and containers.

The figure below illustrates the relationship between Dockerfiles, images, and containers.

Organize the relationship between the Dockerfile, images, and containers.

Docker practice

You will create a simple Dockerfile and launch a container. Docker must be available as a prerequisite. Ensure that the Docker command is accessible in a terminal or command prompt.

For instructions on installing Docker on Windows, please refer to the following article.

【Docker】WindowsでDocker Desktop環境を構築する

Prepare the necessary files.

First, create a Dockerfile. Create a file in any folder, paste the following contents into it, and save the file. Name the file “Dockerfile.”

In this example, we will install curl as an Ubuntu container and display “Hello, Ubuntu Docker!”

# latest Ubuntu image
FROM ubuntu:latest

# Install necessary packages(例: curl)
RUN apt-get update && apt-get install -y curl

# Message to be displayed at container startup
CMD ["echo", "Hello, Ubuntu Docker!"]

Docker image creation

Create a Docker image from the Dockerfile.

Then, at a command prompt or terminal, navigate to the directory where the Dockerfile was created and run the Docker build command.

cd <<Path to Dockerfile>>

docker build -t main .
docker build -t main .

The installation will proceed.

You can check the created Docker image with the following command:

docker images

>> 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
main         latest    62fb03104bed   4 minutes ago   219MB

Docker container creation

Then, run the following command to create a container from the Docker image.

In this command, not only will the container be created, it will also be executed.

The command outputs “Hello, Ubuntu Docker!”

docker run main

>> 
Hello, Ubuntu Docker!

You can check the created Docker container with the following command:

docker ps -a

>> 
CONTAINER ID   IMAGE     COMMAND                   CREATED         STATUS                    PORTS     NAMES
910054399d3e   main      "echo 'Hello, Ubuntu…"   2 seconds ago   Exited (0) 1 second ago             upbeat_elgamal

Starting and Stopping Docker Containers

To restart a container that has already been created, run the following command.

Containers can be specified by ID or name.

docker start <<container ID / container name>>

To stop a running container, run the following command:
The container in the example is terminated automatically after the command is run, so docker stop is not necessary.

This command is used when the container is acting as a continuous service (e.g., for servers).

docker stop <<container ID / container name>>

Closing

I have shown you how to create a Dockerfile and launch a container. While there are many distributed Docker images, you will need to create your own Dockerfile if you want to create custom containers.