1. What is docker

Docker is a virtualization container technology designed around the concept of “Build once, run anywhere”. Does that sound like Java’s” Wirte once, run anywhere”? So what is virtualization container technology? Docker packages the application and its dependencies in a single file. Running this file generates a virtual container in which the program runs as if it were running on a real physical machine. With Docker you don’t have to worry about the environment. Just like the logo of docker in the first picture at the beginning, a Docker is the whale, and the containers carried on it are mutually isolated containers. You can add containers at any time without affecting the contents of the original container.

2. The difference between Docker and virtual machine

The difference between Docker and traditional virtualization mode is that the traditional virtual machine technology is to virtual a set of hardware, run a complete operating system on it, and then run the required application process on this system. The application processes in the container run directly on the host kernel. The container does not have its own kernel, but shares the host kernel, and does not carry out hardware virtualization. Therefore, containers are much lighter than traditional virtual machines.

3. Install the docker

Docker can at all operating system installation, process is very simple, the installation tutorial described here: www.runoob.com/docker/wind… Docker is a hassle to install on Windows, but I don’t want to install it on Linux in the VM because I’m too lazy to open it, so I install it on Windows anyway. There are two ways to install it on Windows:

  • Docker Toolbox: This installation package works with all Windows versions (Windows XP though I didn’t mention it), including Windows 7, Windows 8, and Windows 10 Home. This is the version I installed
  • Docker for Windows: this version is suitable for Win10 professional edition, because Win10 professional edition with hyper-V virtualization function, need to open the function before installation, see the specific installation link above.

3.1. Problems encountered in installing Docker Toolbox

This is the problem when installing Docker Toolbox and getting stuck in downloading boot2Docker. iso. This section of installing other versions of Docker can be ignored. After docker Toolbox is installed, there will be the following three ICONS:

Running pre-create checks...
(default) No default Boot2Docker ISO found locally, downloading the latest release...
(default) Latest release forGithub.com/boot2docker/boot2docker is v1.12.1 (default) Downloading C: \ Users \ huang \ machine \ cache \ boot2docker iso the from https://github.com/boot2docker/boot2docker/releases/download/v1.12.1/boot2docker.iso...Copy the code

The solution is: use a browser (or thunderbolt) to boot2Docker home page to download the latest version of boot2Docker.iso (github.com/boot2docker…). , as shown below:

C:\Users\huang\machine\cache\

3.2. boot2docker

Boot2docker is a lightweight Linux system for running docker containers. It runs completely in memory, only about 45m in size, and starts quickly. Therefore, the principle of running Docker in Windows is to start a Linux VIRTUAL machine with Docker installed. After clicking Docker Quickstart, we can see the output of the console. The virtual host name is default. The default IP address is 192.168.99.100. As can be seen from github’s introduction, the VM has a Docker user and the password is Tcuser. Therefore, we can SSH into the machine from secureCRT or git bash and use the docker command on it. Of course, you can install tools such as vim on the Default VM, but since it is an in-memory operating system, these newly installed tools will disappear after the vm restarts.

SSH [email protected]# your password
tcuser
Copy the code

3.3 docker Ali Cloud image acceleration

This section can temporarily, waiting for you to feel docker download is slow, you can read my another article, configuration image acceleration, the address is: www.jianshu.com/p/aff1a4a47…

4. The docker architecture

Here, I would like to start with the architecture diagram of Docker and several important concepts, and finally talk about specific commands, so that everyone can know the meaning and execution process of each command. After reading this article, I recommend a short Docker tutorial, linked to resources at the bottom of the article.

  • Client: Docker Client, the most common Client is docker commands, such as Docker build, Docker pull, Docker run, etc. Through these commands, we can easily build and run containers on host.
  • Daemon: Docker Deamon is a server component that runs as a Linux background service. Docker Daemon runs on docker host and is responsible for creating, running and monitoring containers, building and storing images.
  • Image: an image that can be viewed as a read-only template from which docker containers can be created. Just like a.class file in Java, you can create an instance of a class from.class. The image is layered, the bottom layer is the Linux kernel, every file or operation will be added on top of the original layer, such as the JDK environment on top of the Linux kernel, Tomcat on top of the JDK environment, at this time if you have JDK image in docker, Just download the Tomcat layer. The mirror is read-only and shared.
  • A container is a running instance of a Docker image.
  • Registry: Registry is the repository where docker images are stored. We can push our images to remote locations, or download images from remote locations to run locally, just like Github. Docker has an official Docker Hub, from which we can download the official image. For example, you can download an official Tomcat image and run the image to start Tomcat without any configuration.

In summary, combined with the above figure, the whole Docker architecture interaction process is as follows: Start docker, docker runs on Linux host in the way of daemon process (boot2Docker virtual Linux under Docker Toolbox), we send docker command to Docker daemon to execute, Docker can pull the required image from the Docker Hub and create a Container based on the image.

5. Actual combat

Knowing the interaction flow of docker architecture above, I take running a Tomcat container as an example to introduce common commands step by step:

  • docker imagesDocker: lists the current docker images, if this is the first time executed, of course there are no images.
  • docker search tomcat: Searches for tomcat images in the Docker Hub. Of course you can access docker hub in your browser at :(hub.docker.com/), just like accessing Maven’s remote repository, to search for what you need.
  • docker pull tomcat: Pull the latest version of tomcat image to the local, after the download is complete, use Docker images to see the downloaded image.
  • docker run --name mytomcat -p 8080:8080 -d tomcat: –name specifies the name of the instance. -p 8080:8080 specifies the name of the instance. -p 8080:8080 specifies the name of the instance. To access the Tomcat port, use localhost:8080(Docker Toolbox is 192.168.99.100:8080). -d indicates background running.
  • docker run --name mytomcat03 -p 8082:8080 -it tomcat /bin/bashAfter entering the container, run the /bin/bash command. After entering the container, run the exit command, and the container will exit. At this time, docker ps -a command will display the exit state.
  • ctrl+p+qIf you run the preceding command in the background, the container will exit after executing exit. If you do not want the container to exit, press this set of shortcut keys.
  • docker ps -a: Displays the status of all containers. Docker ps only displays the running containers. -A means to display all containers, including the exiting containers. You can now see tomcat, which has just started.
  • docker logs -f mytomcat: Displays logs generated when Tomcat is started. -f indicates tracing the printing of rolling logs.
  • docker exec -it mytomcat /bin/bash: exec indicates that the command is executed, and the command must be executed at the end of the command, for example, /bin/bash. It indicates that a terminal is returned after being started in interactive or attaching mode. /bin/bash indicates the command to be executed after entering the container. After this command is executed, you log into the container, which is the Tomcat application. Just like using SSH to log in inside the container. At this point, you can modify the configuration file inside the container. This is not recommended in production environments, and the configuration file is mounted externally.
  • docker stop mytomcat: Stops the myTomcat instance without deleting things in the container.
  • docker start -i mytomcat: Start a stopped instance of MyTomcat. If you have modified the configuration file in the container before starting, restarting the container will reload the configuration. If the configuration file is at the Linux host level, such as /etc/profile, it will not be loaded because the container shares the configuration of the host. -i Indicates that the command interface is started and displayed.
  • docker rm mytomcat: Delete the stopped myTomcat instance
  • docker run --name mytomcat02 -p8081:8080 -e JAVA_HOME=/path/to/java/home -v /host/path/file:/container/path/file -d tomcat-e sets the environment variable JAVA_HOME. Go to the container and run echo $JAVA_HOME to view the value of the environment variable. -v mounts /host/path/file of the host to /container/path/file in the container. When files on the host are modified, the files in the container are also modified. If docker Toolbox is used, the file mounted on the host machine should be in the current user directory, that is, C :\user\yourname. Otherwise, no error will be reported but the mount cannot succeed. I was fooled by this problem for a whole afternoon.
  • docker cp /host/path/file mytomcat02:/container/path/file: Copy the host configuration file to the container. If the container configuration file fails to be started due to an error, copy the configuration file to the host to modify the configuration and then copy the configuration file back to the container.
  • docker inspect mytomcat: Displays container information, including the container IP address.

6. Build a Docker image

Suppose we download the Tomcat image and start the container instance, and modify the configuration file in the Tomcat container. In this case, we can also package the configuration file as an image and send it to another machine for direct execution, instead of configuring each machine separately. Docker provides two ways to build an image, as follows:

  • Docker Commit: Build by container
  • Docker build: Builds from a dockerfile file

Dockerfile file construction need to write dockerfile, also only a few commands, is not difficult, please consult information. Here is the docker commit command. Docker commit mytomcat Huangzb /tomcat: Commit the current mytomcat container as an image named huangzb/tomcat

7. Network structure of containers

It is important to be familiar with the docker network structure so that we can do clustering. Without further ado, let’s cut to the chase. Docker network uses bridge and VETH (Virtual Ethernet) virtual devices in Linux network.

  • A common network bridge has only two interfaces. It connects two networks and works at layer 2 (data link layer). It can forward packets as long as there is a MAC address. Linux’s virtual Bridge has multiple interfaces and can be configured with IP, more like a router. After docker is installed, use the ifconfig command in the host, and you can see that docker0 network appears in the network configuration. Docker0 is a Linux virtual bridge for Docker Settings.
  • Veth: Virtual Ethernet is a virtual network device in Linux. It is like a cable with network cards at both ends. You can connect two devices to configure IP addresses respectively.

    Therefore, the network in my Docker Toolbox under Windows is shown below.

7.1. Interconnection between containers

By default, docker new containers are connected by docker0 bridge, all the default, docker created all containers are in a network, can access each other.

The above. Those of you who are interested can study the resources below.

The resources

  1. Docker tutorial: Yeasy.gitbooks. IO /docker_prac…
  2. Geek college video tutorial: www.bilibili.com/video/av178…
  3. Docker Bridge: www.ibm.com/developerwo…
  4. Docker network depth study: www.jianshu.com/p/19d6f55b9…