Docker文件中的入口文件在循环中运行。

huangapple go评论57阅读模式
英文:

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的ENTRYPOINTCMD指令工作方式的误解。

在Docker中,ENTRYPOINT指令允许您配置一个将作为可执行文件运行的容器。跟在ENTRYPOINT指令后面的命令是在Docker容器启动时执行的命令。

CMD指令为正在执行的容器提供默认值。这些默认值可以包括可执行文件,也可以省略可执行文件,这种情况下,您必须指定一个ENTRYPOINT指令。

在您的情况下,ENTRYPOINT正在执行entrypoint.sh脚本,然后CMD指令正在运行"npm","run","dev"命令。

现在,如果您的entrypoint.sh脚本设置为持续运行(可能是因为脚本中有一个无限循环),那么确实会导致您所见到的行为。但从您分享的脚本内容来看,似乎没有这样的循环。

可能的一个问题是,您的entrypoint.sh脚本中的sequelize-cli命令正在导致脚本重新运行。您正在使用的sequelize-cli命令(db:dropdb:createdb:migrate)可能正在操作Docker容器内的数据库。如果db:drop命令删除数据库,然后db:createdb:migrate重新创建它,这可能会导致entrypoint.sh脚本重新启动。

作为解决方法,您可能希望将数据库初始化与您的ENTRYPOINTCMD分开。您可以将数据库初始化作为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!

huangapple
  • 本文由 发表于 2023年6月22日 04:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定