I. Introduction to Docker

E-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine. Docker is an open source engine that makes it easy to create a lightweight, portable, self-contained container for any application. Containers that developers compile and test on laptops can be deployed in a production environment, including VMs (virtual machines), Bare Metal, OpenStack clusters, and other basic application platforms. Docker is usually used in the following scenarios:

  • Automated packaging and publishing of Web applications;
  • Automated testing and continuous integration, release;
  • Deploy and adjust databases or other backend 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.

The above text refers to relevant articles; In addition, see related tutorials for the installation and basic use of Docker.

Second, preparation

Environmental conditions:

  • Linux, Windows is not recommended
  • Latest version of Docker
  • JDK 1.8
  • maven3.0

The project adopted in this paper is from the project described in the first article. Maven is used to build the project and Docker-Maven-plugin is used to build the Docker image.

Three, transformation project, construction mirror image

Transformation of Eureka-Server project

Add plugins to POM files:

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> <! -- tag::plugin[] --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> The < version > 0.4.3 < / version > < configuration > < imageName >${docker.image.prefix}/${project.artifactId}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <! -- end::plugin[] --> </plugins> </build>Copy the code

Spotify docker-Maven-plugin uses maven plugin to build docker image.

  • ImageName specifies the name of the image. In this example, it is forep/ Eureka-server
  • DockerDirectory specifies the location of a Dockerfile
  • Resources are files that need to be included with dockerFiles to be used when building the image. Generally, application JARS need to be included.

Modify the configuration file:

server:
  port: 8761
eureka:
  instance:
    prefer-ip-address: true
  client:
    registerWithEureka: false
    fetchRegistry: false<br>Copy the code

Dockerfile:

FROM Frolvlad /alpine- Oraclejdk8: Slim VOLUME/TMP ADD Eureka-server-0.0.1 - snapshot.jar app.jar#RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]
EXPOSE 8761Copy the code

Docker file:

  • FROM
Docker file writing instruction: FROMCopy the code

The FROM directive must be specified and must precede other directives in the Dockerfile. The underlying image specified can be in an official remote repository or in a local repository. Subsequent directives depend on the image specified by the directive. Multiple FROM directives can be used when creating multiple images in the same Dockerfile.

  • VOLUME

Format for:

VOLUME ["/data"]Copy the code

Enables persistent storage of data in a directory that can be used by the container itself or shared with other containers. This directive can be used in a Dockerfile when the application in the container needs to persist data.

  • ADD

Copy files from the SRC directory to the container’s dest. SRC can be a relative path to the directory where the Dockerfile resides, a URL, or a compressed package

  • ENTRYPOINT

Specifies the command to execute when the Docker container is started. It can be set multiple times, but only the last one is valid.

  • EXPOSE

Set the external port number for the Docker container. At startup, either the -p option or the -p option can be used.

Build the mirror

Build docker image maven

mvn clean
mvn package docker:buildCopy the code

The Eureka-server image was successfully created. Procedure

Build the service- HI image in the same way

  • Import POM files in the same way as eurek-server
  • Modify the configuration file:
eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka-server:8761/eureka/ This needs to be changed to eureka-server
server:
  port: 8763
spring:
  application:
    name: service-hiCopy the code

E-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine