简体   繁体   中英

How use /var/run/docker.sock inside running docker-compose container?

I have docker-compose.yml like this:

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

In zabbix-agent i use UserParameter like as:

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

When i call from zabbix-server this UserParameter, i have error about not exists psql. And it's correct - in container 'zabbix-agent' psql not exist.

How can i run psql that containing in 'postgres-server' from 'zabbix-agent' and get result?

Just run:

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

How make requests look this: https://docs.docker.com/develop/sdk/examples/

API reference look this: https://docs.docker.com/engine/api/v1.27/#operation/ContainerExec

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM