A recent project needs to use Docker to deploy operation and maintenance projects. Because I only used it briefly before, although I set up an environment on the server, I have never really used it. This is a good time to practice my skills in advance and get familiar with the whole process of Docker deployment service. The whole! (HERE I use a eureka server as an example.)

1. Construction of docker environment

First check the docker version on the server

docker verion
Copy the code

Since my version is relatively low, I choose upgrade, which requires deleting the previous version (since my Docker is not started, it is good to delete it directly. If there is a container that is running, it needs to stop first, then close the Docker, and then delete it).

yum remove docker  docker-common docker-selinux docker-engine
Copy the code

Yum-utils provides yum-config-manager, and the device Mapper storage driver requires device-mapper-persistent-data and LVM2

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

Use the official source address

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Copy the code

Timeout failed… The source ADDRESS of the Ari cloud is specified

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

View the docker versions that can be installed in the repository when you are done

Yum list docker - ce - showduplicates | sort - r docker - ce. X86_64 3:18. 09.2-3. El7 docker - ce - stable docker - ce. X86_64 X86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce. X86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce X86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce. X86_64 18.06.1.ce-3.el7 docker-ce-stable El7 docker-ce-stable docker-ce. X86_64 18.03.1. Ce-1.el7. Centos docker-CE -stableCopy the code

There are a number of versions, you can follow a particular version, but in order to upgrade, of course, we follow the latest version

Sudo yum install docker-ce or sudo yum install docker-ce-18.06.0.ce-3.el7Copy the code

Once installed, you can start Docker

2. Start docker

First, set the Docker to boot

systemctl enable docker
Copy the code

Direct start

systemctl start docker
Copy the code

Check the status

Systemctl status docker ● docker. Service - docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; Vendor PRESET: Disabled) Active: Active (running) since a 2020-07-20 16:56:25 CST; 2 days ago Docs: https://docs.docker.com Main PID: 10992 (dockerd) Tasks: 12 Memory: 133.9m CGroup: / system. Slice/docker. Service └ ─ 10992 / usr/bin/dockerd -h fd: / / -- containerd = / run/containerd containerd. The sock on July 20 16:56:25 orange dockerd[10992]: Time ="2020-07-20T16:56:25.420273881+08:00" level=info MSG ="Docker daemon" commit=48a66213fe GraphDriver (s)=overlay Version =19.03.12 7月 20 16:56:25 Orange Dockerd [10992]: Time =" 2020-07-20T16:56:25.42020374250 +08:00" level=info MSG ="Daemon has completed initialization" 7月 20 16:56:25 orange dockerd[10992]: Time ="2020-07-20T16:56:25.450725623+08:00" level=info MSG ="API listen on /var/run/docker.sock" 7月 20 16:56:25 orange systemd[1]: Started Docker Application Container Engine.Copy the code

Check the version

Docker version Version: 19.03.12 API version: 1.40 Go version: go1.13.10 Git commit: 48a66213fe Built: Mon Jun 22 15:46:54 2020 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.12 API version: 1.40 (minimum version 1.12) Go version: go1.13.10 Git commit: 48a66213fe Built: Mon Jun 22 15:45:28 2020 OS/Arch: Linux/AMd64 Experimental: false containerd: Version: 1.2.13 7 ad184331fa3e55e52b890ea95e65ba581ae3429 runc: Version: 1.0.0 - rc10 GitCommit: Dc9208a3303feef5b3839f4323d9beb36df0a9dd docker - init: Version: 0.18.0 GitCommit: fec3683Copy the code

Now that the Docker installation and startup is complete, you can deploy your Own Spring Boot service

3. Deploy the service

You need to package your SpringBoot project, directly use Maven install in idea, and then upload the jar package to the server

Then you need to write a DockerFile file

Vim dockerfile FROM Java :8 MAINTAINER orange VOLUME/TMP ADD eureka-1.0.0- snapshot.jar app.jar RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]Copy the code
  • The base image used for FROM is Java 8, which means the runtime environment is JDK1.8
  • MAINTAINER MAINTAINER information
  • VOLUME is used to specify the persistence directory. Because the built-in Tomcat container used by Spring Boot uses/TMP as the working directory by default, it is used as the persistence directory
  • ADD adds the local file to the container and renames it
  • Here is the jar package running our project
  • ENTRYPOINT configuration container The ENTRYPOINT configuration container is configured for tomcat to obtain random numbers. If the default method is used, the response may be slow

After editing the Dockerfile, you can start creating the image

Place the Dockerfile in the same directory as the jar uploaded in the previous step, and execute

docker build -t hope-server . ---> Running in b103f0dc0100 Removing intermediate container b103f0dc0100 ---> 27248c274b97 Step 3/6 : VOLUME /tmp ---> Running in 00ab20719506 Removing intermediate container 00ab20719506 ---> 8b03c7d7dc47 Step 4/6 : ADD eureka-1.0.0-snapshot.jar app.jar --> cb269880f278 Step 5/6: RUN bash -c 'touch /app.jar' ---> Running in 54f6087fa2a4 Removing intermediate container 54f6087fa2a4 ---> ffbd61fa294a  Step 6/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] ---> Running in 83c7b669fcbd Removing intermediate container 83c7b669fcbd ---> d5d28abe3ea4 Successfully built d5d28abe3ea4 Successfully tagged hope-server:latestCopy the code

If the final result shows Successfully, the image is Successfully created. You can run the command to view the image

docker images hope-server latest d5d28abe3ea4 56 seconds ago 769MB hyperledger/fabric-tools latest 0403fd1c72c7 3 years Ago 1.32GB Hyperledger/Fabric-Tools x86_64-1.0.0 0403FD1C72C7 3 years ago 1.32GB Hyperledger/Fabric-Couchdb Latest 2fbdbf3AB945 3 years ago 1.48GB Hyperledger/Fabric-Couchdb x86_64-1.0.0 2fbdbf3ab945 3 years ago 1.48GB Hyperledger /fabric-kafka latest DBd3f94DE4b5 3 years ago 1.3GB Hyperledger /fabric-kafka x86_64-1.0.0 dbd3F94de4b5 3 Years ago 1.3GB Hyperledger/Fabric-ZooKeeper Latest E545DBF1C6AF 3 years ago 1.31GB Hyperledger/Fabric-Zookeeper X86_64-1.0.0e545dbf1c6af 3 years ago 1.31GBCopy the code

You can clearly see that the image we made is in the first place. Once we have the image, the last step is to start the container

docker run -d -p 8888:5151 hope-server
633262fc5ee12480f2b3a333a523215cd2b6f4d8af29a884fd114d2c09213d62
Copy the code
  • The -d argument lets the container run in the background
  • -p is used for port mapping. In this case, port 8888 in the server is mapped to port 5151 in the container (the port configured in the project is 5151)

After the startup is complete, we can view the status and log of the service we started through the command

Docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 633262fC5EE1 hope-server "java-djava.securit..." About a minute ago Up About a minute 0.0.0.0:8888->5151/ TCP crazy_WEScoff # 2020-07-22-10 633262 tail fc5ee1 10:17:11. 027 INFO 1 - [Thread - 13] C.N.E.R.P eerAwareInstanceRegistryImpl: Got 1 instances from neighboring DS nodes 2020-07-22 10:17:11.028 INFO 1 - [Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1 the 2020-07-22 10:17:11. 1-028 INFO/Thread - 13 C.N.E.R.P eerAwareInstanceRegistryImpl: Changing the status to the UP the 2020-07-22 10:17:11. 043 INFO 1 - [Thread - 13] E.S.E urekaServerInitializerConfiguration: Started Eureka Server 2020-07-22 10:17:11. 077 INFO 1 - [the main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: Tomcat started on port(s): 5151 (HTTP) with the context path '2020-07-22 10:17:11. 078 INFO 1 - [the main] S.C.N.E.S.E urekaAutoServiceRegistration: Updating the port 5151 to the 2020-07-22 10:17:11. 081 INFO 1 - [the main] C.B.H ope. Eureka. EurekaApplication: Started EurekaApplication in 10.627 seconds (JVM running for 11.371) 2020-07-22 10:18:11.031 INFO 1 -- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the EVict Task with compensationTime 0ms 2020-07-22 10:19:11.030 INFO 1 -- [A-evictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the EVict Task with compensationTime 0ms 2020-07-22 10:20:11.030 INFO 1 -- [A-evictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0msCopy the code

4. Complete

With the service up and running, we just need to enter the server address in the browser to access our SpringBoot project

Use the ps command to view the container services currently running in docker

Docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 633262fC5EE1 hope-server "java-djava.securit..." 3 seconds ago Up 1 second 0.0.0.0:8888->5151/ TCP vigorous_goldbergCopy the code

If you want to stop the service, simply use stop and add the container ID

docker stop 633262fc5ee1
633262fc5ee1
Copy the code