clibdocker  0.1.0-alpha.0
A Docker API for C
docker_containers.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2018-2022 Abhishek Mishra
4  *
5  * This file is part of clibdocker.
6  *
7  * clibdocker is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation,
10  * either version 3 of the License, or (at your option)
11  * any later version.
12  *
13  * clibdocker is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with clibdocker.
20  * If not, see <https://www.gnu.org/licenses/>.
21  *
22  */
23 
29 #ifndef DOCKER_CONTAINERS_H_
30 #define DOCKER_CONTAINERS_H_
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <coll_arraylist.h>
37 #include "docker_result.h"
38 #include "docker_connection_util.h"
39 #include "docker_util.h"
40 
44 typedef json_object docker_ctr_port;
45 
51 #define free_docker_ctr_port(ctr_port) json_object_put((json_object*) ctr_port)
52 
59 #define docker_ctr_port_type_get(ctr_port) get_attr_str((json_object*)ctr_port, "Type")
60 
67 #define docker_ctr_port_private_port_get(ctr_port) get_attr_long((json_object*)ctr_port, "PrivatePort")
68 
75 #define docker_ctr_port_public_port_get(ctr_port) get_attr_long((json_object*)ctr_port, "PublicPort")
76 
80 typedef json_object docker_ctr_ls_item;
81 
87 #define free_docker_ctr_ls_item(ctr) json_object_put((json_object*) ctr)
88 
95 #define docker_ctr_ls_item_id_get(ctr) get_attr_str((json_object*)ctr, "Id")
96 
103 #define docker_ctr_ls_item_names_get(ctr) get_attr_json_object((json_object*)ctr, "Names")
104 
111 #define docker_ctr_ls_item_names_length(ctr) json_object_array_length(docker_ctr_ls_item_names_get(ctr))
112 
120 #define docker_ctr_ls_item_names_get_idx(ctr, i) (char*) json_object_get_string(json_object_array_get_idx(docker_ctr_ls_item_names_get(ctr), i))
121 
128 #define docker_ctr_ls_item_image_get(ctr) get_attr_str((json_object*)ctr, "Image")
129 
136 #define docker_ctr_ls_item_image_id_get(ctr) get_attr_str((json_object*)ctr, "ImageID")
137 
144 #define docker_ctr_ls_item_command_get(ctr) get_attr_str((json_object*)ctr, "Command")
145 
152 #define docker_ctr_ls_item_created_get(ctr) get_attr_long_long((json_object*)ctr, "Created")
153 
160 #define docker_ctr_ls_item_state_get(ctr) get_attr_str((json_object*)ctr, "State")
161 
168 #define docker_ctr_ls_item_status_get(ctr) get_attr_str((json_object*)ctr, "Status")
169 
176 #define docker_ctr_ls_item_size_rw_get(ctr) get_attr_long_long((json_object*)ctr, "SizeRw")
177 
184 #define docker_ctr_ls_item_size_root_fs_get(ctr) get_attr_long_long((json_object*)ctr, "SizeRootFs")
185 
192 #define docker_ctr_ls_item_ports_get(ctr) get_attr_json_object((json_object*)ctr, "Ports")
193 
200 #define docker_ctr_ls_item_ports_length(ctr) json_object_array_length(docker_ctr_ls_item_ports_get(ctr))
201 
209 #define docker_ctr_ls_item_ports_get_idx(ctr, i) (docker_ctr_port*) json_object_array_get_idx(docker_ctr_ls_item_ports_get(ctr), i)
210 
217 #define docker_ctr_ls_item_ports_labels_get(ctr) get_attr_json_object((json_object*)ctr, "Labels")
218 
224 #define docker_ctr_ls_item_ports_labels_foreach(ctr) json_object_object_foreach(docker_ctr_ls_item_ports_labels_get(ctr), key, val)
225 
230 typedef json_object docker_ctr_list;
231 
237 #define free_docker_ctr_list(ctr_ls) json_object_put(ctr_ls)
238 
245 #define docker_ctr_list_length(ctr_ls) json_object_array_length(ctr_ls)
246 
254 #define docker_ctr_list_get_idx(ctr_ls, i) (docker_ctr*) json_object_array_get_idx(ctr_ls, i)
255 
267 MODULE_API d_err_t docker_container_list(docker_context* ctx, docker_ctr_list** container_list,
268  int all, int limit, int size, ...);
269 
282  int all, int limit, int size, const char* filters);
283 
287 typedef json_object docker_ctr_create_params;
288 
294 #define make_docker_ctr_create_params (docker_ctr_create_params*)json_object_new_object
295 
301 #define free_docker_ctr_create_params(ctr_create) json_object_put(ctr_create)
302 
309 #define docker_ctr_create_params_image_set(ctr_create, img) set_attr_str(ctr_create, "Image", img)
310 
317 #define docker_ctr_create_params_entrypoint_set(ctr_create, entry) set_attr_str(ctr_create, "Entrypoint", entry)
318 
325 #define docker_ctr_create_params_cmd_add(ctr_create, cmd) add_array_str(ctr_create, "Cmd", cmd)
326 
333 #define docker_ctr_create_params_env_add(ctr_create, env) add_array_str(ctr_create, "Env", env)
334 
341 #define docker_ctr_create_params_tty_set(ctr_create, tty) set_attr_boolean(ctr_create, "Tty", tty)
342 
349 #define docker_ctr_create_params_openstdin_set(ctr_create, openstdin) set_attr_boolean(ctr_create, "OpenStdin", openstdin)
350 
357 #define docker_ctr_create_params_attachstdin_set(ctr_create, attachstdin) set_attr_boolean(ctr_create, "AttachStdin", attachstdin)
358 
365 #define docker_ctr_create_params_attachstdout_set(ctr_create, attachstdout) set_attr_boolean(ctr_create, "AttachStdout", attachstdout)
366 
373 #define docker_ctr_create_params_attachstderr_set(ctr_create, attachstderr) set_attr_boolean(ctr_create, "AttachStderr", attachstderr)
374 
384  char** id, docker_ctr_create_params* params);
385 
394 typedef json_object docker_ctr;
395 
401 #define free_docker_ctr(ctr) json_object_put((json_object*) ctr)
402 
409 #define docker_ctr_id_get(ctr) get_attr_str((json_object*)ctr, "Id")
410 
417 #define docker_ctr_image_get(ctr) get_attr_str((json_object*)ctr, "Image")
418 
425 #define docker_ctr_name_get(ctr) get_attr_str((json_object*)ctr, "Name")
426 
433 #define docker_ctr_path_get(ctr) get_attr_str((json_object*)ctr, "Path")
434 
441 #define docker_ctr_created_get(ctr) get_attr_long_long((json_object*)ctr, "Created")
442 
449 #define docker_ctr_state_get(ctr) get_attr_str((json_object*)ctr, "State")
450 
457 #define docker_ctr_status_get(ctr) get_attr_str((json_object*)ctr, "Status")
458 
465 #define docker_ctr_size_rw_get(ctr) get_attr_long_long((json_object*)ctr, "SizeRw")
466 
473 #define docker_ctr_size_root_fs_get(ctr) get_attr_long_long((json_object*)ctr, "SizeRootFs")
474 
481 #define docker_ctr_ports_get(ctr) get_attr_json_object((json_object*)ctr, "Ports")
482 
489 #define docker_ctr_ports_length(ctr) json_object_array_length(docker_ctr_ports_get(ctr))
490 
498 #define docker_ctr_ports_get_idx(ctr, i) (docker_ctr_port*) json_object_array_get_idx(docker_ctr_ports_get(ctr), i)
499 
506 #define docker_ctr_ports_labels_get(ctr) get_attr_json_object((json_object*)ctr, "Labels")
507 
513 #define docker_ctr_ports_labels_foreach(ctr) json_object_object_foreach(docker_ctr_ports_labels_get(ctr), key, val)
514 
523 MODULE_API docker_ctr* docker_inspect_container(docker_context* ctx, char* id, int size);
524 
525 // /**
526 // * Struct which holds the titles of the process line, and the details of all processes.
527 // */
528 // typedef struct docker_container_ps_t {
529 // arraylist* titles;
530 
531 // //each item in this list is another list with values for
532 // //each process
533 // arraylist* processes;
534 // } docker_container_ps;
535 
539 typedef json_object docker_ctr_process_details;
540 
547 #define docker_ctr_process_details_length(ctr_ps_ls) json_object_array_length(ctr_ps_ls)
548 
556 #define docker_ctr_process_details_get_idx(ctr_ps_ls, i) (char*) json_object_array_get_idx(ctr_ps_ls, i)
557 
561 typedef json_object docker_ctr_ps;
562 
568 #define free_docker_ctr_ps(ctr_ps) json_object_put((json_object*) ctr_ps)
569 
576 #define docker_ctr_ps_titles_get(ctr_ps) get_attr_json_object((json_object*)ctr_ps, "Titles")
577 
584 #define docker_ctr_ps_titles_length(ctr_ps) json_object_array_length(docker_ctr_ps_titles_get(ctr_ps))
585 
593 #define docker_ctr_ps_titles_get_idx(ctr_ps, i) (char*) json_object_get_string(json_object_array_get_idx(docker_ctr_ps_titles_get(ctr_ps), i))
594 
601 #define docker_ctr_ps_processes_get(ctr_ps) get_attr_json_object((json_object*)ctr_ps, "Processes")
602 
609 #define docker_ctr_ps_processes_length(ctr_ps) json_object_array_length(docker_ctr_ps_processes_get(ctr_ps))
610 
618 #define docker_ctr_ps_processes_get_idx(ctr_ps, i) (docker_ctr_process_details*) json_object_array_get_idx(docker_ctr_ps_processes_get(ctr_ps), i)
619 
630  docker_ctr_ps** ps, char* id, char* process_args);
631 
633 
650 MODULE_API d_err_t docker_container_logs(docker_context* ctx, char** log, size_t* log_length, char* id, int follow,
651  int std_out, int std_err, long since, long until, int timestamps, int tail);
652 
656 typedef void (docker_log_line_handler)(void* handler_args, int stream_id, int line_num, char* line);
657 
667 MODULE_API d_err_t docker_container_logs_foreach(void* handler_args, char* log, size_t log_length, docker_log_line_handler* line_handler);
668 
670 
674 typedef enum {
675  DOCKER_FS_MODIFIED = 0, DOCKER_FS_ADDED = 1, DOCKER_FS_DELETED = 2
676 } change_kind;
677 
683  char* path;
686 
696  const char* path, const char* kind);
697 
704 
708 typedef arraylist docker_changes_list;
709 
716 MODULE_API d_err_t make_docker_changes_list(docker_changes_list** changes_list);
717 
725 MODULE_API int docker_changes_list_add(docker_changes_list* list,
727 
736  int i);
737 
744 MODULE_API size_t docker_changes_list_length(docker_changes_list* list);
745 
754 MODULE_API d_err_t docker_container_changes(docker_context* ctx, docker_changes_list** changes, char* id);
755 
757 
761 typedef json_object docker_percpu_usage_list;
762 
769 #define docker_percpu_usage_list_length(percpu_usage_ls) json_object_array_length(percpu_usage_ls)
770 
778 #define docker_percpu_usage_list_get_idx(percpu_usage_ls, i) (docker_percpu_usage*) json_object_array_get_idx(percpu_usage_ls, i)
779 
783 typedef json_object docker_cpu_usage;
784 
790 #define free_docker_cpu_usage(cpu_usg) json_object_put((json_object*) cpu_usg)
791 
798 #define docker_cpu_usage_usermode_get(cpu_usg) get_attr_long_long((json_object*)cpu_usg, "usage_in_usermode")
799 
806 #define docker_cpu_usage_total_get(cpu_usg) get_attr_long_long((json_object*)cpu_usg, "total_usage")
807 
814 #define docker_cpu_usage_kernelmode_get(cpu_usg) get_attr_long_long((json_object*)cpu_usg, "usage_in_kernelmode")
815 
822 #define docker_cpu_usage_percpu_usage_get(cpu_usg) (docker_percpu_usage_list*)get_attr_json_object((json_object*)cpu_usg, "percpu_usage")
823 
827 typedef json_object docker_throttling_data;
828 
834 #define free_docker_throttling_data(throt) json_object_put((json_object*) throt)
835 
842 #define docker_throttling_data_periods_get(throt) get_attr_long_long((json_object*)throt, "periods")
843 
850 #define docker_throttling_data_throttled_periods_get(throt) get_attr_long_long((json_object*)throt, "throttled_periods")
851 
858 #define docker_throttling_data_throttled_time_get(throt) get_attr_long_long((json_object*)throt, "throttled_time")
859 
863 typedef json_object docker_container_cpu_stats;
864 
870 #define free_docker_container_cpu_stats(cpu_stats) json_object_put((json_object*) cpu_stats)
871 
878 #define docker_container_cpu_stats_system_cpu_usage_get(cpu_stats) get_attr_long_long((json_object*)cpu_stats, "system_cpu_usage")
879 
886 #define docker_container_cpu_stats_online_cpus_get(cpu_stats) get_attr_long((json_object*)cpu_stats, "online_cpus")
887 
894 #define docker_container_cpu_stats_cpu_usage_get(cpu_stats) (docker_cpu_usage*)get_attr_json_object((json_object*)cpu_stats, "cpu_usage")
895 
902 #define docker_container_cpu_stats_throttling_data_get(cpu_stats) (docker_throttling_data*)get_attr_json_object((json_object*)cpu_stats, "throttling_data")
903 
907 typedef json_object docker_container_mem_stats;
908 
914 #define free_docker_container_mem_stats(mem_stats) json_object_put((json_object*) mem_stats)
915 
922 #define docker_container_mem_stats_max_usage_get(mem_stats) get_attr_long_long((json_object*)mem_stats, "max_usage")
923 
930 #define docker_container_mem_stats_usage_get(mem_stats) get_attr_long_long((json_object*)mem_stats, "usage")
931 
938 #define docker_container_mem_stats_failcnt_get(mem_stats) get_attr_long_long((json_object*)mem_stats, "failcnt")
939 
946 #define docker_container_mem_stats_limit_get(mem_stats) get_attr_long_long((json_object*)mem_stats, "limit")
947 
954 #define docker_container_mem_stats_stats_get(mem_stats) get_attr_json_object((json_object*)mem_stats, "stats")
955 
961 #define docker_container_mem_stats_stats_foreach(mem_stats) json_object_object_foreach(docker_container_mem_stats_stats_get(mem_stats), key, val)
962 
966 typedef json_object docker_container_net_stats;
967 
973 #define free_docker_container_net_stats(net_stats) json_object_put((json_object*) net_stats)
974 
980 #define docker_container_net_stats_foreach(net_stats) json_object_object_foreach(net_stats, key, val)
981 
987 #define docker_container_net_stats_stats_foreach(net_stats_val) json_object_object_foreach(net_stats_val, key, val)
988 
992 typedef json_object docker_container_pid_stats;
993 
999 #define free_docker_container_pid_stats(pid_stats) json_object_put((json_object*) pid_stats)
1000 
1006 #define docker_container_pid_stats_foreach(pid_stats) json_object_object_foreach(pid_stats, key, val)
1007 
1018 typedef json_object docker_container_stats;
1019 
1025 #define free_docker_container_stats(stats) json_object_put((json_object*) stats)
1026 
1033 #define docker_container_stats_read_get(stats) get_attr_str((json_object*)stats, "read")
1034 
1041 #define docker_container_stats_pid_stats_get(stats) (docker_container_pid_stats*)get_attr_json_object((json_object*)stats, "pid_stats")
1042 
1049 #define docker_container_stats_networks_get(stats) (docker_container_net_stats*)get_attr_json_object((json_object*)stats, "networks")
1050 
1057 #define docker_container_stats_mem_stats_get(stats) (docker_container_mem_stats*)get_attr_json_object((json_object*)stats, "memory_stats")
1058 
1065 #define docker_container_stats_cpu_stats_get(stats) (docker_container_cpu_stats*)get_attr_json_object((json_object*)stats, "cpu_stats")
1066 
1073 #define docker_container_stats_precpu_stats_get(stats) (docker_container_cpu_stats*)get_attr_json_object((json_object*)stats, "precpu_stats")
1074 
1084  docker_container_stats** stats, char* id);
1085 
1096  void (*docker_container_stats_cb)(docker_container_stats* stats,
1097  void* cbargs), void* cbargs, char* id);
1098 
1106  docker_container_stats* stats);
1107 
1109 
1119  char* id, char* detachKeys);
1120 
1130  char* id, int t);
1131 
1141  char* id, int t);
1142 
1152  char* id, char* signal);
1153 
1163  char* id, char* name);
1164 
1173  char* id);
1174 
1183  char* id);
1184 
1185 //TODO: implement wait status code in API.
1195  char* id, char* condition);
1196 
1197 
1209  char* id, int v, int force, int link);
1210 
1224 MODULE_API d_err_t docker_container_attach_default(docker_context* ctx, char* id,
1225  char* detach_keys, int logs, int stream, int attach_stdin, int attach_stdout, int attach_stderr);
1226 
1227 #ifdef __cplusplus
1228 }
1229 #endif
1230 
1231 #endif /* DOCKER_CONTAINERS_H_ */
Docker Connection Utils.
int docker_changes_list_add(docker_changes_list *list, docker_container_change *item)
Add a container change item to the docker changes list.
Definition: docker_containers.c:309
d_err_t docker_container_attach_default(docker_context *ctx, char *id, char *detach_keys, int logs, int stream, int attach_stdin, int attach_stdout, int attach_stderr)
Attach to a container.
Definition: docker_containers.c:1199
d_err_t docker_wait_container(docker_context *ctx, char *id, char *condition)
Wait for a container.
Definition: docker_containers.c:614
json_object docker_throttling_data
Docker Throttling Data json object.
Definition: docker_containers.h:827
json_object docker_container_net_stats
Docker Container Net Stats json object.
Definition: docker_containers.h:966
change_kind
Enum for the change type of entry in filesystem changes.
Definition: docker_containers.h:674
json_object docker_cpu_usage
Docker cpu usage object.
Definition: docker_containers.h:783
struct docker_container_change_t docker_container_change
Docker container change struct.
d_err_t docker_container_get_stats_cb(docker_context *ctx, void(*docker_container_stats_cb)(docker_container_stats *stats, void *cbargs), void *cbargs, char *id)
Get stats from a running container. (the streaming version)
Definition: docker_containers.c:391
d_err_t docker_create_container(docker_context *ctx, char **id, docker_ctr_create_params *params)
Create a docker container given the container creation params.
Definition: docker_containers.c:120
json_object docker_container_pid_stats
Docker Container Pid Stats json object.
Definition: docker_containers.h:992
d_err_t docker_kill_container(docker_context *ctx, char *id, char *signal)
Kill a container.
Definition: docker_containers.c:516
json_object docker_container_mem_stats
Docker Container Memory Stats json object.
Definition: docker_containers.h:907
d_err_t make_docker_changes_list(docker_changes_list **changes_list)
Create a new docker changes list json object.
Definition: docker_containers.c:303
d_err_t docker_container_changes(docker_context *ctx, docker_changes_list **changes, char *id)
Definition: docker_containers.c:323
void() docker_log_line_handler(void *handler_args, int stream_id, int line_num, char *line)
function type for handling log lines received from the get logs api call.
Definition: docker_containers.h:656
docker_ctr * docker_inspect_container(docker_context *ctx, char *id, int size)
Inspect the docker container given by the id.
Definition: docker_containers.c:150
json_object docker_ctr_list
Docker Container List json object.
Definition: docker_containers.h:230
d_err_t docker_container_logs(docker_context *ctx, char **log, size_t *log_length, char *id, int follow, int std_out, int std_err, long since, long until, int timestamps, int tail)
Get the logs for the docker container.
Definition: docker_containers.c:176
void free_docker_container_change(docker_container_change *item)
Free the docker container change item.
Definition: docker_containers.c:294
json_object docker_ctr
Docker Container json object.
Definition: docker_containers.h:394
json_object docker_ctr_ls_item
Docker Container list item json object.
Definition: docker_containers.h:80
d_err_t docker_container_logs_foreach(void *handler_args, char *log, size_t log_length, docker_log_line_handler *line_handler)
Iterate the log lines received from the logs call.
Definition: docker_containers.c:237
d_err_t docker_container_get_stats(docker_context *ctx, docker_container_stats **stats, char *id)
Get stats from a running container. (the non-streaming version)
Definition: docker_containers.c:360
json_object docker_container_stats
Docker Container Stats object.
Definition: docker_containers.h:1018
float docker_container_stats_get_cpu_usage_percent(docker_container_stats *stats)
Get the cpu usage percentage from the docker container stats.
Definition: docker_containers.c:414
d_err_t docker_unpause_container(docker_context *ctx, char *id)
Unpause a container.
Definition: docker_containers.c:593
json_object docker_percpu_usage_list
Docker PerCPU usage list json object.
Definition: docker_containers.h:761
json_object docker_container_cpu_stats
Docker Container CPU stats json object.
Definition: docker_containers.h:863
d_err_t docker_container_list_filter_str(docker_context *ctx, docker_ctr_list **container_list, int all, int limit, int size, const char *filters)
Definition: docker_containers.c:87
json_object docker_ctr_process_details
Docker container process list object.
Definition: docker_containers.h:539
d_err_t docker_container_list(docker_context *ctx, docker_ctr_list **container_list, int all, int limit, int size,...)
Definition: docker_containers.c:39
arraylist docker_changes_list
Docker Changes list json object.
Definition: docker_containers.h:708
d_err_t docker_rename_container(docker_context *ctx, char *id, char *name)
Rename a container.
Definition: docker_containers.c:544
d_err_t docker_stop_container(docker_context *ctx, char *id, int t)
Stop a container.
Definition: docker_containers.c:451
d_err_t make_docker_container_change(docker_container_change **item, const char *path, const char *kind)
Create a new container change item.
Definition: docker_containers.c:272
d_err_t docker_restart_container(docker_context *ctx, char *id, int t)
Restart a container.
Definition: docker_containers.c:486
json_object docker_ctr_create_params
Docker Container Creation Parameters json object.
Definition: docker_containers.h:287
d_err_t docker_process_list_container(docker_context *ctx, docker_ctr_ps **ps, char *id, char *process_args)
List all processes in a container identified by id.
Definition: docker_containers.c:163
d_err_t docker_remove_container(docker_context *ctx, char *id, int v, int force, int link)
Remove a container.
Definition: docker_containers.c:634
docker_container_change * docker_changes_list_get_idx(docker_changes_list *list, int i)
Get the ith item from the docker changes list.
Definition: docker_containers.c:314
d_err_t docker_pause_container(docker_context *ctx, char *id)
Pause a container.
Definition: docker_containers.c:572
d_err_t docker_start_container(docker_context *ctx, char *id, char *detachKeys)
Start a container.
Definition: docker_containers.c:431
json_object docker_ctr_ps
Docker containers process details object.
Definition: docker_containers.h:561
json_object docker_ctr_port
Docker Container Port json object.
Definition: docker_containers.h:44
size_t docker_changes_list_length(docker_changes_list *list)
Get the length of the docker changes list.
Definition: docker_containers.c:319
Docker Results.
enum _config_error d_err_t
Definition: docker_result.h:60
Docker API utils.
Docker container change struct.
Definition: docker_containers.h:682
change_kind kind
change type for the entry
Definition: docker_containers.h:684
char * path
path which is changed
Definition: docker_containers.h:683
A docker context for a specific docker server.
Definition: docker_connection_util.h:141