1.background
Current software development projects are separated from front-end, back-end and database, which
facilitates horizontal expansion and application service arrangement.
Service choreography often uses k8s as a management tool.
When deploying a separate back-end service to k8s, we fifirst need to create a docker image to ensure the
successful operation of the back-end service in the container, and build a database image to locally test
whether the back-end service can access the database.
2.pre-knowledge
- Use of docker command
- Writing of dockerfifile
3.Specifific implementation
This section takes the uui-server in the ONAP project as an example to build the backend service and
connect its Postgres database.
The project code can be obtained at the following link:
https://gerrit.onap.org/r/admin/repos/usecase-ui/server
uui-server is a single micro service in the project. In the previous version, the server and Postgres database
are in the same container, which is not safe for data. After the service is suspended, the database will also be
suspended, which is not conducive to data recovery and service restart.
The following steps are required:
- Modify dockerfifile and script to build image
- Conduct local tests
3.0 Directory structure of UUI server image fifileThe directory structure is shown in the fifigure above
assembly/bin contains the start and stop command.
Confifig places static confifiguration fifiles.
3.1Modify the application.properties parameter
Modify the application.properties with local connection only to read the environment variable value fifirst
spring.datasource.url=jdbc:postgresql://localhost:5432/uui
spring.datasource.username=uui
spring.datasource.password=uui
Modifified as:
spring.datasource.url=jdbc:postgresql://${POSTGRES_IP:127.0.0.1}:${POSTGRES_PORT:5432}/${PO
STGRES_DB_NAME:uui}
spring.datasource.username=${POSTGRES_USERNAME:uui}
spring.datasource.password=${POSTGRES_PASSWORD:uui}
This can not only keep the local environment unchanged, but also facilitate the injection of environment
variables to achieve external connection.
3.2 Write dockerfifile
FROM nexus3.onap.org:10001/onap/integration-java11:9.0.0
MAINTAINER "Lu Ji" <lu.ji3@zte.com.cn>
# Expose the Usecase-UI backend port
EXPOSE 8082
#Configure Java SDK
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
USER root
#Add Usecase-UI server related resources to the docker image
RUN mkdir /home/uui
WORKDIR /home/uui
ADD usecase-ui-server-*-linux64.tar.gz /home/uui/delete the command to start Postgres in run.sh
3.3 offlfflffline debugging
3.3.0 modify Dockerfifile
Please note that
ADD usecase-ui-server-*-linux64.tar.gz /home/uui/
command is for online Jenkins to build images automatically.
For the local test ,it needs to be modifified first
FROM nexus3.onap.org:10001/onap/integration-java11:9.0.0
MAINTAINER "Lu Ji" <lu.ji3@zte.com.cn>
# Expose the Usecase-UI backend port
EXPOSE 8082
#Configure Java SDK
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
USER root
#Add Usecase-UI server related resources to the docker image
RUN mkdir /home/uui
WORKDIR /home/uui
ADD . /home/uui
RUN addgroup -S uui && adduser -S uui -G uui && chown -R uui:uui /home/uuimodify run.sh
USER uui
WORKDIR /home/uui
ENTRYPOINT /home/uui/bin/run.sh
modify run.sh
main_path="/home/uui"
echo @main_path@ $main_path
JAVA_PATH="$JAVA_HOME/bin/java"
JAVA_OPTS="-Xms50m -Xmx128m"
echo @JAVA_PATH@ $JAVA_PATH
echo @JAVA_OPTS@ $JAVA_OPTS
jar_path="$main_path/usecase-ui-server-*.jar"
echo @jar_path@ $jar_path
echo "Starting usecase-ui-server..."
$JAVA_PATH $JAVA_OPTS -classpath $jar_path -jar $jar_path $SPRING_OPTS
3.3.1 maven package
click package in idea,or use maven command to get package.
we get usecase-ui-server-3.0.1-SNAPSHOT.jar .
move it in assembly dir.
3.3.2 image build
run command below in assembly dir.
docker build . -t uui-server:1
3.3.3 run postgres
Start a Postgres database in docker, and put the initialization script of the database in init In the SQL fifile.
docker run -itd --name uui-postgres -p 5432:5432 -e POSTGRES_USER=uui -e POSTGRES_DB=uui
-e POSTGRES_PASSWORD=uui -v ./init.sql:/docker-entrypoint-initdb.d/postgres.sql
postgres:14.4
-The V command is to set its own init The SQL is hung in the container, and the Postgres container will access
the SQL script in the / docker entrypoint initdb. D directory when it is started. This is designed by the Postgres
image itself and can be used directly.
It is possible that init.sql fails to be mounted. You need to debug whether it is successfully mounted.
My mount command under windows env is:
docker run -itd --name uui-postgres -p 5432:5432 -e POSTGRES_USER=uui -e POSTGRES_DB=uui
-e POSTGRES_PASSWORD=uui -v C:\Users\95652.000\Desktop\LFN\backend\assembly\sql:/docker-
entrypo
int-initdb.d/ postgres:14.4
then
docker inspect uui-postgres -f “{{json .NetworkSettings.Networks }}”
View IP address uui-postgres-ip
Because - P 5432:5432 mounts the port, you can also use the local IP directly
3.3.4 Run the built server image
docker run -itd -e POSTGRES_IP=UUI-POSTGRES-IP -e MSB_ADDR=0.0.0.0 -p 8082:8082 --name
uui-b3 uui-server:1
The uui-postgres-ip is replaced with the Postgres container IP or the local IP.
Only Postgres is added here_ IP, the default is OK for others.
View UUI server logs
docker logs uui-b3
You can see that there is no failure to connect to the database.
3.3.5 fifinally call the API port to query whether the database value
can be read
https://localhost:8082/api/usecaseui-server/v1//listSortMasters
4. error reporting
These problem happens in windows environment. if you use windows to make local test, these report may
helps.
4.1 run.sh not found,after running the image
It is because the format of the Linux system conflflicts with that of the windows system. The windows system
uses CRLF and the Linux fifile format uses LF . Use idea or vs code to modify the encoding type and rebuild the
image.
4.2 user uui cannot be found while docker is running
You need to add users in the dockerfifile
RUN addgroup -S uui && adduser -S uui -G uui && chown -R uui:uui /home/uui
4.3 Start the Postgres database under windows and fifind that the initialization
script is not running
The problem of absolute path and relative path. switch relative path to absolute path.