Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the core concept of Docker?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the core concept of Docker". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the core concept of Docker"?

Tell a story.

To better understand what Docker is, let's start with a story:

I need to build a house, so I move stones, cut wood, draw drawings and build a house. After an operation, the house was finally built.

As a result, after living for a while, I wanted to move to the seaside on a whim. At this time, according to the previous method, I could only go to the seaside and move stones, cut wood, draw drawings and build houses again.

When I was in trouble, a wizard came and taught me a kind of magic. This magic can make a copy of the house I have built, make a "mirror image" and put it in my backpack.

When I get to the seaside, I will use this "mirror image" to copy a house and check in with my bag.

Isn't that amazing? Corresponding to our project, the house is the project itself, the mirror is the copy of the project, and the backpack is the mirror warehouse.

If you want to expand the capacity dynamically, take out the project image from the repository and copy it casually. Build   once,Run   anywhere!

No longer pay attention to version, compatibility, deployment and other issues, completely solved the "online collapse, endless construction" embarrassment.

Virtual machines and containers

Before we begin, let's make some reserves of basic knowledge:

① virtual machines: virtualized Hardwar

Virtual machine Virtual Machine refers to a complete computer system with complete hardware system functions simulated by software and running in a completely isolated environment. Everything that can be done in a physical computer can be done in a virtual machine.

When creating a virtual machine in a computer, it is necessary to use part of the hard disk and memory capacity of the physical machine as the hard disk and memory capacity of the virtual machine.

Each virtual machine has its own CMOS, hard disk, and operating system, which can operate on the virtual machine like a physical machine. Before container technology, the online celebrities in the industry were virtual machines.

The representatives of virtual machine technology are VMWare and OpenStack.

② container: virtualizes the operating system layer and is a standard software unit

Its characteristics are as follows:

Run anywhere: the container can package code with configuration files and related dependent libraries to ensure that it runs consistently in any environment.

High resource utilization: containers provide process-level isolation, so you can set CPU and memory usage more finely, thus making better use of the server's computing resources.

Rapid expansion: each container can be run as a separate process and can share the system resources of the underlying operating system, thus speeding up the start and stop efficiency of the container.

Differences and connections:

Although virtual machines can isolate many "subcomputers", they take up more space and start more slowly. Virtual machine software may cost money, such as VMWare.

Container technology does not need to virtualize the entire operating system, only needs to virtualize a small-scale environment, similar to the "sandbox".

Running space, virtual machines generally need several GB to dozens of GB of space, while containers only need MB level or even KB level.

Let's take a look at the comparative data:

Virtual machines belong to virtualization technology, while container technologies such as Docker belong to lightweight virtualization.

The container is lighter and faster than a virtual machine because it takes advantage of the Linux underlying operating system to run in an isolated environment.

The Hypervisor of a virtual machine creates a very strong boundary to prevent applications from breaking through it, while the boundaries of containers are not so strong.

Get to know Docker

Docker is an open source application container engine that allows developers to package their applications and dependency packages into a portable container, publish them to any popular Linux machine, and virtualize them. Containers are fully sandboxed and will not have any interface with each other.

The three core concepts of Docker technology are:

Mirror Image

Container Container

Warehouse Repository

What is the reason for the lightweight of Docker? I believe you will also have the same doubt: why does Docker start so fast? How to share the kernel with the host?

When we ask Docker to run the container, Docker sets up a resource-isolated environment on the computer.

Then copy the packaged application and associated files to the file system within Namespace, and the configuration of the environment is complete. Docker then executes the command we specified in advance to run the application.

The mirror does not contain any dynamic data, and its content will not be changed after it is built.

Core concept

The core concepts are as follows:

Build,Ship and Run (build, transport, run).

Build   once,Run   anywhere (build once, run everywhere).

Docker itself is not a container, it is a tool for creating containers and an application container engine.

The three core concepts of Docker are: image Image, container Container and warehouse Repository.

Docker technology uses the Linux kernel and kernel functions, such as Cgroups and namespaces, to separate processes so that they run independently of each other.

Because Namespace and Cgroups features are only available on Linux, the container cannot run on other operating systems. So how does Docker run on macOS or Windows? Docker actually uses a trick to install a Linux virtual machine on a non-Linux operating system, and then run the container inside the virtual machine.

An image is an executable package that contains the code, runtime, libraries, environment variables, and configuration files needed to run the application, and the container is the runtime instance of the image.

For more information about how Docker works, you can check out the simple Guide to how Docker works and containerization. I won't repeat it here:

Http://dockone.io/article/8788

Install Docker

① command line installation

Homebrew's Cask already supports Docker for Mac, so you can easily install it using Homebrew Cask by executing the following command:

Brew cask install docker

For more installation methods, please see the official documentation:

Https://www.docker.com/get-started

② View version

The command is as follows:

Docker-v

③ configure Mirror acceleration

Set the Docker Engine write configuration:

{"registry-mirrors": ["http://hub-mirror.c.163.com/"," https://registry.docker-cn.com"], "insecure-registries": [], "experimental": false, "debug": true}

④ installation desktop side

Desktop operation is very simple, first go to the official website to download [1]. Through the Docker desktop, we can easily operate:

Clone: clone a project.

Build: package the image.

Run: run the instance.

Share: share the image.

All right, the work is ready, now we can show our skills!

Quick start

After installing Docker, let's make a mirror image of the actual project and use it as we learn.

① first needs to get an overview of the 11 commands we're going to use.

As shown below:

② New Project

To be quick, let's build the project directly using Vue scaffolding:

Vue create docker-demo

Try to start it:

Yarn serve

Access address: http://localhost:8080/. The project is ready, and we go on to package the project:

Yarn build

At this point, the Dist in the project directory is the static resource we are going to deploy, so let's move on to the next step.

It should be noted that front-end projects are generally divided into two categories, one is direct Nginx static deployment, and the other is to start Node services. We will only consider the first one in this section. With regard to Node services, I will explain in detail later.

③ New Dockerfile

The command is as follows:

Cd docker-demo & & touch Dockerfile

The project directory at this time is as follows:

. ├── Dockerfile ├── README.md ├── babel.config.js ├── dist ├── node_modules ├── package.json ├── public ├── src └── yarn.lock

You can see that we have successfully created the Dockerfile file in the docker-demo directory.

④ prepares Nginx image

Run your Docker desktop, and the instance will be launched by default. We will pull the Nginx image in the console:

Docker pull nginx

The following message appears on the console:

Using default tag: latest latest: Pulling from library/nginx 8559a31e96f4: Pull complete 8d69e59170f7: Pull complete 3f9f1ec1d262: Pull complete d1f5ff4f210d: Pull complete 1e22bfa8652e: Pull complete Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest

If you have such an exception, please make sure that the Docker instance is running properly.

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Image preparation OK, let's create a Nginx configuration file in the root directory:

Touch default.conf

Write:

Server {listen 80; server_name localhost; # charset koi8-r; access_log / var/log/nginx/host.access.log main; error_log / var/log/nginx/error.log error; location / {root / usr/share/nginx/html; index index.html index.htm;} error_page 500 502 503 504 / 50x.html Location = / 50x.html {root / usr/share/nginx/html;}}

⑤ configuration image

Open Dockerfile and write the following:

FROM nginx COPY dist/ / usr/share/nginx/html/ COPY default.conf / etc/nginx/conf.d/default.conf

Let's explain the code line by line:

FROM nginx specifies that the image is based on the nginx:latest image.

The COPY dist/ / usr/share/nginx/html/ command means that all files in the dist folder under the root of the project are copied to the mirror / usr/share/nginx/html/ directory.

COPY default.conf / etc/nginx/conf.d/default.conf copies the default.conf to etc/nginx/conf.d/default.conf and replaces the default configuration in the Nginx image with the local default.conf configuration.

⑥ builds an image

Docker uses the build command to build the image:

Docker build-t jartto-docker-demo.

By convention, let's explain the above code:

The-t parameter names the image jartto-docker-demo.

. The image is built based on the Dockerfile of the current directory.

When the execution is successful, it will output:

Sending build context to Docker daemon 115.4MB Step 1ax 3: FROM nginx-- > 2622e6cca7eb Step 2 COPY default.conf 3: COPY dist/ / usr/share/nginx/html/-- > Using cache-- > 82b31f98dce6 Step 3 Accord 3: COPY default.conf / etc/nginx/conf.d/default.conf-- > 7df6efaf9592 Successfully built 7df6efaf9592 Successfully tagged jartto-docker-demo:latest

The mirror image is made successfully! Let's take a look at the container:

Docker image ls | grep jartto-docker-demo

As you can see, we typed out a project image of 133MB:

Jartto-docker-demo latest 7df6efaf9592 About a minute ago 133MB

There are also good and bad images. We will introduce how to optimize them later, which can be ignored for the time being.

⑦ running Container

The command is as follows:

Docker run-d-p 3000 80-- name docker-vue jartto-docker-demo

Explain the parameters here:

-d sets the container to run in the background.

-p represents port mapping, mapping local port 3000 to container port 80 (so that the external network can be accessed through local port 3000.

-- name sets the container name docker-vue.

Jartto-docker-demo is the name of the image we built above.

One additional point: on the console, we can view the ID of the Container we just ran through docker ps:

Docker ps-a

The console outputs:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab1375befb0b jartto-docker-demo "/ docker-entrypoint." 8 minutes ago Up 7 minutes 0.0.0.080/tcp docker-vue 3000-> 80/tcp docker-vue

If you use the desktop side, open Docker Dashboard and you can see a list of containers, as shown below:

⑧ accesses the project

Because we mapped native port 3000, execute:

Curl-v-I localhost:3000

Or open a browser and visit: localhost:3000.

⑨ publish Image

If you want to contribute to the community, you need to release the image for other developers to use.

You need to take the following steps to publish an image:

Log in to dockerhub [2] and register your account.

The command line executes docker login, and then enter our account password to log in.

Before pushing the image, you need to type a Tag and execute docker tag.

/: .

The whole process is over, and we will use it in the future. We no longer need to "move stones, cut wood, draw drawings, build houses" and carry bags to check in. This is also the unique charm of Docker.

Routine operation

So far, congratulations on having completed the entry-level project of Docker! If you want to go further, you might as well move on.

Use the ① parameter

FROM:

Specify the base image. All built images must have a base image, and the FROM command must be the first command of Dockerfile.

FROM

[AS

] specifies that a new image name is built from one image

FROM

[:

] [AS] specify the version of the image Tag

Example: FROM mysql:5.0 AS database

MAINTAINER:

Mirror maintainer's information

MAINTAINER

Example: MAINTAINER Jartto Jartto@qq.com

RUN:

Commands to be executed when building an image

RUN

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

ADD:

Add and copy local files to the container, the compressed package will be unzipped, files on the network can be accessed, and will be downloaded automatically

ADD

Example: ADD * .js / app add the js file to the app directory in the container

COPY:

The function is the same as ADD, except for copying, not extracting or downloading files.

CMD:

The command executed after starting the container, unlike RUN, RUN is the command to run when building the image

This can be overridden on the command line when running the container using docker run

Example: CMD ["executable", "param1", "param2"]

ENTRYPOINT:

Also executes the command, just like CMD, except that this command will not be overridden by the command line

ENTRYPOINT ["executable", "param1", "param2"]

Example: ENTRYPOINT ["donnet", "myapp.dll"]

LABEL: add metadata to the image, in key-value form

LABEL = =...

Example: LABEL version= "1.0" description= "this is a web application"

ENV: set environment variables, some containers will need some environment variables when they run

ENV sets one environment variable at a time

ENV = set multiple environment variables

Example: ENV JAVA_HOME / usr/java1.8/

EXPOSE: expose the external port (the port of the internal program of the container, although it will be the same as that of the host, but it is actually two ports)

EXPOSE

Example: EXPOSE 80

When the container is running, you need to map the external port with-p to access the port within the container.

VOLUME: specifies the directory where the data is persisted. The official language is called mount.

VOLUME / var/log specifies the directory to be mounted in the container, and maps this directory to a random directory of the host to achieve data persistence and synchronization.

VOLUME ["/ var/log", "/ var/test".] Specify multiple directories in the container that need to be mounted, which will be mapped to multiple random directories of the host to achieve data persistence and synchronization

VOLUME / var/data var/log specifies that the var/log directory in the container is mounted to the / var/data directory on the host. This form allows you to manually specify the directory on the host.

WORKDIR: set the working directory. After setting it, the working directories of RUN, CMD, COPY and ADD will be changed synchronously.

WORKDIR

Example: WORKDIR / app/test

USER: specify the user to use when running the command. For security and permissions, select different users according to the command to be executed.

USER: []

Example: USER test

ARG: sets the parameter to be passed to build the image

ARG [=]

ARG name=sss

For more operations, please move to the official documentation [3]:

Https://docs.docker.com/

Best practic

After mastering the general operation of Docker, we can easily type out the project image we want.

However, the images produced by different operations are also very different. We might as well continue to explore what causes the differences in mirrors.

The following are the best practices in the process of applying Docker. Try to follow the following guidelines:

Require is clear about what mirrors are needed.

Step simplification: Step with few changes is preferred.

Version is clear: the name of the image is clear.

Description document: the whole image packaging step can be reproduced.

At this point, I believe you have a deeper understanding of "what is the core concept of Docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report