In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to install and configure Docker". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, 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.
Dark magic.
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:
1. 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.
two。 Container: virtualizes the operating system layer and is a standard software unit
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.
3. Difference and relation
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 a small-scale virtual 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:
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.
Physical machine deployment can not make full use of resources, resulting in a waste of resources. When the virtual machine is deployed, the virtual machine itself will occupy a lot of resources, resulting in a waste of resources, and the performance of the virtual machine is also very poor. The containerized deployment is more flexible, lightweight, and the performance is better.
Virtual machines belong to virtualization technology, while container technologies such as Docker belong to lightweight virtualization.
Get to know Docker
Docker
1. Concept
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 and warehouse Repository.
Why 2.Docker is lightweight?
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.
IV. Core concepts
1.Build, Ship and Run (build, transport, run)
2.Build once, Run anywhere (build at once, run everywhere)
3.Docker itself is not a container, it is a tool for creating containers and an application container engine
The three core concepts of 4.Docker are: mirror Image, container Container and warehouse Repository.
5.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.
6. 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.
7. 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.
5. Install Docker
1. 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
two。 View version
Docker-v
3. 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}
4. Install the desktop side
Docker desktop side
Desktop operation is very simple, first go to the official website to download. Through the Docker desktop, we can easily operate:
Clone: clone a project
Build: package image
Run: running an instance
Share: shared image
All right, the work is ready, now we can show our skills!
VI. Get started quickly
After installing Docker, let's make a mirror image of the actual project and use it as we learn.
1. First of all, we need to get an overview of the 11 commands we're going to use.
two。 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.
3. Create a new Dockerfile
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.
4. Prepare Nginx Mirror
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;}}
5. Configure Mirror
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.
6. Build 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.
7. Run the container
Docker run-d-p 3000 80-- name docker-vue jartto-docker-demo
Explain the parameters here:
-d set the container to run in the background
-p indicates 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 more point to add:
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:
8. Access to the project
Because we mapped native port 3000, execute:
Curl-v-I localhost:3000
Or open a browser and visit: localhost:3000
9. Publish an 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] (https://hub.docker.com), register your account
Execute docker login on the command line, 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.
VII. 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.
1. Parameter usage
FROM: # specify a base image. All built images must have a base image. And the FROM command must be the first command of Dockerfile FROM [AS] to specify the name of a new image to be built from an image [:] [AS] specify the version of the image Tag example: FROM mysql:5.0 AS database MAINTAINER: # information about the maintainer of the image MAINTAINER example: MAINTAINER Jartto Jartto@qq.com RUN: # commands to be executed when building the image RUN example: RUN [executable, param1 Param2] ADD: # add and copy local files to the container The compressed package will be unzipped, you can access the files on the network, and the ADD example will be downloaded automatically: ADD * .js / app add the js file to the app directory in the container COPY: # function is the same as ADD, but the function is to copy, but not extract or download the file CMD: # the command executed after starting the container, unlike RUN, RUN is the command to run when building an image when you run the container using docker run This can be overridden on the command line example: CMD [executable, param1, param2] ENTRYPOINT: # also executes the command, just like CMD, except that this command will not be overwritten by the command line ENTRYPOINT [executable, param1, param2] example: ENTRYPOINT [donnet, myapp.dll] LABEL: # adds metadata to the image, key-value form LABEL = =. Example: LABEL version=1.0 description= this is a web application ENV: # setting environment variables, some containers will need some environment variables ENV to set 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 container's internal program, although it will be the same as that of the host But it is actually two ports) EXPOSE example: when the EXPOSE 80 container is running, you need to use-p to map the external port to access the port in the container VOLUME: # specify the directory for data persistence, the official language is VOLUME / var/log # specify the directory to be mounted in the container, and map this directory to a random directory of the host Implement data persistence and synchronization VOLUME [/ var/log,/var/test.] # specify multiple directories in the container that need to be mounted, map these directories to multiple random directories of the host, and achieve data persistence and synchronization VOLUME / var/data var/log # Mount the var/log directory in the specified container to the / var/data directory on the host In this form, you can manually specify the directory on the host WORKDIR: # set the working directory. After setting, the working directory of RUN, CMD, COPY, and ADD will be changed synchronously. WORKDIR example: WORKDIR / app/test USER: # specify the user used to run the command, for the sake of security and permissions Select different users according to the command to be executed USER: [] example: USER test ARG: # set the parameter ARG to be passed to build the image [=] ARG name=sss
VIII. Best practices
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: what kind of image is needed?
Step simplification: Step with fewer changes is preferred
The version is clear: the image name is clear.
Description document: the whole image packaging step can be reproduced
This is the end of the content of "how to install Docker configuration". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.