How to make the latest version of Headless CMS STRAPI into a docker image and upload it to the synopsis for service.
In the past, it was popular to create cafes (bulletin boards) and blogs and provide services using CMS (content management systems) such as Zero Board and WordPress. Due to the development of the Frontend, the Headless CMS, which is currently serviced mainly by the backend, is injured and is at the center of the STRAPI.
# STRAPI
Simply put, STRAPI is a service that is only responsible for the backend part of CMS such as WordPress. Whether you develop the front end with react.js or vue.js, you can do it yourself, and the back end is to send and receive data with RESTful or GraphQL provided by STRAPI. The content thus obtained can be managed by STRAPI. Please refer to the link below for a detailed description.
STRAPI is responsible for the backend that is essential for front-end developers
Serving STRAPI V4 in the Synology Docker
There is a function to use the docker when viewed in the synopsis.
You can use this docker to service STRAPI. But there's one small problem. Until last year, STRAPI made a docker image and uploaded it to the docker hub, so it was easy to use, but from now on, they will not make a docker image. Therefore, you can only install STRAPI of the only version (STRAPI V3) that you want to use after receiving STRAPI from Registry (=Docker Hub) of Synology Docker.
So I decided to make a docker image and post it on the synopsis for service.
I share the information I learned while shoveling for three days to create a Docker image. For your information, I'm a beginner who doesn't know much about dockers. I didn't fully understand it because I solved it while Googleing, so please point it out if there is anything wrong.
Create Docker Image
First, we will proceed under the assumption that there is a docker installed. For Windows or Mac, you can install the docker desktop below.
Creating a Dockerfile
In order to create a Docker Image, you must first create a content on how to create it, and that is the Dockerfile file. Images are created in the order of what is written here.
########################
## Dockerfile file ##
########################
# # BASE OS
# Create the default OS for the image as what you want to do.
# It's hard to write here, but... A conclusion is
# Install the Alpine Linux OS with node.js 14 version as the default OS and set the port to 1337.
ARG NODE_VERSION=14
FROM node:${NODE_VERSION}-alpine AS base-alpine
EXPOSE 1337
FROM base-alpine
# Alpine Linux is the lightest Linux of all Linux features and is an OS that has been removed.
# So basically, it's convenient to install a minimum program to use.
# apk update : updates installed programs to the latest version
# apk add~~ :~~~~~~~~~~~~~~~~~~~~~~~~~>
RUN apk update && apk add build-base gcc autoconf automake zlib-dev libpng-dev nasm bash vips-dev
# ARG is a variable used only within a Dockerfile
# Create the STRAPI_VERSION to install. Latest means that we will install the latest version at this point.
ARG STRAPI_VERSION=latest
# First, install the specified version of STRAPI globally on alpine Linux.
# This eliminates the need to install STRAPI on the local computer.
RUN yarn global add @strapi/strapi@${STRAPI_VERSION}
# You will install the STRAPI source in the /srv/app directory.
# Create the /srv/app directory and grant permissions to the first user to free up the directory.
# Now this folder is mine.
RUN mkdir -p /srv/app && chown 1000:1000 -R /srv/app
# Set the Actions folder. Project files will now be stored and executed in this folder.
WORKDIR /srv/app
# Set the volume to send and receive data to and from the local computer. The contents of this folder will now be shared equally among local folders.
VOLUME /srv/app
# # docker-entrypoint.Copy docker-entrypoint.sh to /usr/local/bin/ on the docker to run sh.
# The contents written in this file will be executed and the project file will be created.
COPY docker-entrypoint.sh /usr/local/bin/
# # docker-entrypoint.Release sh's permissions.
# Grant permissions to prevent problems when creating files or folders.
RUN chmod 777 /usr/local/bin/docker-entrypoint.sh && ln -s /usr/local/bin/docker-entrypoint.sh
# Runs the docker-entrypoint.sh file that was moved the first time the container was run via the docker image.
ENTRYPOINT ["docker-entrypoint.sh"]
# The command is executed the first time the container is run via the docker image.
CMD ["strapi"]
The following docker-entrypoint.sh file will be executed by ENTRYPOINT: This file must also be located in the same space as the Dockerfile file.
#################################
## ## docker-entrypoint.sh 파일 ##
#################################
#!/bin/sh
set -ea
if [ "$*" = "strapi" ]; then
if [ ! -f "package.json" ]; then
DATABASE_CLIENT=${DATABASE_CLIENT:-sqlite}
EXTRA_ARGS=${EXTRA_ARGS}
The echo "Strapi $(strapi version) version is in use."
echo "There are currently no projects in the /srv/app folder. Create a new Strapi project..."
DOCKER=true strapi new . --no-run \
--dbclient=$DATABASE_CLIENT \
--dbhost=$DATABASE_HOST \
--dbport=$DATABASE_PORT \
--dbname=$DATABASE_NAME \
--dbusername=$DATABASE_USERNAME \
--dbpassword=$DATABASE_PASSWORD \
--dbssl=$DATABASE_SSL \
$EXTRA_ARGS
elif [ ! -d "node_modules" ] || [ ! "$(ls -qAL node_modules 2>/dev/null)" ]; then
if [ -f "yarn.lock" ]; then
echo "Node modules are not installed. Installing module using Yarn..."
yarn install --prod --silent
yarn build
else
echo "Node modules are not installed. Installing module using npm..."
npm install --only=prod --silent
npm run build
fi
fi
if [ "$NODE_ENV" = "production" ]; then
STRAPI_MODE="start"
elif [ "$NODE_ENV" = "development" ]; then
STRAPI_MODE="develop"
fi
Echo "Launch the App. (with ${STRAPI_MODE:-develop})..."
exec strapi "${STRAPI_MODE:-develop}"
else
exec "$@"
fi
Creating a Docker Image
Now, all the preparations are done. Now, let's create a Docker image. The command to create a docker image is as follows. There are many different options, but I only applied the options below.
Docker build -t
<image name to be created>:<tag name>
<Dockerfile location>
# Sample
docker build -t njo2-strapi-4.5.3:1.0 .
the last of the .Don't forget to paste .
You can specify the location where Dockerfile is located, but usually the command is executed from the location where Dockerfile is located, indicating the current location.Put on it.
This command does not seem to change anything. It is not downloaded as a separate file. If you look at Docker's image list on the Docker command or on the Docker desktop, you can see that the results have been generated.
- On Docker Desktop, run the
docker images ls
command in the CMD window from the left image menu... - A list of the current images will appear.
Download Docker Image to a Local File
To add an image to the Synology Docker, you must download the Docker image first. The downloaded file is a compressed file.Downloaded to tar Enter the command below to create a tar compressed file called njo2-strapi-4.5.3.tar.
docker save -o
<Docker image compression file name to download>
<Docker image to create>
# Sample
docker save -o njo2-strapi-4.5.3.tar njo2-strapi-4.5.3
Now upload this compressed file to the synopsis as it is.
Applying Docker Images to a Synology NAS
It is recommended that you first verify that the container is created and executed successfully with the docker desktop on your local computer. Once determined to be a normal image, now upload the docker image to the shared folder on the Synology NAS.
Uploading an Image to a Synology
It doesn't matter where you are. I created and uploaded a folder called \_IMAGES
.
Registering an Image with a Synology Docker
Now register this image with the Synology Docker. Run the Docker program in the scenario.
- In the Docker app, select Image from the left menu.
- Select Add from the top menu.
- From File, select Add.
Select the image file from the uploaded folder to add the image. It's over 1GB, so it takes some time to add it.(approximately 2 to 5 minutes?) There are times when the addition fails, but it can go up after several more attempts.
The image is then registered as shown above.
Running the Strapi Docker Container
Now, you can run the container with the uploaded image, but before that, you need to install the DB. If you do not install a separate DB, you will automatically use the built-in lightweight sqlite, which is not a powerful DB. We recommend that you install and use postgreSQL whenever possible.
Please refer to the posting below for installation instructions.
There are parts of the above posting that run the Strapi container, but the contents below are improved, so please refer to the following.
Double-click Strapi image added from the image above to begin container installation.
Select a network first. For networks, select the same network as postgreSQL.
- You can usually choose bridge.
- Click the Next button.
- Set the container name first.
- Select Advanced Settings.
- Select the Environment tab and enter the environment variable.
- Click the Add button 7 times to create 7 input spaces.
- Enter the default 7 environment variable information.
The environmental information that needs to be set is as follows. Enter seven important variables and four additional variables.
setting value information setting environment variable
Environment Variables | Values | Description |
---|---|---|
Whether to develop NODE_ENV | development / production. | |
DATABASE_CLIENT | postgres | Create a db type to use. |
Creates the DB name set in DATABASE_NAME | strapi | strapi_postgres. |
DATABASE_HOST | 192.168.0.10 | Create the IP address of the Synology NAS. |
Create the port number set in DATABASE_PORT | 15432 | strapi_postgres. |
Create the User set in DATABASE_USERNAME | strapi | strapi_postgres. |
Create the password you set in DATABASE_PASSWORD | strapi | strapi_postgres. |
Below are options
Environment Variables | Values | Description |
---|---|---|
Create DATABASE_SSL | SSL Security | |
JWT_SECRET | User Rights Plug Citation Password used to sign JWT. | |
ADMIN_JWT_SECRET | Password used to sign JWT for Administrator Panel. | |
APP_KEYS | Secret key used to sign session cookies. |
- Click the Link tab.
- Click the Add button.
- Select postgreSQL from the container name. PostgreSQL must be installed in advance to be listed.
- Enter any alias.
Now exit the advanced settings and click the Next button.
Next, set the port settings.
- Local ports can be created to actually serve. The port must be opened by a firewall and can be accessed through that port. You can enter any number.
- You need to create a container port, and this port number can be 1337 port unless it is special. (It needs to be the same as
EXPOSE 1337
in the docker file setting.)
Finally, you can set the volume. This volume matches a folder on the local computer (shared folder in synopsis) with a specific folder on the docker image. So if you add a file here, it's synchronized to the other folder as well.
- Click the Add File or Add Folder button (Strapi only needs to add folders)
- For File/Folder, you can select the specific folder you want from Synology Nas.
- The mount path can be created by creating a docker image path, unless otherwise, you can create /srv/app. (As long as it is the same as
VOLUME/srv/app
in the dockerFile setting.)
This is the summary information that will eventually be generated.
- Click the Finish button to create and run the container immediately.
Open Firewall
Once the installation is complete, the Synology NAS must open the firewall. Failure to open may cause the container to die immediately upon initial installation.
Firewall is specified on the Security tab in Control Panel.
- Select the Control Panel icon.
- Select the Security tab.
- Select the Firewall tab.
- Click the Edit Rule button.
You can turn off the firewall for the above three Strapi-related programs. (The program name may vary.)
Try connecting
If you connect and see the screen above, 90% of the installation is complete. The rest of the problems can be solved by addressing the errors below.
Possible Errors
Initial container execution error
Situation
Running the container for the first time after the above setting can cause the container to die immediately.
The reason for this is that the dependency file is installed in the node_modules folder by package.json when it first runs, which is blocked by the firewall in the scenario and cannot be installed, resulting in an error.
Workaround
The solution is simple. If you release and install the firewall during the initial installation, the installation proceeds well. However, after the installation, you must re-enable the firewall. If you want to install the plug-in in the future, you must open the firewall again and close it again when the installation is complete. (Please check the explanation above.)
xxx파일 : no such file or directory
docker-entrypoint.sh : no such file or directory 또는 package.json : no such file or directory
Situation
When you create Dockerfile and run it as a container, you may encounter an error similar to the above. No matter how much I look at it, there is nothing wrong, but the above error occurs. I lost almost two days because of this.
For many different reasons, my problem was the CRLF problem. CRLF
, LF
is a new line type with invisible codes (\r\n) for CRLF
, and (\n) for LF
. This is a problem that occurs because each OS, such as Linux and Windows, has a different type of basic line change.
Workaround
If the solution was CRLF
, change it to LF
, and if it was LF
, change the format of the line change to CRLF
.
If you use VSCode, you can modify it as below.
- Click
CRLF
orLF
at the bottom right. - In the center of the top, select one of the two menus.
Strapi: Warning an error occurred while requesting the API
Situation
This part is installed normally and comes up to the initial screen, but when I access the actual administrator screen (https://localhost/admin), I sometimes get an error screen called Strapi: Warning an error while requesting the API
.
Workaround
The above problem is usually solved by building it before service. ( yarn build )
In the code above, the solution has already been applied, so it takes more time to build when loading for the first time.
# ...
if [ -f "yarn.lock" ]; then
echo "Node modules are not installed. Installing module using Yarn..."
yarn install --prod --silent
Yarn build # Added build
else
echo "Node modules are not installed. Installing module using npm..."
npm install --only=prod --silent
npm run build # added build
fi
# ...
oci runtime exec failed "/bin/bash"
Situation
The above problem is not actually an error. However, the problem is caused by the lack of bash in the OS.
This is because alpineLinux, which is used as the base OS for docker images, has removed most of the features to lighten the size, so there are features that are not available instead of small capacity. Among them, this phenomenon occurs because the bash program is also taken away. Instead of bash, sh is available by default.
Workaround
The above issue is the code to install alpine from dockerFile followed by the code below.
RUN apk update && apk add bash
I have applied it to dockerFile above.
Comments