[英]How do I run curl command within Docker yml file?
I have 2 docker containers that are run in docker-compose, but are no longer talking to one another (connection refused error, when trying to perform a GraphQL mutation).我有 2 个 docker 容器,它们在 docker-compose 中运行,但不再相互交谈(尝试执行 GraphQL 突变时,连接被拒绝错误)。 To debug this I want to replace one of them with a simple curl command.
为了调试它,我想用一个简单的 curl 命令替换其中一个。
Original yml:原始yml:
services:
backend_container:
container_name: backend_container
image: backend_container:latest
command: parm1 parm2
volumes:
- /c/path_to_data:/data/in
ports:
- 19777:19777
depends_on:
- ui_container
ui_container:
container_name: ui_container
image: ui_container:latest
ports:
- 801:8080
proposed yml:建议的 yml:
services:
test_ui:
container_name: test_ui
volumes:
- /c/path_to_data:/data/in
depends_on:
- ui_container
command:
bash -c 'curl -X POST --data-binary \"@/data/in/my_query.txt\" -H \"Content-Type: application/json\" -H
\"Accept: application/json\"
http://ui_container:8080/graphql'
ui_container:
same as before
I am new docker so I have a couple of questions:我是新 docker 所以我有几个问题:
ScannerError: mapping values are not allowed here
pointing at the colon after 'Accept'.ScannerError: mapping values are not allowed here
指向“接受”后的冒号。
- Does it make sense to [...] have curl as a service?
[...] 将 curl 作为服务是否有意义?
Not really.并不真地。 For very short-term debugging, possibly, but in general a single command that will exit immediately doesn't make a good Compose service.
对于非常短期的调试,可能,但一般来说,将立即退出的单个命令并不能提供良好的 Compose 服务。 A better approach might be to
docker-compose run
a similarly-configured container with an alternate command:更好的方法可能是
docker-compose run
类似配置的容器:
docker-compose run backend_container \
curl --data-binary @/data/in/my_query.txt ...
- I have tried to escape the quotes and colon in the curl command....
我试图在 curl 命令中转义引号和冒号....
YAML has several ways to quote and escape strings. YAML 有几种方法可以引用和转义字符串。 Here I would probably use a folded block scalar : you can write a string across multiple lines, and it will get collapsed down into one line separated by spaces.
在这里,我可能会使用折叠块标量:您可以跨多行编写一个字符串,它会折叠成由空格分隔的一行。 This particular call does not need a
sh -c
wrapper (you'd only need it if you had shell redirections or environment variable references) and skipping that also saves a level of quoting.此特定调用不需要
sh -c
包装器(仅当您有 shell 重定向或环境变量引用时才需要它)并且跳过这也节省了一定程度的引用。
command: >-
curl
-X POST
--data-binary @/data/in/my_query.txt
-H "Content-Type: application/json"
-H "Accept: application/json"
http://ui_container:8080/graphql
Another good approach here is to use the list form of command
.这里的另一个好方法是使用
command
的列表形式。 Again, you can't use shell constructs here, but you can (and have to) explicitly specify what "words" are.同样,您不能在此处使用 shell 构造,但您可以(并且必须)明确指定“单词”是什么。
command:
- curl
- -X
- POST
- --data-binary
- @/data/in/my_query.txt
- -H
- "Content-Type: application/json" # <-- YAML string quoting
- -H
- "Accept: application/json"
- "http://ui_container:8080/graphql"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.