1. Getting started with Docker - making a hello docker image

introduce

     Docker is an open-source containerization platform that enables developers to package applications into lightweight, portable containers to run anywhere, whether on a local development machine or in a public cloud, private cloud, or physical on the server. Using Docker, developers can create, run, and manage applications and their dependencies, while also ensuring the consistency and repeatability of applications across different environments.

Docker containers are a lightweight virtualization technology that isolates applications and their dependencies at the operating system level. Unlike traditional virtual machines, Docker containers do not need to start the entire operating system, but only the container itself and some components it depends on, so they are very fast, efficient, portable and scalable.

Docker consists of two main components: Docker Engine and Docker Images. The Docker engine is a lightweight runtime environment that can run on different platforms, including Linux, Windows, and MacOS, etc. A Docker image is an executable package that contains all the components and dependencies needed to run an application, including code, libraries, runtime environment, and configuration files. Docker images can be found on Docker Hub, a public registry that centrally stores Docker images.

Using Docker, developers can package an application and all the components it depends on into a Docker image, and then deploy the image to run anywhere without worrying about environment configuration and dependency conflicts. In addition, developers can use Dockerfile files to define image building rules and configurations to automate the build and deployment process.

Make a mirror image

install docker

There are many ways to install Docker in CentOS, the following is one of the commonly used methods:

1. Update the system package manager

First, update the system package manager by running the following command:

sudo yum update

2. Install the Docker dependency package
Run the following command to install the dependency package required by Docker:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

3. Install Docker CE
Run the following command to install Docker CE (Community Edition):

sudo yum install docker-ce docker-ce-cli containerd.io

4. Start the Docker service

Run the following command to start the Docker service:

sudo systemctl start docker

5. Verify Docker installation

Run the following command to verify that the Docker installation was successful:

sudo docker run hello-world

If all is well, you should be able to see output similar to the following:

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

Make a docker image

1. Write a simple Python application: You can use any text editor, such as Notepad, Sublime Text, VS Code, etc., to write a simple Python application, for example:

# hello.py
print("Hello, Docker!")

2. Create a Dockerfile: In the same directory as the application, create a file named Dockerfile, and define the build rules and configuration of the Docker image in it. For example, here is a simple Dockerfile:

FROM python:3.8-slim-buster

WORKDIR /app

COPY hello.py .

CMD [ "python", "./hello.py" ]

Specifically, the Dockerfile uses Python 3.8 as the base image, sets /app as the working directory, copies the hello.py file in the current directory to the /app directory, and defines the entry point of the application as the hello.py file.
3. Build the Docker image: In the application directory, open a terminal or command line interface and enter the following command to build the Docker image:

docker build -t myimage .

Among them, the -t parameter is used to name the image, and "." indicates the current directory where the Dockerfile is located.

4. Run the Docker container: Use the following command to run the Docker container:

docker run myimage

After running, you should see "Hello, Docker!" as output.
5. Export image

Run the following command to export the myimage image as a tar file:

docker save -o myimage.tar myimage

This command will generate a file named myimage.tar in the current directory, which contains all the layers and metadata of the myimage image.

6. Copy the image

Copy the myimage.tar file to other machines or other environments.

7. Import image

Run the following command to import the image in the myimage.tar file into the Docker daemon:

docker load -i myimage.tar

This command will import the myimage image in the Docker daemon's registry and make it available for starting containers.

It should be noted that the exported and imported images only include the layers and metadata of the image, not the running status and data of the container. If you need to save the running state and data of the container, you can consider using Docker data volumes or other persistence solutions.

Guess you like

Origin blog.csdn.net/haoyuxuanyuan/article/details/130157383