search
HomeOperation and MaintenanceDockerThe most systematic mastery of Docker core technology (summary sharing)

This article brings you some related questions about container operation of docker core technology, detailed explanation of Dockerfile, etc. I hope it will be helpful to you.

The most systematic mastery of Docker core technology (summary sharing)

1. Docker

1. Introduction

  • Based on the Cgroup, Namespace, and Union FS technologies of the Linux kernel, Encapsulating and isolating processes is a virtual technology at the operating system level. Since the isolated process is independent of the host and other isolated processes, it is called a container.
  • The initial implementation was based on LXC. LXC will be removed from 0.7 onwards. Switch to the self-developed Libcontainer. Starting from 1.11, it has further evolved to use runC and Containerd
  • Docker has further encapsulated it on the basis of the container, from file system, network interconnection to process isolation, etc. , which greatly simplifies the creation and maintenance of containers, making Docker technology lighter and faster than virtual machine technology

2. Docker advantages

  • Use it more efficiently System resources
  • Faster startup time
  • Consistent operating environment
  • Continuous delivery and deployment
  • Easier migration
  • More Easily maintain and expand

3. Comparison between Docker and virtual machines

##2. Docker installation

Reference article installation: Install Docker Engine on Ubuntu | Docker Documentation

##3. Container operation

    Start:
  • docker run:

-It interaction

-d Running

--P port mapping

--V disk hanging

  • Start the terminated container

  • docker start

  • Stop container

  • docker stop

  • View container process

  • docker ps

    ##View container details
  • docker inspect

## Copy the file to the container
  • docker cp file1 :/file_to_path

#docker exits the container without closing the container: ctrl q p

  • docker exits the container and closes the container: exit

  • ##Query all docker images

  • docker images

##Docker image warehouse

  • Docker hub: https://hub.docker.com

    Create a private image warehouse: docker run -d -p 5000:5000 registry

    Four. Dockerfile Detailed explanation
Dockerfile is generally divided into four parts: basic image information, maintenance information, image operation instructions and container startup operation instructions

Common commands

    From: Specify the basic mirror image, must be the first instructions
  • : Format:
From & LT; Image & GT FROM :

From & LT; Image & GT;@& LT; Digest & GT;

## Example:

## Ubuntu

AINTAINER: Maintenance Information

# This #Format:

MAINTAINER

Example

MAINTAINER ribbon

RUN: Command executed when building the image

Format:

Shell execution: RUN

Execution: RUN ["executable", "param1", "param2"]

Example:

RUN apk update

RUN ["/etc/execfile", "arg1", "arg2"]

RUN apt-get update && apt The two commands -get install are always connected with &&, otherwise the apt-get update build layer will be cached, which will cause the new package to fail to be installed

ADD: Add local files to the container, Types such as tar will automatically decompress and you can access network resources, similar to wget

Format:

ADD ...

Example:

ADD bin/amd64/httpserver /httpserver

COPY: The function is similar to ADD, but it does not decompress files and cannot access network resources

Use multi-stage in Dockerfile: multi-stage in Dockerfile (multi-stage build) - sparkdev - Blog Park

Format:

COPY ... Example:

COPYbin/amd64/httpserver /httpserver

CMD: Called after the container is built, that is, it is called only when the container starts

Format:

CMD ["executable","param1","param2"] (Execute executable file, priority)

CMD ["param1","param2"] ( If ENTRYPOINT is set, call ENTRYPOINT directly to add parameters)
           CMD command param1 param2 (execute shell internal command)

Example:

                                                                                          CMD ["ethtool", "--help"]

CMD echo "1111"

ENTRTPOINT: Configure the container to make it executable.

Format:

ENTRYPOINT ["executable", "param1", "param2"] (executable file, priority)

ENTRYPOINT command param1 param2 (shell internal command)

Example:

ENTRYPOINT /httpserver

CMD [-c]

LABAL: Used to add source data to the image

Format:

          LABEL = = = ...

Example:

       LABEL multi.label1="value1" multi.label2="value2" other="value3"

ENV: Set environment variables

Format:

ENV

Example:

ENV MY_SERVICE_PORT=80 UDP_PORT=90

EXPOSE: Specify external interaction Port

Format:

EXPOSE [...]

Example:

EXPOSE 80

EXPOSE 80/tcp

EXPOSE 80 90

VOLUME: Used to specify the persistence directory

Format:

VOLUME []

Example:

VOLUME ["/data", "/usr1/jenkins"]

USER: Specify the run The user name or UID of the container, and subsequent RUN will also use the specified user.

Format:

USER user
USER user:group
USER uid
USER uid:gid
USER user:gid
USER uid:group

Example:

USER www

ARG: Used to specify variables passed to the build runtime

Format:

ARG [=]
Example:
ARG build_user=ribbon

##

5. Detailed explanation of Linux NameSpace

  • Detailed explanation of NamesSpace:

Linux NameSpace_Frank_Abagnale's Blog-CSDN Blog This article provides a more detailed introduction. You can refer to this article

  • Common operations of NameSpace

  • to view the namespace of the current system:

lsns -t

  • View the namespace of a process:

ls -la /proc//ns/

  • View a namespace running command

nsenter -t -n

6. Detailed explanation of Linux Cgroups

  • Detailed explanation of Cgroups

Container Core: cgroups - Brief Book You can refer to this article to learn more

  • Simulate Cgroups to control CPU resources

Pass Simulate to better familiarize yourself with the effect of Cgroups controlling resources. First create the cpudemo folder

Execute top and you can see that busyloop takes up two CPU resources

Add the process to the cgroup process configuration group

Set cpuquota

You can see that success will occupy 200% The CPU resources are reduced to 1%

  • Simulating Cgroups exceeding the limit memory resources and being killed by OOM

/ Create the memorydemo folder in the sys/fs/cgroup/memory directory

Run the memory-consuming program and use watch to query the memory usage

Configure the process into the cgroups configuration group

Set the maximum memory size

Waiting for the program Killed by OOM, dmesg can see the kill information

Note: To delete self-created cgroup folders, you need to use cgroup-tools

7. Union FS

The technologies used by Docker are all derived from Linux technologies and are not There is no innovation, and the innovation of Docker is the file system.

1. Concept:

  • File system that mounts different directories under the same virtual file system
  • Supports setting readonly, readwrite and without for each member directory -able permissions
  • File system layering, the directory with readonly permissions can be logically modified. The modifications here are incremental and do not affect the readonly part
  • Usual Union FS uses: Multiple disks are mounted to the same directory, and the other is to combine the readonly part and the writeable directory

2. Illustration of Union FS

In the design of the Docker image, The concept of layer is introduced, that is to say, every step of the user's image creation operation will generate a layer, that is, an incremental rootfs (a directory), so that the containers where application A and application B are located jointly reference the same The ubuntu operating system layer and the Golang environment layer (as read-only layers) each have their own application layer and writable layer. When starting the container, mount the relevant layers to a directory through UnionFS as the root file system of the container.

3. Container storage driver

4. Simulate Union FS to better understand the effect

Since the current version of docker uses the overlayFS storage driver, we use the overlay mounting method to conduct experiments. Overlayfs passes through three directories: lower Directory, upper directory, and work directory are implemented. There can be multiple lower directories. The work directory is the basic working directory. After mounting, the content will be cleared and its content will not be visible to the user during use. Finally, the joint mounting is completed. The unified view presented to the user is called the merged directory.

Execute the following command:

mkdir upper lower merged work
echo "lower" > lower/in_lower.txt
echo "from lower" > lower/in_both.txt
echo "from upper" > upper/in_both.txt
echo "upper" > upper/in_upper.txt
path=$(pwd)
mount -t overlay overlay -o lowerdir=${path}/lower,upperdir=${path}/upper,workdir=${path}/work ${path}/merged

## You can see that the overlay storage driver file is mounted using Effect. After the experiment is completed, you need to restore the environment after umounting the merged directory, and then deleting the four directories. If you delete the others first, rm: cannot remove 'merged/': Device or resource busy may appear, resulting in the merged directory not being deleted.

Eight. Docker network

1. Installation tools

Centos system:

        $ yum install bridge-utils

Ubuntu system:

                                                            $ apt-get   install bridge-utils

2. Docker network mode

  • Query the built-in docker Network mode

  • docker run Select the network mode to run

1) Host mode: Specify using --net=host. Share a set of net with the host

# 2) none mode: use --net=none specified. The network configuration needs to be configured by yourself

3) Bridge mode: Use --net=bridge to specify, the default setting.

docker network logic diagram bridge and NAT

4) Container mode: Use --net=container:NAME_or_ID to specify. Using the network configuration of other containers

# The network mode diagram is roughly as shown below

3. Simulate the operation of Docker to start a network bridge

  • Create --net=none nginx

  • Create network namespace

    ## Create network namespace link

## Check the currently created bridge device

Create veth pair

to configure A network

Configure B network
  • Generate eth0 network device in nginx docker

Configure ip gateway for eth0

nginx can access

Configure nat so that windows can also access through ip

Delete the specified nat rule after use

Recommended learning: "docker video tutorial

"

The above is the detailed content of The most systematic mastery of Docker core technology (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Linux Containers: The Foundation of DockerLinux Containers: The Foundation of DockerApr 14, 2025 am 12:14 AM

LXC is the foundation of Docker, and it realizes resource and environment isolation through cgroups and namespaces of the Linux kernel. 1) Resource isolation: cgroups limit CPU, memory and other resources. 2) Environment isolation: namespaces provides independent process, network, and file system views.

Docker on Linux: Best Practices and TipsDocker on Linux: Best Practices and TipsApr 13, 2025 am 12:15 AM

Best practices for using Docker on Linux include: 1. Create and run containers using dockerrun commands, 2. Use DockerCompose to manage multi-container applications, 3. Regularly clean unused images and containers, 4. Use multi-stage construction to optimize image size, 5. Limit container resource usage to improve security, and 6. Follow Dockerfile best practices to improve readability and maintenance. These practices can help users use Docker efficiently, avoid common problems and optimize containerized applications.

Using Docker with Linux: A Comprehensive GuideUsing Docker with Linux: A Comprehensive GuideApr 12, 2025 am 12:07 AM

Using Docker on Linux can improve development and deployment efficiency. 1. Install Docker: Use scripts to install Docker on Ubuntu. 2. Verify the installation: Run sudodockerrunhello-world. 3. Basic usage: Create an Nginx container dockerrun-namemy-nginx-p8080:80-dnginx. 4. Advanced usage: Create a custom image, build and run using Dockerfile. 5. Optimization and Best Practices: Follow best practices for writing Dockerfiles using multi-stage builds and DockerCompose.

Docker Monitoring: Gathering Metrics and Tracking Container HealthDocker Monitoring: Gathering Metrics and Tracking Container HealthApr 10, 2025 am 09:39 AM

The core of Docker monitoring is to collect and analyze the operating data of containers, mainly including indicators such as CPU usage, memory usage, network traffic and disk I/O. By using tools such as Prometheus, Grafana and cAdvisor, comprehensive monitoring and performance optimization of containers can be achieved.

Docker Swarm: Building Scalable and Resilient Container ClustersDocker Swarm: Building Scalable and Resilient Container ClustersApr 09, 2025 am 12:11 AM

DockerSwarm can be used to build scalable and highly available container clusters. 1) Initialize the Swarm cluster using dockerswarminit. 2) Join the Swarm cluster to use dockerswarmjoin--token:. 3) Create a service using dockerservicecreate-namemy-nginx--replicas3nginx. 4) Deploy complex services using dockerstackdeploy-cdocker-compose.ymlmyapp.

Docker with Kubernetes: Container Orchestration for Enterprise ApplicationsDocker with Kubernetes: Container Orchestration for Enterprise ApplicationsApr 08, 2025 am 12:07 AM

How to use Docker and Kubernetes to perform container orchestration of enterprise applications? Implement it through the following steps: Create a Docker image and push it to DockerHub. Create Deployment and Service in Kubernetes to deploy applications. Use Ingress to manage external access. Apply performance optimization and best practices such as multi-stage construction and resource constraints.

Docker Troubleshooting: Diagnosing and Resolving Common IssuesDocker Troubleshooting: Diagnosing and Resolving Common IssuesApr 07, 2025 am 12:15 AM

Docker FAQs can be diagnosed and solved through the following steps: 1. View container status and logs, 2. Check network configuration, 3. Ensure that the volume mounts correctly. Through these methods, problems in Docker can be quickly located and fixed, improving system stability and performance.

Docker Interview Questions: Ace Your DevOps Engineering InterviewDocker Interview Questions: Ace Your DevOps Engineering InterviewApr 06, 2025 am 12:01 AM

Docker is a must-have skill for DevOps engineers. 1.Docker is an open source containerized platform that achieves isolation and portability by packaging applications and their dependencies into containers. 2. Docker works with namespaces, control groups and federated file systems. 3. Basic usage includes creating, running and managing containers. 4. Advanced usage includes using DockerCompose to manage multi-container applications. 5. Common errors include container failure, port mapping problems, and data persistence problems. Debugging skills include viewing logs, entering containers, and viewing detailed information. 6. Performance optimization and best practices include image optimization, resource constraints, network optimization and best practices for using Dockerfile.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools