在docker composer版本3的文档中,根据我的理解,在容器启动后运行一些命令我需要添加“command”标记,如下所示:
version: "3"
services:
broker:
image: "toke/mosquitto"
restart: always
ports:
- "1883:1883"
- "9001:9001"
command: ["cd /etc/mosquitto", "echo \"\" > mosquitto.pwd", "mosquitto_passwd -b /etc/mosquitto/mosquitto.pwd user pass", "echo \"password_file mosquitto.pwd\" >> mosquitto.conf", "echo \"allow_anonymous false\" >> mosquitto.conf"]
日志返回/usr/bin/docker-entrypoint.sh: 5: exec: cd /etc/mosquitto: not found
可以在composer文件中指定要运行的dockerfile和添加应该在那里运行的命令的解决方法,因此我创建了一个dockerfile:
FROM toke/mosquitto
WORKDIR .
EXPOSE 1883:1883 9001:9001
ENTRYPOINT cd /etc/mosquitto
ENTRYPOINT echo "" > mosquitto.pwd
ENTRYPOINT mosquitto_passwd -b mosquitto.pwd usertest passwordtest
ENTRYPOINT echo "password_file mosquitto.pwd" >> mosquitto.conf
ENTRYPOINT echo "allow_anonymous false" >> mosquitto.conf
容器不断重启,日志不会返回任何内容。我也尝试改变“CMD”的“ENTRYPOINT”而不改变输出。 作为指定docker composer文件使用特定dockerfile的加数,它无法解析并说:
ERROR: The Compose file '.\docker-compose.yml' is invalid because:
Unsupported config option for services.broker: 'dockerfile'
因为它无法解析或不理解“dockerfile”标记。有谁知道如何配置dockerfile甚至docker-composer来运行本文中的命令来配置mqtt代理?
答案 0 :(得分:1)
撰写文件中的command
条目不是要运行的命令列表,它只是一个命令及其参数
e.g。运行mosquitto -c /etc/mosquitto/mosquitto.conf
command: ["mosquitto", "-c", "/etc/mosquitto/mosquitto.conf"]
至于Dockerfile,应该只有一个ENTRYPOINT
或CMD
。如果要运行多个命令,则应创建一个shell脚本来运行它们,将其添加到容器中,然后使用ENTRYPOINT
或CMD
来运行脚本。