background

In the context of microservices, the appearance of Docker has reduced a lot of workload for environment deployment, operation and maintenance. As for things related to concepts, this example takes Centos7 system as the explanation:

A. Docker environment

1. Uninstall the Docker container

$ sudo yum remove docker \\
                  docker-client \\
                  docker-client-latest \\
                  docker-common \\
                  docker-latest \\
                  docker-latest-logrotate \\
                  docker-logrotate \\
                  docker-engine
Copy the code

2. Download the docker

1. Install the tool package

sudo yum install -y yum-utils \\
  device-mapper-persistent-data \\
  lvm2
Copy the code

2. Use Aliyun image

$ sudo yum-config-manager \\
    --add-repo \\
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
Copy the code

3. View the installable Docker version

yum list docker-ce --showduplicates | sort -r
Copy the code

4. Install the docker

I use 18.09.1 as an example

Sudo yum install docker-ce-18.09.1 containerd.ioCopy the code

5. Start the docker

$ sudo systemctl start docker
Copy the code

6. Check whether the startup is successful

$ sudo docker run hello-world
Copy the code

7. Uninstall docker

8. Delete installation package:

yum remove docker-ce
Copy the code

9. Delete images, containers, and configuration files.

rm -rf /var/lib/docker
Copy the code

2. Docker operation

1. Docker enables remote access

vim /lib/systemd/system/docker.service
Copy the code

[Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com BindsTo=containerd.service After=network-online.target firewalld.service Wants=network-online.target Requires=docker.socket [Service] Type=notify #  the default is not to use systemd for cgroups because the delegate issues still # exists and systemd currently does not Support the cgroup feature set required # for containers run by docker ExecStart=/usr/bin/dockerd -h TCP :// 0.0.0.00:2375  -H unix://var/run/docker.sock ExecReload=/bin/kill -s HUP $MAINPID TimeoutSec=0 RestartSec=2 Restart=always # Note that  StartLimit* options were moved from "Service" to "Unit" in systemd 229. # Both the old, and new location are accepted by systemd 229 and up, so using the old location # to make them work for either version of systemd. StartLimitBurst=3 # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230. # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make # this option work for either version of systemd. StartLimitInterval=60s # Having non-zero  Limit*s causes performance problems due to accounting overhead # in the kernel. We recommend using cgroups to do container-local accounting. LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity # Comment TasksMax if your systemd version does not supports it. # Only systemd 226 and above support this option. TasksMax=infinity # set delegate  yes so that systemd does not reset the cgroups of docker containers Delegate=yes # kill only the docker process, not all processes in the cgroup KillMode=process [Install] WantedBy=multi-user.targetCopy the code

2. Reload the configuration file

systemctl daemon-reload

3. Restart the Docker service

systemctl restart docker

4. Check whether the port is enabled

netstat -nlpt

5. Disable the firewall

systemctl stop firewalld.service

6. Prohibit the firewall from starting upon startup

systemctl disable firewalld.service

7. Curl up and see if it works

The curl http://127.0.0.1:2375/info

IDE configuration and project POM.xml dependency

1. Set the Docker connection address

2. Set the mirror proxy address

Note: Here you can apply for personal mirror proxy address, enter the address:https://dev.aliyun.com/

3. Add the docker dependent plug-in and configure it

Pom. The XML configuration

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.5.6 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo02</artifactId> <version>0.0.1-SNAPSHOT</version> <name> Demo02 </name> <description>Demo project for Spring Boot</description> < the properties > < Java version > 1.8 < / Java. Version > <! Guoxx </docker.image.prefix> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <! <imageName>${docker.image.prefix}/${project.artifactid}</imageName> <! <imageTags> <imageTag>latest</imageTag> </imageTags> <! Java </baseImage> <! -- Author information --> <maintainer>[email protected]</maintainer> <! - switch to the ROOT directory - > < workdir > / ROOT < / workdir > < CMD > [" Java ", "- version"] < / CMD > < entryPoint > [" Java ", "- the jar," "${project.build.finalName}.jar"]</entryPoint> <! -- DockerFile path --> <! -- <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>--> <! - specifies the remote address Docker API - > < dockerHost > http://192.168.111.128:2375 < / dockerHost > <! -- Copy Jar package to specified Docker container --> <resources> <resource> <targetPath>/ROOT</targetPath> <! <directory>${project.build.directory}</directory> <! <include>${project.build.finalname}. Jar </include> </resource> </resources> </configuration> <executions> <! -- Execute maven Clean Package when executing maven package: build--> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>Copy the code

4. Example code

@SpringBootApplication @RestController public class Demo02Application { public static void main(String[] args) { SpringApplication.run(Demo02Application.class, args); } @GetMapping("/index") public String index() { return "hello world"; }}Copy the code

5. Pack the image file

Run the MVN clean Package docker:build command to package the image file. The image file is automatically uploaded to the cloud service Docker container.

The image is compiled successfully and then returned to the cloud servicedocker imagesCheck whether the image is uploaded successfully

6. Run the image file in the docker container

Run the docker run –name test -d -p 8081:8081 GUoxx /demo02:latest command

Description:

--name test Set the name to test-d -p 8081:8081 Map the external port to the port in the container guoxx/ DEMO02 :latest Container name and versionCopy the code

7. View the running process of the Docker container

Run docker ps to see how the container is running

docker ps -aView the running status of the current container

Test 8.

Type http://192.168.111.128:8081/index in the address bar, returned to the helloworld said the deployment of normal

Four,

The above is the docker online deployment of some operations, other commands can come down their own operation, no more detailed examples!