Docker运行没有入口点

发布于 2025-01-28 00:42:04 字数 284 浏览 4 评论 0原文

我们可以没有入口点的{Docker Run}命令吗? 据我所知,必须有一个入口点(如果未指定,则应在大多数情况下默认为“ bash”)。否则,我将无法将图像作为容器运行。

假设我在物理机器上安装Linux。
有一个背景服务在后台运行,在端口上聆听然后做某事。
在登录之前,没有运行bash shell,我仍然可以通过名称和端口从另一台计算机调用背景服务。

{Docker Run}我可以做类似的事情吗?这意味着,我不需要入口点(或入口点是系统过程而不是bash?),只需让容器系统及其背景服务启动和运行即可。

Could we have a {docker run} command without a entry point?
As far as I can see, there must be an entry point (if not specified, it should be defaulted to "bash" at most cases). Otherwise, I cannot have an image running as an container.

Let say I install Linux at my physical machine.
There is a background service running at background, listening on a port and then doing something.
Before I login, there is no bash shell running and I can still call the background service from another machine by the name and port.

Can I do something similar by {docker run}? That mean, I do not need an entry point (or the entry point is a system process instead of bash?) and just let the container system together with its background services up and running.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2025-02-04 00:42:04

如果您Docker Run一个没有任何特殊入门点或命令选项的容器,则运行由其Image entrypointcmd指令指定的单个命令。这应该是运行容器的通常方式。例如:

# Launches a PostgreSQL server, without manually specifying a command
docker run \
  -d -p 5432:5432 -v pgdata:/var/lib/postgresql/data \
  postgres:14

图像名称之后放置的任何内容都将覆盖图像中的cmd。您可以将其用于各种有用的调试,也可以将多个容器运行在相同的代码库中。

docker build -t django-image .

# What's in that image?
docker run --rm django-image \
  ls -l /app

# Get an interactive shell in a temporary container
docker run --rm -it django-image \
  bash

# Launch the Django server normally
docker run -d --name web -p 8000:8000 django-image

# Also launch a Celery worker off that same image
docker run -d --name worker django-image \
  celery worker

通常,您不应需要Docker Run -EntryPoint选项。由于它是一个docker选项,因此需要在之前出现图像名称,并且它将显示的任何选项出现在之后的“命令”插槽中。

# This syntax is awkward, design to avoid it
docker run --rm \
  --entrypoint ls \
  django-image \
  -l /app

在您的dockerfile entrypoint完全是可选的。如果您认为上面建议的任何命令范围的验证表格,包括在初始开发过程中进行调试,则选择设置cmd而不是entrypoint

# no ENTRYPOINT
CMD python ./manage.py runserver 0.0.0.0:8000

请勿将entrypoint设置为解释器,例如python,然后将命令的其余部分放入cmd;这显然有效,但只能将命令限制在python脚本的内容中,因此您被迫进入尴尬的docker run -Entrypoint设置其他任何内容。

如果您设置entrypoint,我发现的设置最有用的是让它是一个执行一些初始设置的包装器shell脚本,然后以exec“ $@” to结束运行什么cmd是。即使在各种命令以上的表单中也可以使用,这些表单仍在运行包装脚本,但在最后运行用户提供的命令。 dockerfile entrypoint line 必须是json array(“ exec andtax”),以使其工作。

我的一个例外是,当构建Micro-imaimages为GO程序构建微图像时,除了编译程序本身之外,该程序实际上什么也没有。图像中既没有外壳也没有任何其他工具,除了运行一个程序之外,不可能做任何事情。仅在这种情况下,我将将entrypoint设置为汇编二进制的路径;它仍然一定是一个JSON数组。

If you docker run a container without any special entrypoint or command options, it runs the single command specified by its image's ENTRYPOINT and CMD directives. This should be the usual way you run a container. For example:

# Launches a PostgreSQL server, without manually specifying a command
docker run \
  -d -p 5432:5432 -v pgdata:/var/lib/postgresql/data \
  postgres:14

Anything you put after the image name overrides the CMD in the image. You can use this for various sorts of useful debugging, or to run multiple containers off the same code base.

docker build -t django-image .

# What's in that image?
docker run --rm django-image \
  ls -l /app

# Get an interactive shell in a temporary container
docker run --rm -it django-image \
  bash

# Launch the Django server normally
docker run -d --name web -p 8000:8000 django-image

# Also launch a Celery worker off that same image
docker run -d --name worker django-image \
  celery worker

You should not normally need the docker run --entrypoint option. Since it is a Docker option, it needs to appear before the image name, and any options it takes appear in the "command" slot after the image name.

# This syntax is awkward, design to avoid it
docker run --rm \
  --entrypoint ls \
  django-image \
  -l /app

In your Dockerfile ENTRYPOINT is totally optional. Prefer setting the CMD and not ENTRYPOINT if you think you'll ever need any of the command-override forms suggested above, including debugging during initial development.

# no ENTRYPOINT
CMD python ./manage.py runserver 0.0.0.0:8000

Do not set ENTRYPOINT to an interpreter like python and put the rest of the command in CMD; this apparently works but limits commands to only things that are Python scripts, so you're forced into the awkward docker run --entrypoint setup for anything else.

If you do set ENTRYPOINT, the setup I find most useful is to have it be a wrapper shell script that does some initial setup, then ends with exec "$@" to run whatever the CMD is. This works even with the various command-override forms, which still run the wrapper script but then run the user-provided command at the end. The Dockerfile ENTRYPOINT line must be a JSON array ("exec syntax") for this to work.

My one exception to this is when building micro-images for Go programs that literally contain nothing besides the compiled program itself. There is neither a shell nor any other tools in the image, and it's impossible to do anything besides run the one program. In that case only, I'll set ENTRYPOINT to the path to the compiled binary; it still must be a JSON array.

梦幻的味道 2025-02-04 00:42:04

您不需要任何入口点即可运行Docker映像。您可以按照命令运行图像。如果您的Dockerfile默认包含入口点,请删除它。

docker run image_name

还在这里共享Docker文件,这可能会帮助人们更好地回答您的问题。

You don't need any entry point to run the docker image. you can run the image following the command. If your dockerfile contains entry point by default then remove it.

docker run image_name

Also share the docker file here, which might help people to answer better of your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文