Docker usage guide: installation, basic concepts and operation steps

Docker is an open source containerization platform for building, packaging, and running applications. It provides a lightweight, portable and scalable way to package an application and all its dependencies into a self-contained container. The following is a usage guide for Docker novices to help you understand the basic concepts and operation steps of Docker.

Install Docker

Before you begin, you first need to install Docker on your operating system. Docker supports multiple operating systems, including Linux, macOS, and Windows. The following are Docker installation guides for different operating systems:

Debian installation

You can install Docker on Debian systems by following the steps provided in the official Docker documentation. Documentation link: https://docs.docker.com/engine/install/debian/

CentOS installation

If you are using a CentOS system, you can install it according to the steps provided in the official Docker documentation. Documentation link: https://docs.docker.com/engine/install/centos/

After the installation is complete, you can verify whether Docker was installed successfully by running the following command:

docker version

If everything is fine, you should be able to see version information about the Docker client and server.

debian 11.3 Docker version

Docker basic concepts

Before starting to use Docker, there are several basic concepts that need to be understood:

  • Image : An image is a read-only template used to create Docker containers. It contains all the files, libraries and dependencies required to run the application. You can build your own image or obtain a ready-made image from Docker Hub or other image repositories.

  • Container : A container is a running instance created by an image. It can be started, stopped, deleted and moved. Containers are self-contained, isolable running environments in which applications can run in isolation from the host system.

  • Dockerfile : A Dockerfile is a text file that contains a series of instructions and configurations for building a container image. By writing a Dockerfile, you can define the application's running environment, dependencies, and other configurations.

  • Repository : The repository is a place used to store and distribute Docker images. Docker Hub is the most popular public repository where you can get various types of images. You can also create your own private repository to manage your own images.

Make a mirror

Making an image is an important step in using Docker. You can define the image building process and configuration by writing a Dockerfile.

https://docs.docker.com/engine/reference/builder/

Directory Structure:

.
├── AppRun
├── default.desktop
├── default.png
├── doc
│   ├── libdbus-1-3
│   │   └── copyright
│   ├── libkeyutils1
│   │   └── copyright
│   ├── liblzma5
│   │   └── copyright
│   └── libpcre3
│       └── copyright
├── Dockerfile
├── lib
│   ├── libdbus-1.so.3
│   ├── libgcrypt.so.20
│   ├── libglib-2.0.so.0
│   ├── libgssapi_krb5.so.2
│   ├── libgthread-2.0.so.0
│   ├── libicudata.so.56
│   ├── libicui18n.so.56
│   ├── libicuuc.so.56
│   ├── libk5crypto.so.3
│   ├── libkeyutils.so.1
│   ├── libkrb5.so.3
│   ├── libkrb5support.so.0
│   ├── liblz4.so.1
│   ├── liblzma.so.5
│   ├── libpcre.so.3
│   ├── libQt5Core.so.5
│   ├── libQt5DBus.so.5
│   ├── libQt5Network.so.5
│   ├── libsystemd.so.0
│   └── libzstd.so.1
├── AppName
├── plugins
│   └── bearer
│       ├── libqconnmanbearer.so
│       ├── libqgenericbearer.so
│       └── libqnmbearer.so
├── qt.conf
├── .dockerignore
├── sources.list
└── translations
    └── qt_en.qm

Here is a simple Dockerfile example:

FROM debian:bullseye

COPY sources.list /etc/apt

ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /usr/local/bin/app

COPY . /usr/local/bin/app

RUN /bin/chmod +x /usr/local/bin/app/AppName

CMD ["./AppName"]

This is a simple Dockerfile used to build a container based on the Debian Bullseye image. Below is an explanation of each directive:

  1. FROM debian:bullseye: Specifies the base image as Debian Bullseye. It will serve as the initial environment required for the build.

  2. COPY sources.list /etc/apt: Copy files from the host sources.listto the /etc/aptdirectory in the container. sources.listIt is a file used to configure the software package source in the Debian system. It is copied to the container for subsequent installation of the software package.

  3. ENV DEBIAN_FRONTEND=noninteractiveDEBIAN_FRONTEND: Set the value of an environment variable to noninteractive. This environment variable setting can make subsequent software package installation processes without human-computer interaction and avoid interactive prompts.

  4. WORKDIR /usr/local/bin/app: Specifies the working directory of the container /usr/local/bin/app. Subsequent instructions will be executed in this directory.

  5. COPY . /usr/local/bin/app: Copies all files and folders in the current host directory to the container's directory /usr/local/bin/app. This will include everything in the directory where the Dockerfile is located.

  6. RUN /bin/chmod +x /usr/local/bin/app/AppName: Run the command in the container /bin/chmod +x /usr/local/bin/app/AppName. /usr/local/bin/app/AppNameThis command adds executable permissions to the files in the container .

  7. CMD ["./AppName"]: Set the default command when starting the container ./AppName. This will execute the executable file inside the container ./AppName.

Taken together, the function of this Dockerfile is to build a container based on the Debian Bullseye image and sources.listcopy the files on the host to the container to configure the package source. It then sets the working directory to that /usr/local/bin/app, copies all files and folders from the host to that directory in the container, and adds AppNameexecutable permissions to the files in the container. Finally, when the container starts, ./AppNamethe executable will be executed as the default command.

Note that the COPY command can only copy files in the current directory and subdirectories of the Dockfile, so the system /etc/apt/sources.listfiles are copied here in advance.

To build the image, you can use the following command:

docker build -t image-name:tag .

Among them, -tthe parameters specify the name and label of the image, .indicating the current directory where the Dockerfile is located. The build process will be executed step by step according to the instructions in the Dockerfile and the image will be generated.

Once the image is built, you can use the following command to view the list of built images:

docker images

Now you can use this image to create and run containers.

Create and run containers

To create a container, you can use the following command:

docker run -d --name container-name -p host-port:container-port image-name:tag

Among them, -dthe parameter indicates that the container is running in the background, --namethe parameter specifies the name of the container, -pthe parameter specifies the mapping relationship between the host port and the container port, image-name:tagand is the image name and label you want to use.

If you need to automatically restart the container, you can docker run -d --name container-name -p host-port:container-port image-name:tag append the following instructions at the end.
--restart=always: This option tells Docker to automatically restart the container when it exits. Whether the container is stopped manually or an error occurs that causes the container to exit, Docker will attempt to restart the container

After running the container, you can use the following command to view the list of running containers:

docker ps

If you want to enter the interactive terminal of the container, you can use the following command:

docker exec -it container-name /bin/bash

where container-nameis the name of the container.

To stop and delete a container, you can use the following commands:

docker stop container-name
docker rm container-name

These commands will stop the container and delete the container instance.

Export and import images

If you need to transfer an image from one environment to another, you can use the export and import methods.

export image

Export the image to a file using the following command:

docker save -o appname_image.tar appname:latest

This command will appname_image:latestexport the image named to appname_image_image.tara file named .

Import image

Use the following command to import an image file:

docker load -i appname_image.tar

This command will appname_image.tarload the image from a file named.

These are basic usage guidelines for Docker that will hopefully help you get started with Docker and start using containerization to build and deploy applications. By using Docker, you can more easily manage application dependencies, deployment environments, and running configurations. If you want to learn more about more functions and advanced usage of Docker, you can refer to the official Docker documentation or other related resources.

Guess you like

Origin blog.csdn.net/cheungxiongwei/article/details/131785930