X. Preamble
The most popular deployment method in the era of cloud services is container deployment, which is also the most recommended deployment method.
Without going into too much detail about the GoFrame framework, let's go straight to initializing a demo for backup.
// Initialize a project: gf-demo
gf init gf-demo -u
// The -u parameter updates the existing goframe framework to the latest version.
// Go to the project folder and run
cd gf-demo && gf run
// Check out the service by typing one of the following addresses
http://127.0.0.1:8000/swagger
http://127.0.0.1:8000/
I. About Dockerfile files
1.1 Dockerfile file contents
After initializing the gf project, a Dockerfile is automatically generated under the path gf-demo/manifest/docker.
The contents and details of the document are set out below:
# This line specifies that the base image is loads/alpine:3.8, i.e. Alpine Linux version 3.8.
FROM loads/alpine:3.8
###############################################################################
# INSTALLATION
###############################################################################
# Set the environment variable WORKDIR to /app, which will be used as the working directory for subsequent commands, and is also easy to re-reference
ENV WORKDIR /app
# Copy a file or folder named resource to the working directory /app.
# resource is usually used to store static resource files, such as images, CSS stylesheets, JavaScript scripts, etc.
# These resource files can be accessed and used directly by applications.
ADD resource $WORKDIR/
# Copy the temp/linux_amd64/main file from the current directory to the main file in the working directory /app.
# This file is usually a compiled binary executable file for running on the Linux operating system.
# This file contains all the code and library dependencies of the program and can be executed directly without additional source code or dependencies.
ADD . /temp/linux_amd64/main $WORKDIR/main
# Modify the main file in the working directory /app to make it executable.
# chmod is a command used to change the permissions of a file or directory.
# +x is an option to the chmod command that adds executable permissions to a file, which means that users, groups, and other users can execute the file
RUN chmod +x $WORKDIR/main
###############################################################################
# START
###############################################################################
# Set the working directory to the value of the previously defined WORKDIR environment variable, i.e. /app
WORKDIR $WORKDIR
# Set the default command to run the main file in the working directory when the container is started.
CMD . /main
Note: With regard toSyntax Parsing of Dockerfile FilesSee previous posts for details:/hnzhengfy/p/18334471/Docker_Harbor#_label1_1
For Golang, we recommend using the alpine base system image, which produces a container image of about 20MB.
1.2 About the base image loads/alpine:3.8
Its role is to provide a lightweight, secure and easy-to-maintain base environment when building Docker images.
This base image is recommended for users in China.The Dockerfile address of the base image:/gqcn/dockerfiles , warehouse address:/u/loads。
Alpine Linux is a lightweight Linux distribution based on musl libc and busybox that aims to provide a small and simple operating system for containerized deployments.Alpine Linux features a very small and simple package management system, apk, which contains only the necessary packages, thus reducing system complexity and potential security risks.
Lightweight:Alpine Linux images are typically smaller than traditional Linux distributions such as Ubuntu or CentOS, which makes image downloads and deployments faster and take up fewer resources.
Security:Since Alpine Linux contains only the necessary packages, potential security vulnerabilities and attack surfaces are reduced. In addition, Alpine Linux is regularly updated to fix known security vulnerabilities.
Easy to maintain:The package management tool apk for Alpine Linux is very simple to use and makes it easy to install, upgrade and manage packages. This makes it easier to manage dependencies and install software in Docker images.
Compatibility:Alpine Linux supports a variety of architectures, including x86, ARM, etc., which allows it to run on different hardware platforms.
II. Automated Uploading of Harbor Scripts
Automation script files with .sh extension, e.g., can be executed by double clicking the mouse. The execution folder should be in the root directory of the project, i.e. at the same level.
The following code scripts the entire process from packing to uploading:
Note: To generate an image on Windows, you need theInstalling Docker , the installation process is detailed in the blogger's previous post:/hnzhengfy/p/18334471/Docker_Harbor#_label2_0
# read is a shell command that reads user input.
# -p is an option that identifies a prompt to output the subsequent "version:" directly to the command line.
# -p is an option that identifies the prompt, followed by "version:", which is output directly to the command line. The last version identifies a variable into which user input is stored
read -p "version:" version
# echo is a common shell command used to output text or variable values on the terminal.
# echo is a common shell command used to output text or variable values on the terminal, such as the following string of text, with $version representing the value of the version number entered in the previous step
echo "build start, version:$version"
echo "1) gf build running..."
# Use the gf utility to build a Go program named main, generate a Linux executable for the amd64 architecture, and store the result in the temp directory.
# -n main: Specify the Go source file to be compiled.
# -n main: Specify the name of the generated executable as main.
# -a amd64: Specifies that the target architecture is amd64 (i.e., 64-bit Intel architecture).
# -s linux: Specifies that the target operating system is Linux.
# -p . /temp: Specify that the output directory is the temp folder in the current directory.
gf build -n main -a amd64 -s linux -p . /temp
echo "2) docker build running..."
# Use Dockerfile to build an image called test/test-demo with a user-entered version number as the label
# [-t test/test-demo:"$version"] Specify the name and label of the image to be created.
# [-f . /manifest/docker/Dockerfile] Specify the path to the Dockerfile file used to build the image.
# 【.】 Specifies the path to the Docker build context. A dot indicates that the current directory is used as the build context
docker build -t test/test-demo:"$version" -f . /manifest/docker/Dockerfile .
echo "3) docker tag running..."
# Add a new tag to the image you just built, assign it to the repository, and prepare it for push
docker tag test/test-demo:"$version" /test/test-demo:"$version"
echo "4) docker pushing..."
# Push the image with the new tag to the remote repository
docker push /test/test-demo:"$version"
echo "done!"
# Exit the script, returning a status code of 0 to indicate that execution is complete. This value can be captured by the parent process of the calling script to determine whether the script execution was successful or not
exit 0
Enter the version number:
Execute subsequent scripts:
After the script finishes executing, check the Harbor remote repository to see if the upload was successful: