如何在运行的docker-compose容器中使用/var/run/docker.sock?

时间:2019-07-14 15:08:16

标签: postgresql docker-compose zabbix unix-socket

我有这样的docker-compose.yml:

version: '3'
services:
  zabbix-agent:
    image: zabbix/zabbix-agent
    ports:
     - "10050:10050"
     - "10051:10051"
    volumes:
     - /var/run/docker.sock:/var/run/docker.sock
     - /etc/localtime:/etc/localtime:ro
     - ./zbx_env/etc/zabbix/zabbix_agentd.d:/etc/zabbix/zabbix_agentd.d:ro
     - ./zbx_env/var/lib/zabbix/modules:/var/lib/zabbix/modules:ro
     - ./zbx_env/var/lib/zabbix/enc:/var/lib/zabbix/enc:ro
     - ./zbx_env/var/lib/zabbix/ssh_keys:/var/lib/zabbix/ssh_keys:ro
    links:
     - db
    env_file:
     - .env_agent
    user: root
    privileged: true
    pid: "host"
    stop_grace_period: 5s
    labels:
     com.zabbix.description: "Zabbix agent"
     com.zabbix.company: "Zabbix SIA"
     com.zabbix.component: "zabbix-agentd"
     com.zabbix.os: "ubuntu"

  postgres-server:
   image: postgres:latest
   volumes:
    - ./zbx_env/var/lib/postgresql/data:/var/lib/postgresql/data:rw
   env_file:
    - .env_db_pgsql
   user: root
   stop_grace_period: 1m

在zabbix-agent中,我使用UserParameter如下:

...
UserParameter=pgsql.ping[*],/bin/echo -e "\\\timing \n select 1" | psql -qAtX $1 | tail -n 1 |cut -d' ' -f2|sed 's/,/./'
...

当我从zabbix-server调用此UserParameter时,我有关于不存在psql的错误。而且是正确的-在容器“ zabbix-agent” psql中不存在。

如何从“ zabbix-agent”运行包含在“ postgres-server”中的psql并获得结果?

1 个答案:

答案 0 :(得分:1)

只需运行:

curl -H 'Content-Type: application/json' --unix-socket /var/run/docker.sock localhost:4243/containers/zabbix-agent/exec -d '{"Cmd":["date"]}'

发出请求的外观如下: https://docs.docker.com/develop/sdk/examples/

API参考如下: https://docs.docker.com/engine/api/v1.27/#operation/ContainerExec