This article mainly introduces, by writing Dockerfile file, the SpringBoot application is deployed to the Docker container, and external access; Dockerfile command: Dockerfile command: Dockerfile

Environment to prepare

  1. Java Web services need a Java environment to run, so we need to build a Java environment in Dockerfile, where we need to use the JDK installation package.

JDK8 download: www.oracle.com/java/techno…

  1. Java application, this program is just used to demonstrate the preparation of Dockerfile, so it is relatively simple, a simple SpringBoot program.

Dockerfile file preparation

  1. Write a Dockerfile file

    FROM centos:latest MAINTAINER [email protected] # COPY Java programs to the container directory RUN mkdir /opt/web COPY web_server.jar RUN mkdir /usr/local/ Java RUN the ADD command to decompress ADD jdk-8u311-linux-x64.tar.gz /usr/local/java RUN ln -s /usr/local/java/jdk1.8.0_311 /usr/local/java/jdk /usr/local/ Java/JDK ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar PATH=${JAVA_HOME}/bin:$PATH # EXPOSE 8080 # switch WORKDIR WORKDIR /opt/web/ ENTRYPOINT ["nohup","java","-jar","web_server.jar>webserver.log","&"]Copy the code
  2. Compile Dockerfile

    Run docker build -f Dockerfile -t webserver:v1.Copy the code

    The docker build directive is used to compile a Dockerfile file. By default, a Docker build will look for a Dockerfile file in the current context directory for compilation

    The -f parameter can be used to specify the Dockerfile file

    -t: Sets the name and label of the mirror

    Docker build -f Dockerfile -t webserver:v1 This represents the current build environment context. The resource files in the Dockerfile are found in the default directory of this context, and then copied to the container. You can also specify a directory.

    If you do not include “. “, or specify the wrong context directory, the following error 🙅 will occur during compilation

  3. View the compiled image

    Docker Image Ls or Docker imagesCopy the code

    Run the WebServer program

    1. At present, through the Dockerfile file to complete the construction of the running environment of the Webserver program, has not completed the deployment of the WebServer program.

      Docker ps -a looks at all containers and does not have a WebserverCopy the code

    2. Run the WebServer service

       docker run -d --name webserver webserver:v1
      Copy the code

      Access the WebServer service and return the same result as the local run.

      The curl http://127.0.0.1:8080/helloCopy the code