英文:
docker file entrypoint file runs in loop
问题
以下是翻译好的内容:
我有一个像这样的Dockerfile
FROM node:alpine
WORKDIR /app
COPY package.json package-lock.json ./
COPY . /app
COPY entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh
RUN npm i -g sequelize-cli
EXPOSE 3000
ENTRYPOINT [ "sh", "./entrypoint.sh" ]
CMD ["npm", "run", "dev"]
# entrypoint.sh
sequelize-cli db:drop
sequelize-cli db:create
sequelize-cli db:migrate
在运行Docker文件后,entrypoint.sh文件继续循环运行。
英文:
I have a dockerfile like this
FROM node:alpine
WORKDIR /app
COPY package.json package-lock.json ./
COPY . /app
COPY entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh
RUN npm i -g sequelize-cli
EXPOSE 3000
ENTRYPOINT [ "sh", "./entrypoint.sh" ]
CMD ["npm", "run", "dev"]
entrypoint.sh
sequelize-cli db:drop
sequelize-cli db:create
sequelize-cli db:migrate
The entrypoint.sh file keeps running in a loop after running the docker file
答案1
得分: 1
你面临的问题可能是因为对Docker的ENTRYPOINT
和CMD
指令工作方式的误解。
在Docker中,ENTRYPOINT
指令允许您配置一个将作为可执行文件运行的容器。跟在ENTRYPOINT
指令后面的命令是在Docker容器启动时执行的命令。
CMD
指令为正在执行的容器提供默认值。这些默认值可以包括可执行文件,也可以省略可执行文件,这种情况下,您必须指定一个ENTRYPOINT
指令。
在您的情况下,ENTRYPOINT
正在执行entrypoint.sh
脚本,然后CMD
指令正在运行"npm","run","dev"
命令。
现在,如果您的entrypoint.sh
脚本设置为持续运行(可能是因为脚本中有一个无限循环),那么确实会导致您所见到的行为。但从您分享的脚本内容来看,似乎没有这样的循环。
可能的一个问题是,您的entrypoint.sh
脚本中的sequelize-cli
命令正在导致脚本重新运行。您正在使用的sequelize-cli
命令(db:drop
,db:create
,db:migrate
)可能正在操作Docker容器内的数据库。如果db:drop
命令删除数据库,然后db:create
和db:migrate
重新创建它,这可能会导致entrypoint.sh
脚本重新启动。
作为解决方法,您可能希望将数据库初始化与您的ENTRYPOINT
或CMD
分开。您可以将数据库初始化作为Dockerfile中的RUN
命令的一部分,以便在图像构建过程中执行,而不是每次容器启动时执行。
如果需要每次容器启动时初始化数据库,您可以使用一个包装脚本,该脚本首先运行数据库初始化,然后启动您的应用程序。这样,初始化将不会不断触发ENTRYPOINT
脚本。
以下是您可能如何调整Dockerfile的示例:
FROM node:alpine
WORKDIR /app
COPY package.json package-lock.json ./
COPY . /app
COPY entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh
RUN npm i -g sequelize-cli
EXPOSE 3000
RUN ["sh", "./entrypoint.sh"]
CMD ["npm", "run", "dev"]
如果这有所帮助,请告诉我!
英文:
The problem you are facing is likely due to a misunderstanding of how Docker's ENTRYPOINT
and CMD
instructions work.
In Docker, the ENTRYPOINT
instruction allows you to configure a container that will run as an executable. The command following the ENTRYPOINT
instruction is the command that gets executed when the Docker container starts up.
The CMD
instruction provides defaults for an executing container. These can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT
instruction.
In your case, the ENTRYPOINT
is executing the entrypoint.sh
script, and after that, the CMD
instruction is running the "npm", "run", "dev"
command.
Now, if your entrypoint.sh
script is set to continuously run (perhaps due to an infinite loop in the script), it would indeed cause the behavior you are seeing. But from the script content you shared, there seems to be no such loop.
One possible issue might be that the sequelize-cli
commands in your entrypoint.sh
script are causing the script to rerun. The sequelize-cli
commands you're using (db:drop
, db:create
, db:migrate
) are presumably operating on a database that is within your Docker container. If the db:drop
command is removing the database and then db:create
and db:migrate
are recreating it, this could somehow be causing the entrypoint.sh
script to restart.
As a solution, you might want to separate the database initialization from your ENTRYPOINT
or CMD
. You could make the database initialization a part of the Dockerfile RUN
command instead, so it's executed during the image build process, not every time the container starts up.
If the database needs to be initialized every time the container starts, you might want to use a wrapper script that first runs the database initialization, then starts your application. That way, the initialization won't keep triggering the ENTRYPOINT
script.
Here's an example of how you might adjust your Dockerfile:
FROM node:alpine
WORKDIR /app
COPY package.json package-lock.json ./
COPY . /app
COPY entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh
RUN npm i -g sequelize-cli
EXPOSE 3000
RUN ["sh", "./entrypoint.sh"]
CMD ["npm", "run", "dev"]
Let me know if this helped!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论