Categories
Uncategorized

Read a paper container of -Docker

A, Docker Profile

1.1 What is a docker

docker dockers English meaning, which means carrying things mean, in fact, the characteristics of which are the same and docker, docker provides a kind of container handling things (our software program) process. docker himself could have been a run on the operating system software, it will provide a container environment, make our program run independently in the container, so that, from the official to the docker’s name is really the occasion.

Even the icon is so vivid, poetic, appeals to the imagination. . . . (This is the car to go to kindergarten, I have not turned into University City)

Imagine a scenario below: When we made our web site distributed, we have to add a server, and then configure the web configuration required in each server, such as: database, web server, is running what, so our website can run up, but every time we add a server, we have to re-allocation, it is cumbersome, with docker, we can configure our website and the required environment, labeled as a package (docker mirror), and then install the docker on the server, with docker pull packaged mirror, direct run (container) together on the line, what do not have control, it is convenient, more manageable, mirror modify the configuration, re update, all containers will also be changed, we will modify the website updated, particularly convenient.

1.2 docker features

docker features contained in its slogan

The first sentence is “Build, Ship and Run”. (Construction, transportation, run)

The second sentence, “Build once, Run anywhere (build once, run anywhere).”

Read the top of the scene, we can understand these two words, and now is not the time to build what software, run anywhere, really are embarrassed to say, from the beginning of java, and now a variety of cross-platform front-end frame (Flutter, ReactNative, Ionic Han), small framework (Taro), which is the liberation of developers reduce development costs and improve efficiency for the benefit of developing trifecta thing yard farmers ah, endless variety of applications, that we feel then, painful, bear the brunt, strikes, ah, accidentally exposed my culture.

1.3 docker and virtual machines

When it comes to docker, I believe before we all find out about, and it is necessary to compare the virtual machine to do something, and in fact docker is a virtual machine is something like that, we should know that the virtual machine is a virtual computer out on our operating system and then can be installed inside, run a variety of software, computer and we really are about the same, we can take this virtual good computer (actually a file) can be run directly on a virtual machine by other computers , inside stuff we do not need to install and configure a back and forth, it is also very convenient.

Is one such technology docker actually provided, but it is more than a virtual machine efficiency, quick start, taking advantage of a series of small resources, and the virtual machine is cumbersome, because the virtual machine to achieve ideological and docker on essentially different, we can compare the two pictures by the following:

The principle of operation of the virtual machine: a virtual computer hardware resources, hardware resources allocated out, and then out of the virtual multiple operating systems, it is a complete virtual PC.

Principle of operation docker: Virtual software resources, the computer in the network, what is divided into several virtual storage into a container, our software runs in a container, each easily occupies only part of the computer resources needed, not a complete computer.

Probably something like the top so understand it, anyway, do not know right or wrong, but should look good.

Therefore, from the operational point of view in principle, docker more lightweight, more cumbersome virtual machine, docker start very fast, it is also easy to deploy, so more and more people start using docker up.

Below is a comparison of the virtual machine and docker, here to note: But docker itself is not a container, but the container creation tool; it is rather a virtual machine virtual machine.

Two, docker’s core concept

Let’s introduce the use docker process must master the concepts, understanding of these concepts docker use and learning is very necessary.

2.1 mirror (Image)

Mirror in the end is what is it, a lot of people are confused when learning docker, but the mirror is crooked nuts soft spot for it, a lot of things have mirrored concept. For example, we install the system .iso file, in fact, a mirror, where you can put a mirror considered a template. We can use the docker container is created based on this template to run, in fact, more can be understood as a mirror is like a warehouse on github, we can clone under source code is then run the code up and running can be a website, an app Han, This can be called a container. To put it plainly, the mirror is a bunch of static template, the mirror is up and running container. Mirroring generally require us to pull down, and is read-only, this warehouse on our github clone is the same as the same.

docker mirroring the concept of layering is a mirror may be based on several mirror, such as a web operating environment may require the operating system ubuntu, a good database mysql, .net core runtime is running, then we will pull this mirror package this several mirror, which is the same as if we say in front of packaged operating environment, directly pulled down, like a small computer.

2.2 the container (Container)

When we pull up a mirror, and then run it, will be based on a container, the container running as if our application is running out, like this image, you can access can stop, many times we use the run command, run a lot container, it can be said mirror instance. From this perspective, we can be seen as mirroring the class, seen as a new container out of the instance, is very appropriate.

2.3 Warehouse (Repository)

Mirroring is the place to store warehouse, like github is more than a place to store the same code, we put github called warehouse code, github is the biggest warehouse. Then the place to store docker mirror we call dockerhub, a docker’s central warehouse. In fact, this site has dockerhub (https://hub.docker.com/), which is mirrored storage docker official warehouse, the official also save a lot here, to ensure the safety and reliability of the mirror, we can learn from mirroring the top pulling it to run our software. Of course, we can also make a good push up our own image, but these must be to the official audit, prevent some people write malicious code. However, we can push our own dockerhub up for our own use, this is like our github account same, belonging to the private mirroring.

2.4 data volume (Volume)

In fact, we like containers is a simple version of the operating system, but the system only environment we needed to run the program is installed, the front comes out of our containers are new examples, since it is out of the new instance of that will be destroyed, how do we destroyed that if the program generated a need for data persistence it, when we can run into the container to view the container, the container once it is destroyed nothing. Therefore, data volume is to solve this problem, is used for data persistence to data sharing between containers on our host, simply put, it is to map the host to the directory in the directory container, the application container the directory will be synchronized to read and write data on the host, the data generated by such containers can be a persistent, such as our database container, you can put the data stored on the disk of our real host the.

docker basic concept has been said, here since we can use a docker.

Third, the installation and use of docker

Because the server docker’s only run on linux operating system, of course, we can also learn to install docker windows, but too much trouble, need to open a series of configuration, we will take up in a linux system with a virtual machine learning docker. Here we learn linux ubuntu 16.04 version.

3.1 docker installation

We first execute the command to install docker

1. Perform sudo apt-get update Update Package

2. Run sudo apt-get -y install docker.io mounted docker

3. Enter the docker docker version check whether the installation was successful

If you have the following output, indicating that the installation was successful docker

Use of 3.2 docker

docker –help to view the help information, general software will have this command, I say no more.

docker version check the version of docker.

View docker docker info basic information about how many containers, mirror or something.

See docker images all the mirrors on the machine.

REPOSITORY: warehouse, also the name of the mirror.

TAG: label, is the version number, the mirror will have a different version number.

IMAGE ID: Mirror id, id based on this we can distinguish between different images, you can also operate on a mirror.

CREATED: creation time.

SIZE: the size of the image.

It is not currently a mirror on my machine, shown below:

docker pull : [label name]: pull the mirror, do not write the default label name to pull in the mirror.

We enter docker pull hello-world pulling the latest hello-world image

docker run : running a mirror, this time into a container, the equivalent of a new Image of. Below we run this image, outputs the following information run hello-world:

At this point, our docker’s hello-world has run successfully, and then we start a nginx server to visit us from the outside of the container nginx look.

Similarly, we first pull nginx mirror docker pull nginx, and then wait for the pull is complete, run nginx container, docker run -p 8080: 80 nginx

Then we use the built-in browser to access port 8080, there welcom to nginx represents a successful start.

The basic method is to use docker top of this, then we can look at the detailed parameters.

3.3 docker command

3.3.1 Mirror command:

docker images to view mirror of the present machine.

REPOSITORY: warehouse, also the name of the mirror.

TAG: label, is the version number, the mirror will have a different version number.

IMAGE ID: Mirror id, id based on this we can distinguish between different images, you can also operate on a mirror.

CREATED: creation time.

SIZE: the size of the image.

docker rmi delete the local mirror, plus a -f parameter represents the force delete, because sometimes if the relevant vessel running time can not be deleted, such as: docker rmi -f nginx forcibly stop the container and remove a mirror, regardless of whether there occupancy.

docker search based on name search remote mirror repository mirroring, you can look at all the relevant names found in the mirror, we can choose which image you want to pull, below is a search nignx related image, the red part of the explanation is ok official mirror.

docker pull : [label name]: pull the mirror, do not write the default label name to pull in the mirror.

docker push push mirror, when we made our own image, we can push ourselves to go docker hub.

3.3.2 container command

With a new image we can mirror instance, that is what we call the container.

docker run: a container based on a Mirror operation, if there is a local image to the image creation based on local, if no, go to the remote image and then creates a pull, the following parameters:

-d: starting a container, running in the background, does not occupy our current consoles are generally required to add, before we start nginx this parameter is not specified, will take up the current console, it would have been suspended, with this command will not be occupied.

-i: Run vessel in interactive mode, and typically use -t together (-it).

-t: container also create a command-line window, the contents of the container is a command line window, such as we pull a mirror image of ubuntu, we want to execute some commands at the operating system image inside, it would need this value.

-P: This is a capital P, randomly assigned to the host port of the port mapping the interior of the container.

-p: This is the lowercase p, specify a specific port mapping to the interior of the container port, such as in front of us with the -p 8080: 80, is to let the host of the 8080 port mapping to port 80 in the container, so that we can 8080 port with external access to the interior of the container nginx we (the default container must have an external port mapping, or not visit).

-v: Specifies the host and the interior of the container directory mapping, that is before the parameter data volume required, good for persistence and synchronization of data.

–name = “mynginx”: specify a name for the container, if not specified, it is assigned a random name.

Here we start with the parameters of the mirror nginx restart: docker run -itd -p 8848: 80 –name = “mynginx” nginx

docker ps display container is running, add a parameter -a can be seen in the container stop

docker stop stop the container.

docker kill forcibly stop the container.

docker restart to restart the container.

docker rm delete container, the container is not in the deletion can not be stopped and restarted.

For more information see the container docker inspect.

These are almost our common commands, functions much more specific we can see the official document https://docs.docker.com/get-started/.

Four, Dockerfile

We are on top of others pulled the mirror, we can actually make your own exclusive image, according to our needs, configure a mirror to our own use. Dockerfile is used to build our image files in it can write some command to build our image, to build a release docker hub can be used for others to pull back good.

Dockerfile is not a text file suffix, to realize we build mirrored by writing commands. Let’s take a look at Dockerfile how to write. Form below the command identifier <> parameter represents needed, [] is an optional parameter.

FROM: Specifies the base image, all building must have a mirror image of the base image, and FROM command must be the first order of Dockerfile.

FROM [AS ] Specifies image from a build up a new image AS name.

FROM [: ] [AS ] Specifies mirrored version tag.

For example: FROM mysql: 5.0 AS database

MAINTAINER: Mirroring the preservation of human information.

  MAINTAINER 

For example: MAINTAINER haha ​​[email protected]

RUN: the command to be executed when building a mirror.

  RUN

For example: RUN [ “executable”, “param1”, “param2”] RUN [ “dotnet restore”, “. * Csproj”]

ADD: adding local files to copy to the container, the archive will be unpacked, you can access files on the network, it will automatically download.

  ADD  

For example: ADD * .csproj / app csproj add files to the container app directory.

COPY: Function and ADD the same, but only copy, do not unzip or download files.

CMD: Command executed after the start of the container, and not the same as RUN, RUN command is mirrored in the building is to be run. When using the container docker run to run, this can be overridden at the command line.

  CMD [“executable”, “param1”, “param2”]

ENTRYPOINT: also execute commands, and CMD same, but the command will not be covered by the command line.

  ENTRYPOINT [“executable”, “param1”, “param2”]

For example: ENTRYPOINT [ “donnet”, “myapp.dll”]

LABEL: adding metadata, key-value in the form of a mirror image.

  LABEL =  =  =  ….

For example: LABEL version = “1.0” description = “This is a web application.”

ENV: setting environment variables, you will need some environment variables such as some container runtime: JAVA_HOME.

ENV first set an environment variable.

ENV = = = …. plurality of environment variables.

For example: ENV JAVA_HOME /usr/java1.8/

EXPOSE: exposure to external ports.

  EXPOSE

For example: EXPOSE 80

This refers to the internal procedures of the container port, though the same will and the host, but actually two ports, container operation, you need access to a port in the container with the -p port to an external mapping.

VOLUME: Specifies the data persistence directory, the official language is called the mount.

VOLUME / var / log specified container to be mounted directory, the directory will randomly mapped to a directory of the host, to achieve synchronization and data persistence.

VOLUME [ “/ var / log”, “/ var / test” …..] Specifies a plurality of containers need to be mounted directory, these directories will be mapped to a plurality of host random directories, data persistence and synchronization.

VOLUME / var / data var / log specified container var / log directory mount / var / data directory on the host, this form can be manually specified directory on the host.

WORKDIR: set the working directory, after you set up the working directory, on top of RUN, CMD, COPY, ADD and other working directories have become that much.

  WORKDIR

For example: WORKDIR / app / test

USER: User command is used to specify the run, and permissions for security reasons, some users may permission high, some low-privilege user may select different users according to the command to be executed.

  USER  :[]

For example: USER test

ARG: Construction parameter setting image is to be passed.

  ARG  [=]

For example: ARG name = sss

These are the command almost build Dockerfile used, and constructing a command is executed sequentially from top to bottom, the result may be behind the front command requires a command, use docker build compile Dockerfile can build up our image.

docker build a specified directory can be used Dockerfile -f argument, the default is to find in the current directory; -t specified and constructed mirror name tags, for example: docker build -f ./aa/bb -t myimage: 1.0

Fifth, build our own image

With the basis of the above, we can build our own image, and then can be uploaded to our own docker hub altar others pull use. Took over we have to achieve the following. Here reference to the official build steps: https: //docs.docker.com/engine/examples/dotnetcore/

5.1 Creating a project mvc

First, look for a directory, create a folder DockerDemo, then execute the command dotnet new mvc, mvc create our programs in this directory, as follows:

5.2 Add Dockerfile file

We add a name to direct Dockerfile file, and then enter the following command:

#构建sdk镜像,是为了进行编译、发布我们的web应用
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-

env # Arranged in a container

/app目录 WORKDIR /app # 复制csproj文件 到当前目录下(app目录下) 并执行dotnet restore 还原包 COPY *.csproj ./ RUN dotnet restore # 再复制其他剩余的文件到当前目录,执行发布命令,用Release模式 并且输出到out文件夹 COPY . ./ RUN dotnet publish -c Release -o out #构建运行时镜像 FROM mcr.microsoft.com/dotnet/core/aspnet:2.2

Mirroring a # runtime settings

/app目录 WORKDIR /

app The product of the previous image # replication

/app/out目录下的文件 到当前新镜像的 /app录下(当前目录) COPY --from=build-env /app/out . # 执行dotnet 命令,运行我们的web应用 ENTRYPOINT ["dotnet", "DockerDemo.dll"]

I added Dockerfile with vscode directly into a whale’s icon, it is a Docker file, as well as highlighting:

5.3 Construction of Docker image

We DockerDemo directly to this folder copied directly to a virtual machine, I copied down to the Desktop:

Then go to the next DockerDemo directory, execute commands directly docker build -t dockerdemo., The meaning of this sentence is to use Dockerfile command in the current directory build mirrors dockerdemo, pay attention to building the image name must be lowercase, otherwise it will error.

Then is to wait constructed, may be slower, if you want fast, then, can be replaced by domestic mirror source, I can not say here, because I do not bother to change.

The build process will be divided into two steps, because there are two mirrored building is completed, we use docker images to view, you will find our newly constructed mirror dockerdemo:

5.4 Running container

Next we enter the command docker run -d -p 8848: 80 –name myapp dockerdemo, run a container name is called myapp

Use docker ps to see running in the container:

5.5 Access to our mvc program

Type http browser virtual machine: // localhost: 8848, or enter the address of the virtual machine on your computer to access http://192.168.226.130:8848/ (this is my address), shown below, table name success:

Sixth, publish mirror

We’ve built our mirrors, then we’ll be pushed to mirror our own warehouse to docker, and github this step is exactly the same, like putting our code pushed to github same.

6.1 Create an account

First of all, we need to create a dockerhub account, log in https://hub.docker.com/, follow the prompts to register an account, this step would not elaborate.

6.2 Login

Command line input docker login, enter our account password to log on.

6.3 Push mirroring

Before pushing the mirror, first of all to mark our image, that is, to our image a name, use the command docker tag / : , below we mark our just dockerdemo mirror image :

Use the command docker tag dockerdemo emmaccc / dockerdemo: 1.0.0

We have just labeled View Mirror:

Use the command docker push emmaccc / dockerdemo: 1.0.0 Push mirroring, after a period push successful, you can see in their own account dockerhub

6.4 Mirroring

After a successful push, we can install on a computer docker in Renyiyitai pull up our image, we start directly run the web application.

docker run -p 4396: 80 emmaccc / dockerdemo: 1.0.0, then run, do not need to worry about the various dependencies, configuration, do not install anything on your new host, run directly run on the line, which docker unique charm lies.

VI Summary

Learning docker, looked back and forth several times, exactly what the hell, ah, quite a while to understand, the basic use of docker’s almost the same, the next estimate is k8s, alas, so many things, I feel the whole community has it in making the wheels of it, that engage in non-engage in this, no way, still have to follow the bigwigs to go, if you really do not understand, it would give you a word.

web put the tomcat, tomcat put docker in, docker put k8s years, k8s put on the operating system, around a large circle, it nonsense, direct web Why do not put directly on the operating system it runs.

 

Leave a Reply