In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1. Introduction of Docker
Docker is developed based on some features of the Linux operating system written in the go language. It provides operating system-level abstraction and is a container management technology that isolates the dependence of applications on infrastructure (operating system, etc.). Compared with virtual machines, Docker shares the hardware resources of the host and uses containers to provide an independent running environment to run applications. Virtual machines are based on Supervisor (hypervisor) use virtualization technology to provide isolated virtual machines and provide a running environment on the operating system of virtual machines! Although both provide good resource isolation, it is clear that Docker has lower virtualization overhead!
Docker involves three core concepts: Register, Image, and Container.
1. Registry: warehouse. It is used to store Docker images. For example, Docker's official Docker Hub is an open repository where we can download the images we need.
2. Image: image. The developer creates an application or service and packages it and its dependencies into a container image. A mirror is a static form of the configuration of an application and its dependencies.
3. Container: container. Container is a running instance of a mirror. It is an isolated, resource-controlled and portable runtime environment, which includes the operating system, programs to run, dependencies of running programs, environment variables, and so on.
The interaction of the three is as follows:
When we execute the Docker pull or Docker run command, if we do not have the required image locally, we will download (pull) an image from the repository (usually DockerHub). Docker executes the run method to get a container in which the user performs various operations. Docker executes the commit method to convert a container into a mirror. Docker uses commands such as login and push to push the local image to the repository. This image can be used on other machines or servers to generate containers and then run the corresponding applications.
II. Docker installation
1. Install using yum source. Due to the slow access to official source in China, add Ali source here.
> wget-P / etc/yum.repos.d/ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo > yum install-y docker-ce
2. Start Docker
/ / start Docker CE > systemctl start docker > systemctl enable docker / / View Docker status > systemctl status docker / / View docker version > docker-v
3. Test whether Docker is installed correctly and execute the command:
> docker run hello-world
A. When executing docker run hello-world, docker will first find the image of hello-world locally. If it does not exist locally, it will pull the image from the default image repository Docker Hub. After pulling the image locally, instantiate the image to get the container, and output Hello from Docker!.
B. Docker Engine provides the core technology of Docker: image (images) and container (containers). In the last step of the installation tutorial, you ran the Engine command docker run hello-world. This command, which includes three parts, enables Engine to complete the core task of Docker.
C. A container is a stripped-down version of the Linux operating system, and an image is the software loaded into the container. When you run this command, Engine will do the following:
1. Check to see if the software image hello-world exists
two。 Download the image from Docker Hub (learn more about Docker Hub later)
3. Load this image into the container and run it
The .Netcore project runs in Docker
1. Pull the microsoft/dotnet image, and wait a few minutes to install it. If you execute docker images, you can see that the local microsoft/dotnet image is already included.
> docker pull microsoft/dotnet
2. Run the microsoft/dotnet image. Use docker run to start the image, and start it in interactive mode (inside the container) by specifying the parameter-it. Execute the following commands in turn:
> docker run-it microsoft/dotnet / / start a dotnet image > dotnet new mvc-n mvctest / / create a .NET Core MVC project named mvctest > cd mvctest / / enter the mvctest folder > dotnet run / / start the .NET Core MVC project
The running result is shown in the following figure:
Press and hold Ctrl+C on the keyboard to close the application, and enter exit to exit the current container.
With the above simple steps to create and run a .NET Core MVC project, you may wonder, the .NET Core SDK is not installed on the Linux host, how is the MVC project created? This is the magic of Docker. The dotnet image we pulled from the image repository contains all the dependencies and runtime environments needed to create, build, and run the. NET Core project.
After exiting the container, execute find-name mvctest (look for the mvctest file), and we find that it is not found. This means that the .NET Core MVC project we just created is created inside the container and is completely isolated from the host. At this time, you may think, it is too inconvenient to install the source code in the container every time. Can we let the container run the source code project of our host? Well, that's a good question. Of course it is achievable, so let's answer this question.
4. Create the. NET Core project on the host machine
In order to create the. NET Core project on the host, at this point we need to install the .NET Core SDK on the Linux host
1. Install the .NET Core SDK on the host
Add Yum source: sudo rpm-Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
Start installation: yum install-y dotnet-sdk-2.1
Check the installation version. The following figure indicates that the installation is correct.
2. Create the. NET Core project
> mkdir data > cd data > dotnet new mvc-n mvctest / / create a .NET Core MVC project named mvctest > cd mvctest / / enter the mvctest folder > dotnet run / / start the .NET Core MVC project
Note: add the code .UseUrls ("http://*:5000")" to the Program.cs file and visit it in the browser as shown below:
Next, we will share the source code projects in this directory into the container by mounting them.
3. Mount the host project to the container
When starting the Docker image, Docker allows us to mount the host's files to the specified directory of the container by using the-v parameter. In other words, it is equivalent to the host sharing the specified file for the container to access
The `\ `in the / / command is combined with the `Enter` key to form a newline character, which allows us to enter a long command. > docker run-it\ >-v / data/mvctest/:/app\ > microsoft/dotnet:latest
The above command is to mount the files under the / data/mvctest/ folder to the container's\ app directory
Judging from the above execution results, the app directory inside the container contains the source code items on the host.
The above mentioned is in the form of sharing, not that the container has a copy of the host directory, which means that changes to the directory on the host machine will be immediately reflected in the container. Conversely, changes to the shared directory in the container will not be reflected on the host, otherwise the isolation feature of the container will be broken.
Through such a simple scenario, will you be smart enough to think of the application of this scenario in our daily coding? Yes, we can use it to CI continuously. The basic idea is to load the source code to the host through git clone, and then mount the source directory to the container to build it.
4. With the help of Dockerfile file
In the next part, we upgrade this operation without the dependence of Dockerfile. We can do it with one command.
Dockerfile is used to define the series of operations you will perform in the container. Let's create the first Dockerfile
> cd / data/mvctest/ make sure to enter the MVC project directory we created > touch Dockerfile / / use the touch command to create a Dockerfile > vi Dockerfile / / use the vi command to edit the Dockerfile
After entering the VI editing interface, copy the following code and use the shift + Ins command to paste. Then press ESE to exit editing mode, press shift +:, enter wq to save and exit the editing interface.
FROM microsoft/dotnet:latest WORKDIR / app COPY. / app RUN dotnet restore EXPOSE 5000 ENV ASPNETCORE_URLS http://*:5000 ENTRYPOINT ["dotnet", "run"]
Let me explain the above orders in turn:
Use FROM to specify the image used by the container
Use WORKDIR to specify a working directory
Using the COPY directive, copy the current directory (where. That is, represent the current directory) to the / app directory in the container
Use the RUN command to specify the commands executed in the container
Use EXPOSE to specify the port number exposed by the container
Use ENV to specify environment parameters, which are used to tell the .NET Core project to listen on port 5000 on all network interfaces
Use ENTRYPOINT to define the entry point of the container
When Dockerfile is ready, we can package our current project as an image to distribute the deployment.
Use the docker build-t instruction to package the image:
> docker build-t mvctest.web.
The above command tells docker to package the current directory into a mirror and name it hellodocker.web. After the command is executed, enter docker images to see our newly packaged image
After the image is created, we can run it directly:
> docker run-d-p 805000mvctest.web
The above instruction is to run our newly packaged image and use the-p parameter to map the container's 5000 to port 80 of the host, where the-d parameter tells docker to run the image as a background task. Since 80 is the default web port, we can access the MVC website running in our container by directly accessing ip through the browser
So far, we have completed the containerized deployment of the NET Core project with the help of Docker, and we will deploy the image on other machines later.
Push the image to the warehouse
Please register an account with Docker Hub yourself, and then we will put the locally packaged images under the warehouse under our account.
1. After registration, execute the command
> docker login
2. Execute the order again
> docker push
The push failed, indicating that the naming of our image does not conform to the standard. It turns out that the image should be named in / format before pushing. So how do you rename it? we rename it by tagging it.
The above information indicates that the push is successful. Check your repository, as shown below:
Finally, when we change the machine, we directly execute the following command to complete the multiple deployment.
> docker run-p 8081 docker run 5000 79522860/mvcdemo.web
Is it convenient to have your own image repository done above? if you combine business, you will find that more images are needed, and distributed deployment containers need to start containers one by one. No, we can also deploy images and containers with one click through configuration files, which we'll talk about in the next article.
6. Docker common commands
1. Container-related operations
> docker ps / / View currently running containers > docker ps-a / / View the status of all containers > docker start/stop id/name / / start / stop a container > docker attach id/ / enter a container (the container also stops running after exiting using exit) > docker rm id/name / / delete a container if it is running You need to stop > docker rm $(docker ps-a-Q) / / delete the stopped container > docker logs-f hello-world / / view the log records of the specified container.
> docker run-it-- name hello_001 hello-world / / create a container and specify the label
-I: allows us to interact with (STDIN) in the container
-t: specify a pseudo terminal or terminal in the new container
-- name: a name is given to the container, which can be omitted. If omitted, docker will randomly generate a name.
2. Mirror-related operations
> docker images / / View local images > docker rmi id/name / / Delete an image. If tag is not specified, the latest tag > docker rmi $(docker images-Q) / / deletes all images by default Be careful > docker rmi $(docker images-f "dangling=true"-Q) / / remove all unnamed images (possibly intermediate images generated during construction) > docker start/stop id/name / / start / stop a container > docker attach id/ / enter a container (the container stops running after exiting with exit)
Through the ID tag image, the following is a local image of tag with an id of 0e5574283393 to the "fedora" repository, with the tag name version1.0
> docker tag 0e5574283393 fedora/httpd:version1.0
The name tag image is locally mirrored to the repository "fedora" with the name "httpd" tag, and its tag name is version1.0
> docker tag httpd fedora/httpd:version1.0
Note that since the tag name referencing httpd is not specified, httpd:latest is referenced by default.
Tag an image by name and tag name, and label the local image named httpd and tag named test. The repository is fedora and the label name is version1.0.test.
> docker tag httpd:test fedora/httpd:version1.0.test
Tag an image to a private repository and push an image to a private registry instead of a public docker registry. You must specify a registry hostname and port to tag this image
> docker tag 0e5574283393 myregistryhost:5000/fedora/httpd:version1.
3. Uninstall Docker CE
A. Uninstall the Docker package
> yum remove docker-ce
B, images, containers, volumes or custom configuration files on the host will not be deleted automatically. Delete all mages, containers, volumes commands
> rm-rf / var/lib/docker
VI. Popularization of additional knowledge
1. The address of the official mirror library of docker
Https://hub.docker.com/r/microsoft/dotnet/
2. The difference between microsoft/dotnet image versions
A, microsoft/dotnet:-sdk (microsoft/dotnet:2.1-sdk)
This image contains the .NET Core SDK with .NET Core and command line tool (CLI). This image will be mapped to the development scenario. You can use this image for local development, debugging, and unit testing. This image can also be used to generate scenarios. The latest version is always available with microsoft/dotnet:sdk.
B, microsoft/dotnet:-runtime (microsoft/dotnet:2.1-runtime)
This image contains the .NET Core (runtime and libraries) and is optimized for running .NET Core applications in a production environment.
C 、 microsoft/dotnet:-runtime-deps
The runtime-deps image includes the operating system with all the native dependencies required by the .NET Core. This image applies to stand-alone applications.
3. Image acceleration
In view of the domestic network problems, it is very slow to pull the Docker image later. We can configure an accelerator to solve it. I use NetEase's image address: http://hub-mirror.c.163.com.
The new version of Docker uses / etc/docker/daemon.json (Linux) or% programdata%\ docker\ config\ daemon.json (Windows) to configure Daemon.
Please add to the configuration file (if there is no such file, please create one first):
{"registry-mirrors": ["http://hub-mirror.c.163.com"]}"
# refresh the configuration file and restart docker
Systemctl daemon-reloadsystemctl restart docker
* if you use aliyun, you need to log in to your Aliyun account to get your image address *
Summary
The above is the .NET Core Docker introduced by the editor to implement containerization and private image warehouse management. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to the website!
If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank 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.