This is the 23rd day of my participation in the August More Text Challenge

Due to the introduction of containerization, using Docker to release projects is a necessary skill, the following will show you how to use Docker to release the front and back end projects respectively. This document uses the simplest configuration method as an example to ensure normal operation.

1. Publish the SpringBoot project

1. Compile a Dockerfile file

The following uses the Eureka project as an example, which can be modified according to the configuration.

# Docker image for springboot file run # VERSION 0.0.1 Eangulee # base image uses Java FROM Java :8 # VOLUME to specify the temporary file directory as/TMP. Create a temporary file in /var/lib/docker Jar = / TMP VOLUME/TMP # eureka-0.0.1- snapshot.jar = / TMP VOLUME/TMP # eureka-0.0.1- snapshot.jar = / TMP VOLUME/TMP # eureka-0.0.1- snapshot.jar = / TMP VOLUME/TMP # eureka-0.0.1- snapshot.jar = / TMP VOLUME/TMP # eureka-0.0.1- snapshot.jar = / TMP VOLUME/TMP # eureka-0.0.1- snapshot.jar = / TMP VOLUME/TMP # eureka-0.0.1- snapshot.jar RUN bash -c 'touch /eureka.jar' ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/eureka.jar"]Copy the code

Explain the configuration file:

VOLUME specifies the temporary file directory as/TMP. The effect is to create a temporary file in the host /var/lib/docker directory, linked to the container’s/TMP. This step is optional and necessary if file system applications are involved. The/TMP directory is used to persist to the Docker data folder Because the embedded Tomcat container used by Spring Boot defaults to using/TMP as the working directory project’s JAR file as “eureka.jar” added to the container’s ENTRYPOINT execution project app.jar. To shorten Tomcat startup time, add a system attribute pointing to “/dev/./urandom” as the Entropy Source. If it is packaged for the first time, it will automatically download the image of Java 8 as the base image, which will not be downloaded for future image making.

2. Deploy files

Create a docker folder on the server and copy the JAR packages and dockerfiles packed by Maven to the docker folder on the server

3. Create an image

CD /root/docker docker build -t eureka. // Note that eureka is the alias of the image !!!!Copy the code

Be sure to notice a space after the “. “!

ADD eureka-0.0.1- snapshot. jar app.jarlstat eureka-0.0.1- snapshot. jar: no such file or directory

The ADD demo-0.1-snapshot. jar /eureka.jar configuration problem, please check whether the Application startup file is specified.

4. View the mirror

docker images
Copy the code

As shown in the following figure, you can see that the docker image is already available locally.

5. Start the container

  • The first 8761: is the external port of the virtual machine you are operating on.
  • Second 8761: interface for the Docker container to the virtual machine.
  • –net=host: Use the local IP address as the container address.
  • Eureka: indicates the mirror name.
docker run -d -p 8761:8761 --net =host eureka 
Copy the code

Note that there is a pit where, if springboot Netflix is used, the IP address of the service will be pulled from Eureka when called between services. However, if –net =host is not added, the virtual address of the Docker will be pulled. When services are accessed, the virtual address will be accessed instead of the server address that needs to be accessed, so here the host uses the server address as the docker address. If there is a better way please contact the landlord.

Now you can go into the container and manipulate the container.

docker run -it eureka  /bin/bash
Copy the code