简体   繁体   English

如何在 Docker yml 文件中运行 curl 命令?

[英]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 所以我有几个问题:

  1. Does this make sense to do this (have a curl as a service)?这样做有意义吗(将 curl 作为服务)?
  2. I have tried to escape the quotes and colon in the curl command but I get an error ScannerError: mapping values are not allowed here pointing at the colon after 'Accept'.我试图在 curl 命令中转义引号和冒号,但出现错误ScannerError: mapping values are not allowed here指向“接受”后的冒号。
  1. 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 ...
  1. 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.

相关问题 如何在python命令行中运行.py文件? - how do I run my .py file from within python command line? 如何在cmd批处理文件中运行另一个命令? - How do I run another command in a cmd batch file? 从命令行运行 m 文件时如何隐藏“MATLAB 命令行窗口”? - How do I hide “MATLAB Command Window” when I run an m-file from command line? 如何运行cURL命令以从Windows命令提示符下的文本文件下载数据? - How to run a cURL command for downloading data from a text file in Windows command prompt? 如何使用 Powershell 在 Docker RUN 命令中扩展参数? - How to I expand arguments in a Docker RUN command using Powershell? 我如何使此命令在后台运行 - How do I make this command run in the background 如何在 Windows 上运行这个 docker 命令? - How to run this docker command on windows? 如何在.bat文件的“ for”命令中格式化Tesseract-OCR语言设置 - How do I format Tesseract-OCR language settings within a .bat file's “for” command 在将参数传递给 PS 文件时,如何使用 invoke-command 从 windows 命令行运行 PS 文件? - How do I use invoke-command to run a PS file from the windows command line while passing parameters to the PS file? 我应该在 Windows 或 Docker 命令行上运行 Docker 命令吗? - Should I run Docker command on Windows or Docker command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM