How to append proxies items to an existing etcdockerdaemon.json file with yq - Stack Overflow

admin2025-04-28  3

I have the following /etc/docker/daemon.json file :

$ yq . /etc/docker/daemon.json
{
  "dns": [
    "a.b.c.d1"
  ]
}
$

I tried this command to append proxy lines to this file with yq :

$ yq '. + { proxies : {  "http-proxy":env($http_proxy) , "https-proxy":env($https_proxy) , "no-proxy":env($no_proxy)} }' /etc/docker/daemon.json
Error: 1:7: invalid input text "proxies : {  \"ht..."
$

I also tried this yq command :

$ yq '. + { .proxies = {  .http-proxy=env($http_proxy) , .https-proxy=env($https_proxy) , .no-proxy=env($no_proxy)} }' /etc/docker/daemon.json
Error: !!str () cannot be added to a !!map ()
$

I expect this json :

{
  "dns": [
    "a.b.c.d1"
  ],
  "proxies": {
    "http-proxy": "http://x.y.z.t:8080",
    "https-proxy": "http://x.y.z.t:8080",
    "no-proxy": "localhost,127.0.0.1,localaddress,.localdomain"
  }
}

How can I do that with yq ?

I have the following /etc/docker/daemon.json file :

$ yq . /etc/docker/daemon.json
{
  "dns": [
    "a.b.c.d1"
  ]
}
$

I tried this command to append proxy lines to this file with yq :

$ yq '. + { proxies : {  "http-proxy":env($http_proxy) , "https-proxy":env($https_proxy) , "no-proxy":env($no_proxy)} }' /etc/docker/daemon.json
Error: 1:7: invalid input text "proxies : {  \"ht..."
$

I also tried this yq command :

$ yq '. + { .proxies = {  .http-proxy=env($http_proxy) , .https-proxy=env($https_proxy) , .no-proxy=env($no_proxy)} }' /etc/docker/daemon.json
Error: !!str () cannot be added to a !!map ()
$

I expect this json :

{
  "dns": [
    "a.b.c.d1"
  ],
  "proxies": {
    "http-proxy": "http://x.y.z.t:8080",
    "https-proxy": "http://x.y.z.t:8080",
    "no-proxy": "localhost,127.0.0.1,localaddress,.localdomain.com"
  }
}

How can I do that with yq ?

Share Improve this question edited Jan 9 at 18:26 SebMa asked Jan 9 at 18:19 SebMaSebMa 4,8291 gold badge39 silver badges49 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Apparently, you're using mikefarah/yq, which requires object keys to be wrapped in quotes (unlike kislyuk/yq, for example), so change proxies: to "proxies": to resolve Error: 1:7: invalid input text "proxies: {\"ht...". Also, env expects its parameter without a $ sign. So, as an example, this would work:

http_proxy=a https_proxy=b no_proxy=c yq '. + {"proxies": {
  "http-proxy": env(http_proxy),
  "https-proxy": env(https_proxy),
  "no-proxy": env(no_proxy)
}}' /etc/docker/daemon.json

Alternatively, use the .proxies = syntax from your second attempt, which doesn't require the quotes but as an expression it needs to be outside the curly braces:

http_proxy=a https_proxy=b no_proxy=c yq '.proxies = {
  "http-proxy": env(http_proxy),
  "https-proxy": env(https_proxy),
  "no-proxy": env(no_proxy)
}' /etc/docker/daemon.json

Both examples produce (tested with mikefarah/yq v4.44.6):

{
  "dns": [
    "a.b.c.d1"
  ],
  "proxies": {
    "http-proxy": "a",
    "https-proxy": "b",
    "no-proxy": "c"
  }
}

Note: If you don't want the environment variables be parsed as YAML nodes, use envstr instead which always parses the values as strings.

转载请注明原文地址:http://anycun.com/QandA/1745781283a91206.html