DavesCodeMusings / container-central

A minimalist, not quite ready for prime time solution to managing Docker containers.
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Docker Exec command dictionary for common tasks #8

Closed DavesCodeMusings closed 2 years ago

DavesCodeMusings commented 2 years ago

The Docker API accepts commands much like docker exec on command-line. (See below.)

It would be nice to have a set of common commands that can be run by clicking/tapping, rather than typing. For example: service nginx configtest and service nginx reload. If the list of commands could be filtered based on container name that would be even better. For example, if the container name has 'nginx' in it, show the two service nginx ... commands. If nginx is not in the container name, filter them out.

Maybe like this:

quick-commands.json

[
  {
    "name": "Validate nginx config",
    "cmd": [ "service", "nginx", "configtest" ],
    "filter": "nginx"
  },
  {
    "name": "Restart nginx",
    "cmd": [ "service", "nginx", "reload" ],
    "filter": "nginx"
  }
]

Some cURL examples:

# Get a list of container IDs.
curl --unix-socket /var/run/docker.sock http://localhost/containers/json | jq .[].Id

# Choose one of the IDs returned. It will be referred to as ${CONTAINER_ID} in the next command.

# Create an exec instance, passing the command and any arguments. 
curl --unix-socket /var/run/docker.sock -X POST http://localhost/containers/${CONTAINER_ID}/exec -H 'Content-Type: application/json' --data '{ "Tty": true, "AttachStdout": true
, "Cmd": [ "date", "--iso-8601=minutes" ] }' | jq .Id

# Note the ID returned. It will be referred to as ${EXEC_ID} in the next command.

# Run the exec instance.
curl --unix-socket /var/run/docker.sock -X POST ${EXEC_ID}/start -H 'Content-Type: application/json' --data '{ "Detach": false, "Tty": true }'

# View the output from stdout.
2022-01-02T19:37+00:00
DavesCodeMusings commented 2 years ago

Implemented quick commands that can be defined in data/quick_commands.json with a format of:

[
  {
    "id": "983bbf9f-3c71-4814-ac01-e6cf80c9cf0f",
    "cmd": "service nginx configtest",
    "filter": "nginx",
    "tooltip": "Validate configuration"
  },
  {
    "id": "c251bb37-a9b1-4392-97be-d9652b6b15af",
    "cmd": "service nginx reload",
    "filter": "nginx",
    "tooltip": "Apply configuration"
  }
]

Where id is a random uuid, cmd is the shell command, filter ensures the command only appears for containers with this string in the name, and tooltip is the text shown when a user hovers the mouse over the command link.