Docker profile

Docker is an open source application container engine, which is based on the Go language and complies with the Apache2.0 protocol.

Docker allows developers to package their applications and dependencies into a lightweight, portable container that can then be distributed to any popular Linux machine, as well as virtualization.

Containers are completely sandboxed, have no interfaces with each other (like iPhone apps), and most importantly, have very low performance overhead.

Docker application scenario

1. Automated packaging and distribution of Web applications.

2. Automated testing and continuous integration and release.

Deploy and adjust database or other background applications in a service environment.

Build from scratch or extend existing OpenShift or Cloud Foundry platforms to build your own PaaS environment.

The advantages of the docker

1. Simplicity: Docker allows developers to package their applications and dependencies into a portable container and distribute them to any popular Linux machine for virtualization. Docker changes the way of virtualization, so that developers can directly put their work into Docker for management. Convenience and speed has been the biggest advantage of Docker. Tasks that used to take days or even weeks can be completed in seconds under the processing of Docker containers.

2. Avoid choice phobia: If you have choice phobia, you are a senior sufferer. Docker helps you pack your tangle! Docker images; Docker image contains the operating environment and configuration, so Docker can simplify the deployment of multiple application instances. Web applications, backend applications, database applications, big data applications such as Hadoop clusters, message queues, and so on can all be packaged and deployed as a single image.

3. Cost saving: On the one hand, with the advent of the era of cloud computing, developers do not need to configure expensive hardware in pursuit of effect. Docker has changed the mindset that high performance inevitably leads to high price. The combination of Docker and cloud makes cloud space more fully utilized. It not only solves the problem of hardware management, but also changes the way virtualization is done.

Docker architecture

Docker uses client-server (C/S) architecture mode, and uses remote API to manage and create Docker containers.

2. Docker containers are created by Docker images.

The relationship between containers and images is similar to that between objects and classes in object-oriented programming.

Docker object-oriented
The container object
The mirror class

Docker image (Images) Docker images are templates used to create Docker containers.
Docker Container (Container) A container is a single application or group of applications that run independently.
Docker Client Docker client through the command line or Docker apis used by the other tools to communicate with the Docker daemon (https://docs.docker.com/reference/api/docker_remote_api).
Docker Host (Host) A physical or virtual machine for executing Docker daemons and containers.
The Docker warehouse (Registry) Docker repository is used to store images, which can be understood as code repository in code control. Docker Hub(https://hub.docker.com) provides a huge collection of images for use.
Docker Machine Docker Machine is a command line tool to simplify the installation of Docker, through a simple command line can be installed on the corresponding platform Docker, such as VirtualBox, Digital Ocean, Microsoft Azure.

Basic concepts in Docker

1. Image

A mirror, cognitively simple, is an object-oriented class, equivalent to a template. In essence, a mirror is a file system. Docker image is a special file system, in addition to providing programs, libraries, resources, configuration files required by the container runtime, but also contains some configuration parameters prepared for the runtime (such as anonymous volumes, environment variables, users, etc.). The image does not contain any dynamic data and its contents are not changed after the build.

2. Container

A container, conceptually, is an instance of a class, an entity created from a template that mirrors it. The essence of a container is a process, but unlike processes that execute directly on the host, container processes run in their own separate namespace. So a container can have its own root file system, its own network configuration, its own process space, and even its own user ID space. The processes inside the container run in an isolated environment and are used as if they were operating on a separate system from the host. This feature makes container-wrapped applications more secure than running directly on the host.

3. Repository

A repository, cognitively speaking, is like a software package upload and download site, where different versions of various software are uploaded for users to download. After the image is built, it can be easily run on the current host. However, if the image needs to be used on other servers, we need a centralized service to store and distribute the image, and Docker Registry is such a service.

4. Docker version

Docker is divided into CE and EE. CE stands for Community edition (free, three-month support cycle) and EE stands for enterprise edition, which emphasizes security and pays for use. After version 1.13, starting from March 1, 2017, the version naming rules are changed as follows:

Item Description Version Format YY.MM Stable Version Edge version released quarterly Edge version released monthly Docker CE Edge version released monthly (17.03, 17.04, 17.05…) , release a Stable release every three months (17.03, 17.06, 17.09…) , Docker EE and Stable version numbers remain the same, but each version provides one-year maintenance.

5. Tiered storage

Because the image contains the complete root file system of the operating system, and its volume is often huge, Docker made full use of the technology of Union FS to design it as a layered storage architecture. So, strictly speaking, an image is not a packaged file like an ISO. An image is a virtual concept whose actual embodiment is not a file but a group of file systems, or a combination of multiple file systems.

When a mirror is built, one layer is built on top of the other. After each layer is built, there are no more changes, and any changes on the next layer only happen on your own layer. For example, deleting a file at the previous layer does not actually delete the file at the previous layer, but only marks the file as deleted at the current layer. This file will not be seen when the final container runs, but it will actually follow the image. Therefore, when building the image, extra care should be taken. Each layer should contain only what needs to be added to that layer, and any extras should be cleared away before the layer is built.

The feature of hierarchical storage also makes it easier to reuse and customize images. You can even use the previously built image as the base layer and then add new layers to customize what you need to build new images.

Under Windows Docker installed, you can refer to novice tutorial: www.runoob.com/docker/wind…

Note: Docker can be installed on MAC, Windows, and Linux. However, in The Windows system, Docker currently only has the installation package of Win10 Professional edition and Enterprise edition, and win7/ Win8 / Win10 home edition needs to be installed through Docker Toolbox.

Boot2Docker is a lightweight Linux distribution designed for Docker, to solve the problem of Windows or OS X users can not install Docker. Boot2Docker runs entirely in memory, is 24M in size and only takes 5-6 seconds to boot. Boot2Docker needs to run in VirtualBox, so it generally exists as a Docker image.

Looks like something went wrong in step ´Looking for vboxmanage.exe´ Press any key to continue…” , you can consult: www.cnblogs.com/yqpy/p/9529…

Another point is that docker will automatically assign an IP to our virtual machine after startup, which can be seen in the startup window

Have time can take a good look at the introduction of the rookie tutorial on Docker, personal feel very useful, how to use, and some of the introduction of the command

Detailed will not record, here their own record may often use the command, the specific parameters of the command can be taken a look at the novice tutorial

Docker run: Create a new container and run a command 2, docker start: start one or more stopped containers 3, docker stop: stop a running container 4, docker restart: restart the container 5, docker rm: Delete one or more containers. Docker create: Create a new container without starting itexecDocker ps: lists containers. Docker commit: creates a new image from the container. 10, Docker pull: pull or update the specified image from the image repository 11, docker push: upload the local image to the image repository, login to the image repository 12, docker search: Docker images: Lists local images. Docker images: Lists local images. Docker info: displays docker system information, including the number of images and containers. 16. Docker version: Displays the docker version.Copy the code

Create a mirror image

When the image we download from the Docker image repository does not meet our needs, we can change the image in the following two ways.

1. Update the image from the created container and commit the image

2. Create a new image using the Dockerfile directive


Dockfile grammar:

1. Basic explanation

Dockfile syntax

2.1, the FROM

Note: The first directive must be FROM, which specifies a base image to build FROM, or if it is not locally available it will be pulled FROM the public library. The default latest tag is used for tags that do not specify the image, which can appear more than once if you need to build multiple images in a Dockerfile.

2.2、 MAINTAINER

Description: Describes the creator, name, and email address of the image

2.3、 MAINTAINER

Note: The RUN command is a common command. After the RUN command is executed, a new image will be created, which also refers to the layered construction of the image. A RUN sentence is a layer, also equivalent to a version. That’s how caching works. We know that The docker image layer is read-only, so if you install the software in the first sentence, it is impossible to delete the following sentence. So this is done in a single RUN command, where multiple RUN statements can be connected by an ampersand. RUN must be followed by double quotation marks, not single quotation marks. The shell will not be called by command, so the variable will not be inherited.HOME “.

2.4、 CMD

CMD command param1 param2

CMD can only appear once in a Dockerfile, there are more than one, only the last one is valid. It provides a default command item when the container is started. If the user provides a command item when executing docker run, the command will be overwritten. Build time commands are used if not provided.

2.5、 EXPOSE

Description: Tells the Docker server the container port number mapped to the external container, which takes effect when Docker run -p.

2.6、 EVN

EVN = Allow multiple Settings at a time

Note: Set the container’s environment variable, which can be used by the following RUN command. This variable will be retained while the container is running.

2.7, the ADD

Copy local files or directories or remote files, add to the specified container directory, support GO’s regular fuzzy matching. A path is an absolute path and is not automatically created. If the source is a directory, only the contents of the directory are copied, not the directory itself. The ADD command automatically decompresses the copied compressed folder, which is the biggest difference from the COPY command.

2.8、 COPY

Note: COPY can not automatically decompress, also can not COPY network files. Other functions are the same as ADD.

2.9、 ENTRYPOINT

Note: This command is the same as CMD command, the only difference is that it cannot be overwritten by the execution command of docker run. If you want to overwrite the command, you need to bring the option –entrypoint. If there are multiple options, only the last one will take effect.

2.10、 VOLUME

Create a mount on the host and mount it to the specified path of the container. The docker run -v command can also do this, but more powerful. This command does not specify the host folder path to mount to the container. But Docker Run-v can, and it can also mount data containers.

2.11, the USER

Usage: USER daemon

Note: Specify the user name or UID for running the container. The subsequent RUN, CMD, and ENTRYPOINT commands will also use the specified user to RUN commands.

2.12、 WORKDIR

Usage: WORKDIR path

Description: Configure working directories for RUN, CMD, and ENTRYPOINT directives. Multiple WORKDIR directives can be used, and subsequent arguments, if relative paths, are based on the path specified by the previous command. Example: WORKDIR /home WORKDIR test. The final path is /home/test. HOME=/ HOME; WORKDIR $HOME/test; / HOME/test

2.13、 ONBUILD

This command is used to configure the currently created image as the base image of other newly created images. This means that the ONBUILD command for this image will be executed first if other images are based on it.

Each instruction creates a new layer on the mirror, and the prefix of each instruction must be uppercase.

Now let’s build a mirror image of our project.

1. Prepare the SpringBoot JAR package for deployment

2, write Dockerfile file

# Java :8 is using the JDK version
FROM java:8

The temporary file directory is/TMP
VOLUME /tmp

Add jar to container and rename itADD the demo - 0.0.1 - the SNAPSHOT. Jar/app. The jarExecute the jar file
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]
Copy the code

3, use the docker build command to build the image, note that the last space is a dot

docker build -t springboot1 .
Copy the code

Check the local mirror repository at this point to see the image we just built

Run the Docker container

docker run -d -p 8080:80 springboot1
Copy the code

-d: background running

-p: 8080 refers to the port exposed outside the Doucker, 80 refers to the port of the project inside the Docker

You can see that the container is already running in the background

5. Project visit

At this point, the project deployment is complete, in the browser use: server IP (note docker VM IP, mentioned above) :8080 can access the Docker container deployment of springBoot project.