简体   繁体   English

在 shell 脚本中使用 Dockerfile 命令?

[英]Use Dockerfile commands inside of a shell script?

Suppose that I have an entry-point to a shell script as I want to use some conditionals in a dockerfile.假设我有一个 shell 脚本的入口点,因为我想在 dockerfile 中使用一些条件。 Is there a way to do something like this?有没有办法做这样的事情?

ENTRYPOINT ["./entry.sh", "lambda-name"]入口点 ["./entry.sh", "lambda-name"]

Inside of entry.sh entry.sh 内部

#!/usr/bin/env bash

lambda_name=$1

echo "$lambda_name"

if [ "$lambda_name" = "a.handler" ]; then
    CMD [ "a.handler" ]
elif [ "$lambda_name" = "b.handler" ];then
    CMD [ "b.handler" ]
else
    echo "not found"
fi

first of all you don't need that complication.首先,您不需要那种复杂性。

why not like this?为什么不这样?

#!/usr/bin/env bash

lambda_name=$1

echo "$lambda_name"

if [ "$lambda_name" = "a.handler" ]; then
    ./a.handler
elif [ "$lambda_name" = "b.handler" ];then
    ./b.handler
else
    echo "not found"
fi

also in your script you could use something like在你的脚本中你也可以使用类似的东西

exec "$@"

at the end of your script.在脚本的末尾。 this would run all your arguments.这将运行您所有的 arguments。

The ENTRYPOINT is the main container process. ENTRYPOINT是主容器进程。 It executes when you docker run the built image;它在您docker run构建的映像时执行; it's too late to run any other Dockerfile directives at that point.此时运行任何其他 Dockerfile 指令为时已晚。

In particular, the ENTRYPOINT gets passed the image's CMD (or a Compose command: or an alternate command after the docker run image-name ) as arguments, and it can do whatever it wants with that.特别是, CMD ENTRYPOINT或 Compose command:docker run image-name之后的备用命令),因为 arguments 可以随心所欲, So at this point you don't really need to "set the container's command", you can just execute whatever command it is you might have wanted to run.因此,此时您实际上并不需要“设置容器的命令”,您只需执行您可能想要运行的任何命令。

#!/bin/sh

case "$1" in
  a.handler) a.handler ;;
  b.handler) b.handler ;;
  *)
    echo "$1 not found" >&2
    exit 1
    ;;
esac

With this setup, a fairly common Docker pattern is just to take whatever gets passed as arguments and to run that at the end of the entrypoint script.使用此设置,一个相当常见的 Docker 模式只是将传递的任何内容作为 arguments 并在入口点脚本的末尾运行它。

#!/bin/sh

# ... do any startup-time setup required here ...

exec "$@"

That matches what you show in the question: if the first argument is a.handler then run that command, and so on.这与您在问题中显示的内容相匹配:如果第一个参数是a.handler则运行该命令,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM