gpt4 book ai didi

curl - 获取 docker API 引擎 exec 输出

转载 作者:行者123 更新时间:2023-12-01 05:58:09 24 4
gpt4 key购买 nike

我尝试从我在容器内运行的命令获取输出,但到目前为止我只在 RAW 文本中获取它。我需要能够使用 PHP 获取输出,当它以 RAW 文本形式返回时,我似乎无法做到这一点。

这是我的 Dockerfile:

FROM phusion/baseimage:0.9.19

CMD ["/sbin/my_init"]

WORKDIR /root

RUN sed -i "s/^exit 101$/exit 0/" /usr/sbin/policy-rc.d

RUN apt-get update
RUN DEBIAN_FRONTEND="noninteractive" apt-get install -y \
nginx \
php-fpm php-mysql php-gd php-curl php-cli php-mbstring php-dom unzip

RUN service php7.0-fpm start

EXPOSE 80

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

这是我正在运行的 curl 请求:
## Create container
curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -d '{"Image":"test-container","Env":["VIRTUAL_HOST=test.domain.dev"]}' -X POST http:/v1.27/containers/create?name=test.domain.dev

## Start container
curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -X POST http:/v1.27/containers/test.domain.dev/start

## Create exec instance
curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -d '{ "AttachStdin":false,"AttachStdout":true,"AttachStderr":true, "Tty":false, "Cmd":["/bin/bash", "-c", "date"] }' -X POST http:/v1.27/containers/test.domain.dev/exec

## Start exec instance
curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -d '{ "Detach": false, "Tty": false }' -X POST http:/v1.27/exec/bcd34186a9a7360809cace3674fec03bd1c70c1c453be24ad058a03fa0b0e960/start

// Sun Mar 26 23:23:53 UTC 2017

来自 docker info 的输出:
Containers: 8
Running: 4
Paused: 0
Stopped: 4
Images: 152
Server Version: 17.03.0-ce
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 977c511eda0925a723debdc94d09459af49d082a
runc version: a01dafd48bc1c7cc12bdb01206f9fea7dd6feb70
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.12-moby
Operating System: Alpine Linux v3.5
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.952 GiB
Name: moby
ID: JNZW:S7LC:4JRG:M3K3:6EFV:RZNR:Q7T5:XNJA:LB2Z:PO5G:PGPX:RI2E
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 43
Goroutines: 81
System Time: 2017-03-26T23:25:45.809302866Z
EventsListeners: 2
No Proxy: *.local, 169.254/16
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

那么,有没有正确的方法来获得这个输出?谢谢你的帮助

最佳答案

创建 exec 实例时,我将“AttachStdout”设置为 true,“Tty”设置为 true。
在请求 start exec 中,我将“Tty”设置为 true。

这向我显示了 tty 中的值,但我无法从应用程序中捕获它。

# Create the exec
#
EXEC_CREATE=`curl --silent --unix-socket /var/run/docker.sock "http:/containers/${CONTAINER_NAME}/exec" -XPOST \
-H "Content-Type: application/json" \
-d '{
"AttachStdout": true,
"Tty": true,
"Cmd": [ "ruby", "-e", "5.times { puts :hi; sleep 1 }"]
}'
`
echo $EXEC_CREATE | jq '.'

# Run the exec
#
EXEC_ID=$(echo $EXEC_CREATE | jq -r '.Id')
curl --silent --unix-socket /var/run/docker.sock "http:/exec/${EXEC_ID}/start" -XPOST \
-H "Content-Type: application/json" \
-d '{
"Detach": false,
"Tty": true
}'

taken from

关于curl - 获取 docker API 引擎 exec 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com