Docker packaging front-end and back-end project image deployment documents

illustrate

This time, 6 ports are used to do a front-end and back-end docker image deployment

8080,8081 are the ports of the two backend services
8082,8083 are the ports for the two front ends

The project uses nginx as a proxy to solve the cross-domain problem caused by different front-end and back-end ports, because I use both the front-end and back-end ip addresses here.

80 Proxy to front-end admin interface
8084 Proxy to app mobile store interface

The 4 services are deployed using the docker packaged image, and the front end is to add the nginx packaged docker image

The host also needs an nginx, which is the last to proxy the services above

local preparation

The admin_java_project service prepares a Dockerfile

The dockerfile can be placed anywhere, and it will be uploaded to the server later. The content is as follows:
"--spring.profiles.active=prod" is a parameter to set the yml configuration file, change the environment to the production environment

FROM java:8
MAINTAINER docker_maven docker_maven@email.com
ADD admin_java_project.jar app.jar
EXPOSE 8080
#RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENTRYPOINT ["java", "-jar","/app.jar","--spring.profiles.active=prod"]

The last parameter in ENTRYPOINT [] is to change the operating environment to prod

The app_java_project service also prepares a Dockerfile

The dockerfile can be placed anywhere, and it will be uploaded to the server later. The content is as follows:

FROM java:8
MAINTAINER docker_maven docker_maven@email.com
ADD app_java_project.jar app.jar
EXPOSE 8081
#RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENTRYPOINT ["java", "-jar","/app.jar","--spring.profiles.active=prod"]

Two background services package jar packages locally

front-end app

Create a new Dockerfile and nginx.conf file in the root directory

The contents of the nginx.conf file are as follows:

The following app_h5 is the name of the custom file directory

server {
        listen       8082;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html/app_h5;
            index  index.html index.htm;
            # try_files $uri $uri/ index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

The contents of the Dockerfile are as follows

# Set the base image, if the image does not exist locally, the image will be pull ed from the Docker.io server
FROM nginx

ADD ./h5  /usr/share/nginx/html/app_h5

COPY nginx.conf /etc/nginx/conf.d/

EXPOSE 8082

# CMD /usr/share/nginx/sense/env.sh

CMD ["/bin/bash", "-c", "envsubst < /etc/nginx/conf.d/nginx.conf > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"]

Packaged with HBuilder,

Location after packaging: the unpackage directory will be generated in the project

Front-end Web

Create a new Dockerfile and default.conf file in the root directory

The content of the default.conf file is as follows:

server {
    listen       8083;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html/;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}


The contents of the Dockerfile are as follows

# base image
FROM nginx
MAINTAINER cw
# Delete the original default.conf file
RUN rm /etc/nginx/conf.d/default.conf
# Add a custom default.conf file to the corresponding directory
ADD default.conf /etc/nginx/conf.d/
# Copy the files in the dist directory to the directory in nginx, corresponding to the above
COPY dist/ /usr/share/nginx/html/

Run the command npm run build to package

Server packaging docker image articles

java backend

Create a new project file directory in the outermost layer of the server

Create app_project, java_project, web_project respectively

Create two new folders, admin and app, in the java_project file

Upload the jar package and Dockerfile file of the admin service, as well as the local image.

Note: Execute the docker packaging command, the following admin_java_project customization is the name of the imags image generated later, and the latter 1103 is also customized. Currently, it is represented by a month plus a date as a version

#Enter the admin directory
cd /project/java_project/admin

# Execute the package docker image command
docker build -t admin_java_project:1103 .
docker images command view docker mirror

The same is true for the app, but there is no need to upload the image file here.

#Enter the admin directory
cd /project/java_project/app

# Execute the package docker image command
docker build -t app_java_project:1103 .
 docker images  View mirror

app h5 front end

Go to the /project/app_project directory

Upload the h5 folder under unpackage\dist\build\ and the previously built Dockerfile and nginx.conf files

Execute the following command to package:

# Enter the app front-end file directory
cd /project/app_project

# Execute the package docker image command
docker build -t app_h5:1103 .
# Also execute the command to see if the docker image is successful
docker images 

Web front end

Go to the /project/web_project directory

Upload the previously packaged dist file, and the previously built Dockerfile and default.conf files

Execute the following command to package:

# Enter the app front-end file directory
cd /project/web_project

# Execute the package docker image command
docker build -t web_pc:1103 .
# Also execute the command to see if the docker image is successful
docker images 

Finally execute the docker run command to run one by one

# The back-end admin, where the file and picture directory is mounted
docker run -it -d -p 8080:8080 --name=admin_java_project -v /project/java_project/admin/image:/project/java_project/admin/image admin_java_project:1103

# Backend app (frot) service
docker run -it -d -p 8081:8081 --name=app_java_project app_java_project:1103

# front end web
docker run -it -d -p 8083:8083 --name=app_h5 app_h5:1103

# front end app
docker run -it -d -p 8082:8082 --name=web_pc web_pc:1103

After the execution is complete, you can run docker ps to view the running status of the container

docker ps

Nginx proxy, nginx is installed in /usr/local/nginx directory

//create a folder

cd /usr/local
mkdir nginx
cd nginx

//download the tar package

#download
wget http://nginx.org/download/nginx-1.13.7.tar.gz

#decompress
tar -xvf nginx-1.13.7.tar.gz

3. Install nginx

//Enter the nginx directory
cd /usr/local/nginx
//enter the directory
cd nginx-1.13.7
//Execute the command to take into account the subsequent installation of the ssl certificate to add two modules
./configure --with-http_stub_status_module --with-http_ssl_module
//Execute the make command
make
//Execute the make install command
make install

4. Start the nginx service

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

4. Configure nginx.conf

Open configuration file

vi /usr/local/nginx/conf/nginx.conf

The configuration file is as follows nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    # enable gzip compression
	gzip on;
	# Do not compress the critical value, and only compress those larger than 1K, generally do not need to change
	gzip_min_length 1k;
	# compressed buffer
	gzip_buffers 16 64K;
	# Compressed version (default 1.1, if the front end is squid2.5, please use 1.0)
	gzip_http_version 1.1;
	# Compression level, 1-10, the higher the number, the better the compression and the longer the time
	gzip_comp_level 5;
	# Compressed file type
	gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
	# It is related to caching services such as Squid. If it is on, "Vary: Accept-Encoding" will be added to the Header.
	gzip_vary on;
	# IE6 is not very friendly to Gzip, don't give it Gzip anymore
	gzip_disable "MSIE [1-6]\.";

# web proxy
    server{
       listen 80;
       server_name ip or domain name;
 
       location / {
          proxy_pass http:// ip address: 8083;
          index  index.html index.htm;
       }

       location /appApi/ {
           proxy_pass http:// ip address: 8081/;
           #index  index.html index.htm;
       }

       location /webApi/ {
           proxy_pass http:// ip address: 8080/;
           #index  index.html index.htm;
       }


    }    

    # app proxy
    server{
       listen 8084;
       server_name  ip address;

       location / {
          proxy_pass http:// ip address: 8082/;
          #try_files $uri $uri/ /index.html;
          index  index.html index.htm;
       }
 
       location /appApi/ {
           proxy_pass http:// ip address: 8081/;
           #index  index.html index.htm;
       }

       location /webApi/ {
           proxy_pass http:// ip address: 8080/;
           #index  index.html index.htm;
       }
       
     
    }

    server {
        listen       8088;
        server_name  ip address;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        
    }



    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

5. Restart nginx

/usr/local/nginx/sbin/nginx -s reload

Check whether the nginx process is started:

ps -ef | grep nginx

Then access the ip: port to see the nginx interface.

Installation is complete general common commands

Enter the installation directory,

Order: cd /usr/local/nginx/sbin

startup, shutdown, restart, command:

./nginx start up

./nginx -s stop closure

./nginx -s reload reboot

Tags: Java Docker Nginx Container

Posted by knight on Sat, 05 Nov 2022 03:38:32 +0300