Class: DockerEngineAPI::Resources::Containers
- Inherits:
-
Object
- Object
- DockerEngineAPI::Resources::Containers
- Defined in:
- lib/docker_engine_api/resources/containers.rb
Instance Method Summary collapse
-
#archive(id, path:, request_options: {}) ⇒ nil
Get a tar archive of a resource in the filesystem of container id.
-
#attach(id, detach_keys: nil, logs: nil, stderr: nil, stdin: nil, stdout: nil, stream: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerAttachParams for more details.
-
#changes(id, request_options: {}) ⇒ Array<DockerEngineAPI::Models::FilesystemChange>
Returns which files in a container’s filesystem have been added, deleted, or modified.
-
#create(config:, name: nil, platform: nil, request_options: {}) ⇒ DockerEngineAPI::Models::CreateResponse
Some parameter documentations has been truncated, see Models::ContainerCreateParams for more details.
-
#delete(id, force: nil, link: nil, v: nil, request_options: {}) ⇒ nil
Remove a container.
-
#exec_(id, attach_stderr: nil, attach_stdin: nil, attach_stdout: nil, cmd: nil, console_size: nil, detach_keys: nil, env: nil, privileged: nil, tty: nil, user: nil, working_dir: nil, request_options: {}) ⇒ DockerEngineAPI::Models::ContainerExecResponse
Some parameter documentations has been truncated, see Models::ContainerExecParams for more details.
-
#export(id, request_options: {}) ⇒ nil
Export the contents of a container as a tarball.
-
#initialize(client:) ⇒ Containers
constructor
private
A new instance of Containers.
-
#inspect_(id, size: nil, request_options: {}) ⇒ DockerEngineAPI::Models::Container
Return low-level information about a container.
-
#kill(id, signal: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerKillParams for more details.
-
#list(all: nil, filters: nil, limit: nil, size: nil, request_options: {}) ⇒ Array<DockerEngineAPI::Models::Summary>
Some parameter documentations has been truncated, see Models::ContainerListParams for more details.
-
#logs(id, follow: nil, since: nil, stderr: nil, stdout: nil, tail: nil, timestamps: nil, until_: nil, request_options: {}) ⇒ StringIO
Some parameter documentations has been truncated, see Models::ContainerLogsParams for more details.
-
#pause(id, request_options: {}) ⇒ nil
Use the freezer cgroup to suspend all processes in a container.
-
#prune(filters: nil, request_options: {}) ⇒ DockerEngineAPI::Models::ContainerPruneResponse
Some parameter documentations has been truncated, see Models::ContainerPruneParams for more details.
-
#rename(id, name:, request_options: {}) ⇒ nil
Rename a container.
-
#resize(id, h:, w:, request_options: {}) ⇒ nil
Resize the TTY for a container.
-
#restart(id, signal: nil, t: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerRestartParams for more details.
-
#start(id, detach_keys: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerStartParams for more details.
-
#stats(id, one_shot: nil, stream: nil, request_options: {}) ⇒ DockerEngineAPI::Models::StatsResponse
Some parameter documentations has been truncated, see Models::ContainerStatsParams for more details.
-
#stop(id, signal: nil, t: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerStopParams for more details.
-
#top(id, ps_args: nil, request_options: {}) ⇒ DockerEngineAPI::Models::TopResponse
On Unix systems, this is done by running the
pscommand. -
#unpause(id, request_options: {}) ⇒ nil
Resume a container which has been paused.
-
#update(id, blkio_device_read_bps: nil, blkio_device_read_i_ops: nil, blkio_device_write_bps: nil, blkio_device_write_i_ops: nil, blkio_weight: nil, blkio_weight_device: nil, cgroup_parent: nil, cpu_count: nil, cpu_percent: nil, cpu_period: nil, cpu_quota: nil, cpu_realtime_period: nil, cpu_realtime_runtime: nil, cpuset_cpus: nil, cpuset_mems: nil, cpu_shares: nil, device_cgroup_rules: nil, device_requests: nil, devices: nil, init: nil, io_maximum_bandwidth: nil, io_maximum_i_ops: nil, memory: nil, memory_reservation: nil, memory_swap: nil, memory_swappiness: nil, nano_cpus: nil, oom_kill_disable: nil, pids_limit: nil, restart_policy: nil, ulimits: nil, request_options: {}) ⇒ DockerEngineAPI::Models::UpdateResponse
Some parameter documentations has been truncated, see Models::ContainerUpdateParams for more details.
-
#wait(id, condition: nil, request_options: {}) ⇒ DockerEngineAPI::Models::WaitResponse
Some parameter documentations has been truncated, see Models::ContainerWaitParams for more details.
Constructor Details
#initialize(client:) ⇒ Containers
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Containers.
849 850 851 |
# File 'lib/docker_engine_api/resources/containers.rb', line 849 def initialize(client:) @client = client end |
Instance Method Details
#archive(id, path:, request_options: {}) ⇒ nil
Get a tar archive of a resource in the filesystem of container id.
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/docker_engine_api/resources/containers.rb', line 201 def archive(id, params) parsed, = DockerEngineAPI::ContainerArchiveParams.dump_request(params) @client.request( method: :get, path: ["containers/%1$s/archive", id], query: parsed, model: NilClass, options: ) end |
#attach(id, detach_keys: nil, logs: nil, stderr: nil, stdin: nil, stdout: nil, stream: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerAttachParams for more details.
Attach to a container to read its output or send it input. You can attach to the same container multiple times and you can reattach to containers that have been detached.
Either the stream or logs parameter must be true for this endpoint to do anything.
See the [documentation for the ‘docker attach` command](docs.docker.com/engine/reference/commandline/attach/) for more details.
### Hijacking
This endpoint hijacks the HTTP connection to transport stdin, stdout, and stderr on the same socket.
This is the response from the daemon for an attach request:
“‘ HTTP/1.1 200 OK Content-Type: application/vnd.docker.raw-stream
- STREAM
-
“‘
After the headers and two new lines, the TCP connection can now be used for raw, bidirectional communication between the client and server.
To hint potential proxies about connection hijacking, the Docker client can also optionally send connection upgrade headers.
For example, the client sends this request to upgrade the connection:
“‘ POST /containers/16253994b7c4/attach?stream=1&stdout=1 HTTP/1.1 Upgrade: tcp Connection: Upgrade “`
The Docker daemon will respond with a ‘101 UPGRADED` response, and will similarly follow with the raw stream:
“‘ HTTP/1.1 101 UPGRADED Content-Type: application/vnd.docker.raw-stream Connection: Upgrade Upgrade: tcp
- STREAM
-
“‘
### Stream format
When the TTY setting is disabled in [‘POST /containers/create`](#operation/ContainerCreate), the HTTP Content-Type header is set to application/vnd.docker.multiplexed-stream and the stream over the hijacked connected is multiplexed to separate out
stdoutandstderr. The stream consists of a series of frames, each containing a header and a payload.The header contains the information which the stream writes (
stdoutorstderr). It also contains the size of the associated frame encoded in the last four bytes (uint32).It is encoded on the first eight bytes like this:
“‘go header := [8]byte0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4 “`
STREAM_TYPEcan be:-
0:
stdin(is written onstdout) -
1:
stdout -
2:
stderr
‘SIZE1, SIZE2, SIZE3, SIZE4` are the four bytes of the
uint32size encoded as big endian.Following the header is the payload, which is the specified number of bytes of
STREAM_TYPE.The simplest way to implement this protocol is the following:
-
Read 8 bytes.
-
Choose
stdoutorstderrdepending on the first byte. -
Extract the frame size from the last four bytes.
-
Read the extracted size and output it on the correct output.
-
Goto 1.
### Stream format when using a TTY
When the TTY setting is enabled in [‘POST /containers/create`](#operation/ContainerCreate), the stream is not multiplexed. The data exchanged over the hijacked connection is simply the raw data from the process PTY and client’s
stdin. -
332 333 334 335 336 337 338 339 340 341 |
# File 'lib/docker_engine_api/resources/containers.rb', line 332 def attach(id, params = {}) parsed, = DockerEngineAPI::ContainerAttachParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/attach", id], query: parsed.transform_keys(detach_keys: "detachKeys"), model: NilClass, options: ) end |
#changes(id, request_options: {}) ⇒ Array<DockerEngineAPI::Models::FilesystemChange>
Returns which files in a container’s filesystem have been added, deleted, or modified. The Kind of modification can be one of:
-
0: Modified (“C”) -
1: Added (“A”) -
2: Deleted (“D”)
359 360 361 362 363 364 365 366 |
# File 'lib/docker_engine_api/resources/containers.rb', line 359 def changes(id, params = {}) @client.request( method: :get, path: ["containers/%1$s/changes", id], model: DockerEngineAPI::Internal::Type::ArrayOf[DockerEngineAPI::FilesystemChange], options: params[:request_options] ) end |
#create(config:, name: nil, platform: nil, request_options: {}) ⇒ DockerEngineAPI::Models::CreateResponse
Some parameter documentations has been truncated, see Models::ContainerCreateParams for more details.
Create a container
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/docker_engine_api/resources/containers.rb', line 24 def create(params) parsed, = DockerEngineAPI::ContainerCreateParams.dump_request(params) @client.request( method: :post, path: "containers/create", query: parsed.except(:config), body: parsed[:config], model: DockerEngineAPI::CreateResponse, options: ) end |
#delete(id, force: nil, link: nil, v: nil, request_options: {}) ⇒ nil
Remove a container
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/docker_engine_api/resources/containers.rb', line 177 def delete(id, params = {}) parsed, = DockerEngineAPI::ContainerDeleteParams.dump_request(params) @client.request( method: :delete, path: ["containers/%1$s", id], query: parsed, model: NilClass, options: ) end |
#exec_(id, attach_stderr: nil, attach_stdin: nil, attach_stdout: nil, cmd: nil, console_size: nil, detach_keys: nil, env: nil, privileged: nil, tty: nil, user: nil, working_dir: nil, request_options: {}) ⇒ DockerEngineAPI::Models::ContainerExecResponse
Some parameter documentations has been truncated, see Models::ContainerExecParams for more details.
Run a command inside a running container.
404 405 406 407 408 409 410 411 412 413 |
# File 'lib/docker_engine_api/resources/containers.rb', line 404 def exec_(id, params = {}) parsed, = DockerEngineAPI::ContainerExecParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/exec", id], body: parsed, model: DockerEngineAPI::Models::ContainerExecResponse, options: ) end |
#export(id, request_options: {}) ⇒ nil
Export the contents of a container as a tarball.
426 427 428 429 430 431 432 433 |
# File 'lib/docker_engine_api/resources/containers.rb', line 426 def export(id, params = {}) @client.request( method: :get, path: ["containers/%1$s/export", id], model: NilClass, options: params[:request_options] ) end |
#inspect_(id, size: nil, request_options: {}) ⇒ DockerEngineAPI::Models::Container
Return low-level information about a container.
448 449 450 451 452 453 454 455 456 457 |
# File 'lib/docker_engine_api/resources/containers.rb', line 448 def inspect_(id, params = {}) parsed, = DockerEngineAPI::ContainerInspectParams.dump_request(params) @client.request( method: :get, path: ["containers/%1$s/json", id], query: parsed, model: DockerEngineAPI::Container, options: ) end |
#kill(id, signal: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerKillParams for more details.
Send a POSIX signal to a container, defaulting to killing to the container.
475 476 477 478 479 480 481 482 483 484 |
# File 'lib/docker_engine_api/resources/containers.rb', line 475 def kill(id, params = {}) parsed, = DockerEngineAPI::ContainerKillParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/kill", id], query: parsed, model: NilClass, options: ) end |
#list(all: nil, filters: nil, limit: nil, size: nil, request_options: {}) ⇒ Array<DockerEngineAPI::Models::Summary>
Some parameter documentations has been truncated, see Models::ContainerListParams for more details.
Returns a list of containers. For details on the format, see the [inspect endpoint](#operation/ContainerInspect).
Note that it uses a different, smaller representation of a container than inspecting a single container. For example, the list of linked containers is not propagated .
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/docker_engine_api/resources/containers.rb', line 149 def list(params = {}) parsed, = DockerEngineAPI::ContainerListParams.dump_request(params) @client.request( method: :get, path: "containers/json", query: parsed, model: DockerEngineAPI::Internal::Type::ArrayOf[DockerEngineAPI::Summary], options: ) end |
#logs(id, follow: nil, since: nil, stderr: nil, stdout: nil, tail: nil, timestamps: nil, until_: nil, request_options: {}) ⇒ StringIO
Some parameter documentations has been truncated, see Models::ContainerLogsParams for more details.
Get stdout and stderr logs from a container.
Note: This endpoint works only for containers with the json-file or journald logging driver.
517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/docker_engine_api/resources/containers.rb', line 517 def logs(id, params = {}) parsed, = DockerEngineAPI::ContainerLogsParams.dump_request(params) @client.request( method: :get, path: ["containers/%1$s/logs", id], query: parsed.transform_keys(until_: "until"), headers: {"accept" => "application/vnd.docker.multiplexed-stream"}, model: StringIO, options: ) end |
#pause(id, request_options: {}) ⇒ nil
Use the freezer cgroup to suspend all processes in a container.
Traditionally, when suspending a process the SIGSTOP signal is used, which is observable by the process being suspended. With the freezer cgroup the process is unaware, and unable to capture, that it is being suspended, and subsequently resumed.
545 546 547 548 549 550 551 552 |
# File 'lib/docker_engine_api/resources/containers.rb', line 545 def pause(id, params = {}) @client.request( method: :post, path: ["containers/%1$s/pause", id], model: NilClass, options: params[:request_options] ) end |
#prune(filters: nil, request_options: {}) ⇒ DockerEngineAPI::Models::ContainerPruneResponse
Some parameter documentations has been truncated, see Models::ContainerPruneParams for more details.
Delete stopped containers
568 569 570 571 572 573 574 575 576 577 |
# File 'lib/docker_engine_api/resources/containers.rb', line 568 def prune(params = {}) parsed, = DockerEngineAPI::ContainerPruneParams.dump_request(params) @client.request( method: :post, path: "containers/prune", query: parsed, model: DockerEngineAPI::Models::ContainerPruneResponse, options: ) end |
#rename(id, name:, request_options: {}) ⇒ nil
Rename a container
592 593 594 595 596 597 598 599 600 601 |
# File 'lib/docker_engine_api/resources/containers.rb', line 592 def rename(id, params) parsed, = DockerEngineAPI::ContainerRenameParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/rename", id], query: parsed, model: NilClass, options: ) end |
#resize(id, h:, w:, request_options: {}) ⇒ nil
Resize the TTY for a container.
618 619 620 621 622 623 624 625 626 627 |
# File 'lib/docker_engine_api/resources/containers.rb', line 618 def resize(id, params) parsed, = DockerEngineAPI::ContainerResizeParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/resize", id], query: parsed, model: NilClass, options: ) end |
#restart(id, signal: nil, t: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerRestartParams for more details.
Restart a container
647 648 649 650 651 652 653 654 655 656 |
# File 'lib/docker_engine_api/resources/containers.rb', line 647 def restart(id, params = {}) parsed, = DockerEngineAPI::ContainerRestartParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/restart", id], query: parsed, model: NilClass, options: ) end |
#start(id, detach_keys: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerStartParams for more details.
Start a container
674 675 676 677 678 679 680 681 682 683 |
# File 'lib/docker_engine_api/resources/containers.rb', line 674 def start(id, params = {}) parsed, = DockerEngineAPI::ContainerStartParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/start", id], query: parsed.transform_keys(detach_keys: "detachKeys"), model: NilClass, options: ) end |
#stats(id, one_shot: nil, stream: nil, request_options: {}) ⇒ DockerEngineAPI::Models::StatsResponse
Some parameter documentations has been truncated, see Models::ContainerStatsParams for more details.
This endpoint returns a live stream of a container’s resource usage statistics.
The precpu_stats is the CPU statistic of the previous read, and is used to calculate the CPU usage percentage. It is not an exact copy of the cpu_stats field.
If either precpu_stats.online_cpus or cpu_stats.online_cpus is nil then for compatibility with older daemons the length of the corresponding cpu_usage.percpu_usage array should be used.
On a cgroup v2 host, the following fields are not set
-
blkio_stats: all fields other thanio_service_bytes_recursive -
cpu_stats:cpu_usage.percpu_usage -
memory_stats:max_usageandfailcntAlso,memory_stats.statsfields are incompatible with cgroup v1.
To calculate the values shown by the stats command of the docker cli tool the following formulas can be used:
-
used_memory = ‘memory_stats.usage - memory_stats.stats.cache` (cgroups v1)
-
used_memory = ‘memory_stats.usage - memory_stats.stats.inactive_file` (cgroups v2)
-
available_memory =
memory_stats.limit -
Memory usage % = ‘(used_memory / available_memory) * 100.0`
-
cpu_delta = ‘cpu_stats.cpu_usage.total_usage - precpu_stats.cpu_usage.total_usage`
-
system_cpu_delta = ‘cpu_stats.system_cpu_usage - precpu_stats.system_cpu_usage`
-
number_cpus = ‘length(cpu_stats.cpu_usage.percpu_usage)` or
cpu_stats.online_cpus -
CPU usage % = ‘(cpu_delta / system_cpu_delta) * number_cpus * 100.0`
734 735 736 737 738 739 740 741 742 743 |
# File 'lib/docker_engine_api/resources/containers.rb', line 734 def stats(id, params = {}) parsed, = DockerEngineAPI::ContainerStatsParams.dump_request(params) @client.request( method: :get, path: ["containers/%1$s/stats", id], query: parsed.transform_keys(one_shot: "one-shot"), model: DockerEngineAPI::StatsResponse, options: ) end |
#stop(id, signal: nil, t: nil, request_options: {}) ⇒ nil
Some parameter documentations has been truncated, see Models::ContainerStopParams for more details.
Stop a container
763 764 765 766 767 768 769 770 771 772 |
# File 'lib/docker_engine_api/resources/containers.rb', line 763 def stop(id, params = {}) parsed, = DockerEngineAPI::ContainerStopParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/stop", id], query: parsed, model: NilClass, options: ) end |
#top(id, ps_args: nil, request_options: {}) ⇒ DockerEngineAPI::Models::TopResponse
On Unix systems, this is done by running the ps command. This endpoint is not supported on Windows.
788 789 790 791 792 793 794 795 796 797 |
# File 'lib/docker_engine_api/resources/containers.rb', line 788 def top(id, params = {}) parsed, = DockerEngineAPI::ContainerTopParams.dump_request(params) @client.request( method: :get, path: ["containers/%1$s/top", id], query: parsed, model: DockerEngineAPI::TopResponse, options: ) end |
#unpause(id, request_options: {}) ⇒ nil
Resume a container which has been paused.
810 811 812 813 814 815 816 817 |
# File 'lib/docker_engine_api/resources/containers.rb', line 810 def unpause(id, params = {}) @client.request( method: :post, path: ["containers/%1$s/unpause", id], model: NilClass, options: params[:request_options] ) end |
#update(id, blkio_device_read_bps: nil, blkio_device_read_i_ops: nil, blkio_device_write_bps: nil, blkio_device_write_i_ops: nil, blkio_weight: nil, blkio_weight_device: nil, cgroup_parent: nil, cpu_count: nil, cpu_percent: nil, cpu_period: nil, cpu_quota: nil, cpu_realtime_period: nil, cpu_realtime_runtime: nil, cpuset_cpus: nil, cpuset_mems: nil, cpu_shares: nil, device_cgroup_rules: nil, device_requests: nil, devices: nil, init: nil, io_maximum_bandwidth: nil, io_maximum_i_ops: nil, memory: nil, memory_reservation: nil, memory_swap: nil, memory_swappiness: nil, nano_cpus: nil, oom_kill_disable: nil, pids_limit: nil, restart_policy: nil, ulimits: nil, request_options: {}) ⇒ DockerEngineAPI::Models::UpdateResponse
Some parameter documentations has been truncated, see Models::ContainerUpdateParams for more details.
Change various configuration options of a container without having to recreate it.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/docker_engine_api/resources/containers.rb', line 113 def update(id, params = {}) parsed, = DockerEngineAPI::ContainerUpdateParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/update", id], body: parsed, model: DockerEngineAPI::UpdateResponse, options: ) end |
#wait(id, condition: nil, request_options: {}) ⇒ DockerEngineAPI::Models::WaitResponse
Some parameter documentations has been truncated, see Models::ContainerWaitParams for more details.
Block until a container stops, then returns the exit code.
835 836 837 838 839 840 841 842 843 844 |
# File 'lib/docker_engine_api/resources/containers.rb', line 835 def wait(id, params = {}) parsed, = DockerEngineAPI::ContainerWaitParams.dump_request(params) @client.request( method: :post, path: ["containers/%1$s/wait", id], query: parsed, model: DockerEngineAPI::WaitResponse, options: ) end |