我已经创建了一个docker-compose.yaml
文件、Dockerfile
和entrypoint.sh
文件.Dockerfile用于创建一个ubuntu容器.在这个ubuntu容器中,我安装了Node.js
并克隆了我的项目存储库,然后运行该项目.该项目运行起来没有任何问题.但是,需要执行run.js
文件中写的以下函数.
const users = require("./user");
users();
console.log()
在user.js
文件中,
function users() {
console.log("I am the function in user.js")
}
module.exports = users;
Docker-Compose.yaml:,
version: '3'
services:
node-backend:
build:
context: .
dockerfile: Dockerfile
image: cron-api
container_name: cron-api-cont
ports:
- 3002:3002
entrypoint: /app/entrypoint.sh
文档文件:
FROM ubuntu:22.04
# Install necessary dependencies
RUN apt-get update && \
apt-get install -y git curl cron
# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install -y nodejs
# Create app working directory
WORKDIR /app
# Copy the entrypoint script into the container
COPY entrypoint.sh /app/entrypoint.sh
# Set executable permissions for the entrypoint script
RUN chmod +x /app/entrypoint.sh
# Expose the app port
EXPOSE 3002
入口点.sh:
!/bin/bash
# Set GitHub credentials
GIT_USERNAME="username"
GIT_TOKEN="my_token_private"
BRANCH="main"
# Clone the repository using credentials
git clone -b $BRANCH "https://${GIT_USERNAME}:${GIT_TOKEN}@github.com/username/node-for-docker-test.git" /app/api
# Check if the cloning process is complete
if [ -d "/app/api" ]; then
echo "Cloning completed successfully."
echo "List of directories inside api:"
ls -d /app/api
else
echo "Cloning failed."
fi
# Go to the backend directory
cd /app/api
# Install node modules
npm ci
# Start the server
npm start &
# Schedule the cron job to execute the users() function every 5 minutes
echo "*/1 * * * * node /app/api/run.js" > /tmp/cron_job.txt
echo "Cron job definition created."
# Check if the run.js file exists and print its content
run_js_file="/app/api/run.js"
if [ -f "$run_js_file" ]; then
echo "Found run.js file. Content:"
cat "$run_js_file"
else
echo "run.js file not found."
fi
# Install the cron job from the file
crontab /tmp/cron_job.txt
echo "Cron job installed."
# Start the cron service
service cron start &
echo "Cron service started."
# Keep the container running
tail -f /dev/null
使用entry ypoint t.sh,我克隆了repo并安装了 node 模块.然后开始这个项目.我不想为cron作业(job)编写另一个.sh.当我在容器内的/app/
或/app/api/
目录中编写命令时,当我手动执行node /app/api/run.js
时,它会运行users()
函数.
root@6ef26d9188:/app/api# ls
index.js node_modules package-lock.json package.json run.js user.js
root@6ef26d9188:/app/api# pwd
/app/api
root@6ef26d9188:/app/api# node /app/api/run.js
I am the function in user.js
在 docker 内部,运行cron服务.运行容器后,输出crontab -l
为;
root@6ef26d9188:/# crontab -l
*/1 * * * * node /app/api/run.js
输出service cron status
个IS;
root@6ef26d918826:/# service cron status
* cron is running
这是容器的日志(log):
✔ Container cron-api-cont Created 0.1s
Attaching to cron-api-cont
cron-api-cont | Cloning into '/app/api'...
cron-api-cont | Cloning completed successfully.
cron-api-cont | List of directories inside api:
cron-api-cont | /app/api
cron-api-cont |
cron-api-cont | added 90 packages, and audited 91 packages in 3s
cron-api-cont |
cron-api-cont | 11 packages are looking for funding
cron-api-cont | run `npm fund` for details
cron-api-cont |
cron-api-cont | 3 moderate severity vulnerabilities
cron-api-cont |
cron-api-cont | To address all issues (including breaking changes), run:
cron-api-cont | npm audit fix --force
cron-api-cont |
cron-api-cont | Run `npm audit` for details.
cron-api-cont | npm notice
cron-api-cont | npm notice New major version of npm available! 8.19.4 -> 9.7.2
cron-api-cont | npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.7.2>
cron-api-cont | npm notice Run `npm install -g npm@9.7.2` to update!
cron-api-cont | npm notice
cron-api-cont | Cron job definition created.
cron-api-cont | Found run.js file. Content:
cron-api-cont | const users = require("./user");
cron-api-cont |
cron-api-cont | users();Cron job installed.
cron-api-cont | Cron service started.
cron-api-cont | * Starting periodic command scheduler cron
cron-api-cont | ...done.
cron-api-cont |
cron-api-cont | > cron_tutorial@1.0.0 start
cron-api-cont | > nodemon index.js
cron-api-cont |
cron-api-cont | [nodemon] 2.0.22
cron-api-cont | [nodemon] to restart at any time, enter `rs`
cron-api-cont | [nodemon] watching path(s): *.*
cron-api-cont | [nodemon] watching extensions: js,mjs,json
cron-api-cont | [nodemon] starting `node index.js`
cron-api-cont | server is running on port 3002
有没有人知道,为什么不在终端上每1分钟打印一次console log
结果?