How to use docker remote api to create container? - docker

I'm new to docker. I have read the tutorial in docker remote API . In aspect of creating container. It show me too many param to fill. I want to know what is equivalent to this command :
docker run -d -p 5000:5000 --restart=always --name registry
registry:2.
I have no idea about it. Can anyone tell me? Thanks!

Original answer (July 2015):
That would be (not tested directly), as in this tutorial (provided the remote API is enabled):
First create the container:
curl -v -X POST -H "Content-Type: application/json" -d '{"Image": " registry:2.",}' http://localhost:2376/containers/create?name=registry
Then start it:
curl -v -X POST -H "Content-Type: application/json" -d '{"PortBindings": { "5000/tcp": [{ "HostPort": "5000" }] },"RestartPolicy": { "Name": "always",},}' http://localhost:2376/containers/registry/start?name=registry
Update February 2017, for docker 1.13+ see rocksteady's answer, using a similar idea but with the current engine/api/v1.26.

More or less just copying VonCs answer in order to update to todays version of docker (1.13) and docker remote api version (v1.26).
What is different:
All the configuration needs to be done when the container is created, otherwise the following error message is returned when starting the container the way VonC did.
{"message":"starting container with non-empty request body was deprecated since v1.10 and removed in v1.12"}
First create the container: (including all the configuration)
curl -v -X POST -H "Content-Type: application/json" -d #docker.conf http://localhost:2376/containers/create?name=registry
The file docker.conf looks like this:
{
"Image": registry:2.",
"ExposedPorts": {
"5000/tcp": {}
},
"HostConfig": {
"PortBindings": {
"5000/tcp": [
{
"HostPort": "5000"
}
]
},
"RestartPolicy": {
"Name": "always"
}
"AutoRemove": true
}
}
Then start it: (the parameter name is not necessary, the container is just named registry)
curl -v -X POST -H "Content-Type: application/json" http://localhost:2376/containers/registry/start

Create docker container in Docker Engine v1.24
Execute the post request -
curl -X POST -H "Content-Type: application/json" http://DOCKER_SERVER_HOST:DOCKER_PORT/v1.24/containers/create?name=containername
In the request body, you can specify the JSON parameters like
{
"Hostname": "172.x.x.x",
"Image": "docker-image-name",
"Volumes": "",
"Entrypoint": "",
"Tty": true
}
It creates your docker container
Start the container
Execute the POST request
curl -X POST http://DOCKER_SERVER_HOST:DOCKER_PORT/v1.24/containers/containername/start
Reference link - https://docs.docker.com/engine/api/v1.24/

Related

call bitcoind rpc api over docker

I started my node container with this flags:
daemon=1
printtoconsole=1
testnet=1
rpcport=9332
rpcallowip=0.0.0.0/0
rpcuser=user
rpcpassword=password
rpcbind=0.0.0.0
server=1
I opened port in my docker-compose :
node:
image: bitcoin-sv
container_name: 'node'
restart: always
ports:
- '9332:9332'
I can call methods from bitcoin-cli in my container
docker exec -it node bash
root#9196d074e4d8:/opt/bitcoin-sv# ./bitcoin-cli getinfo
But I cannot call it from curl
curl --user user --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo, "params": ["", 0.1, "donation", "seans outpost"] }' -H 'content-type: text/plain;' http://127.0.0.1:9332
Enter host password for user 'user':
curl: (52) Empty reply from server
How can i call it from curl? Maybe i have to call to cli?
not sure what can be your issue, but the first approach would be do the curl inside the container in order to verify that the HTTP interface is working properly. So you should try this:
docker exec -it node bash
root#9196d074e4d8:/opt/bitcoin-sv# curl --user user --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo, "params": ["", 0.1, "donation", "seans outpost"] }' -H 'content-type: text/plain;' localhost:9332
Once you are sure the interface is working inside the container, you can move forwred and try it from the host.

Docker API push to private registry error

I can't manage to push an image to a private registry using the docker API. I have read everything I found everywhere and tried everything with no luck...
I tried :
curl -X POST -H "X-Registry-Auth:XXXXXXXXXXXXXXX" http://dockerapiurl:2375/images/registryurl/python/push?tag=6
OR
curl -X POST -H 'X-Registry-Auth:{"username": "xxxxxx","password": "xxxxx", "serveraddress": "xxxx.url.net", "auth": ""}' http://dockerapiurl:2375/images/registryurl/python/push?tag=6
I always get the same error :
{"errorDetail":{"message":"errors:\ndenied: requested access to the resource is denied\nunauthorized: authentication required\n"},"error":"errors:\ndenied: requested access to the resource is denied\nunauthorized: authentication required\n"}
If I use docker push in CLI mode everything works, what am I doing wrong?
Thanks!!
it needs to be encoded in base 64, try this
XRA=`echo "{ \"username\": \"xxxxxx\", \"password\": \"xxxxxx\", \"email\": \"youmail#example.org\", \"serveraddress\": \"xxxxxx\" }" | base64 --wrap=0`
curl -X POST -d "" -H "X-Registry-Auth: $XRA" http://dockerapiurl:2375/images/registryurl/python/push?tag=6
end result should look like this
curl -X POST -d "" -H "X-Registry-Auth: eyAidXNlcm5hbWUiOiAieHh4eHh4IiwgInBhc3N3b3JkIjogInh4eHh4eCIsICJlbWFpbCI6ICJ5b3VtYWlsQGV4YW1wbGUub3JnIiB9Cg==" http://dockerapiurl:2375/images/registryurl/python/push?tag=6

Curl call failling when made with chai

I have an endpoint inside a docker container. When I call the endpoint from postman it works as intended. When I call it from node.fetch from another container it also works but when I call it from another docker container using chai, I get the following error:
curl: (7) Couldn't connect to server
The chai call works well, since I can see the correct logs. How ever when I get to the following line of code it doesn't behave as expected.
This is the chai code:
res = await chai
.request(server)
.post('/bot_manager/management/initiliaze')
.set('content-type', 'application/json')
.send(body)
This is the line where it fails:
exec(`curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -d '{ "Image": "strategy_baseline", "ExposedPorts": { "${PORT}/tcp": {} }, "HostConfig": { "Binds": ["utils:/usr/src/app/utils:delegated","${process.env.CURRENT_PATH}/src/bot_manager/api/strategies/${botId}.js:/usr/src/app/strategies/${botId}.js"], "NetworkMode": "goatFish_backend", "PortBindings": { "${PORT}/tcp": [{ "HostPort": "${PORT}" }]}}, "Env": ["BOTNAME=${botId}", "PORT=${PORT}", "PAIR=${PAIR}"]}' -X POST http:/v1.4/containers/create?name=${botId}`, (err, stdout, stderr) => {// empty}
This works well in every method except chai. I also use the exact sam input through out the different methods.
Any help or indications are much appreciated. TIA!!

Icinga: How to enable maintenance mode through remote api or tool?

I am using Icinga Version 2.4.2 to monitor services on several hosts. I would like to be able to place certain hosts in maintenance mode for a set amount of time using a cli tool or rest API instead of the Web UI.
Is this possible and if so what tool/api should I use?
If I cannot do this through a remote tool/api what command should I use on the server or client to place clients in maintenance mode?
Update: It seems like the rest api has a solution. This set of permissions works:
object ApiUser "root" {
password = "foobar"
permissions = [ "console", "objects/query/Host", "objects/query/Service", "actions/schedule-downtime", "actions/remove-downtime"]
}
Then the following allows me to make and remove downtimes:
curl -k -s -u root:foobar -H 'Accept: application/json' -X POST "https://localhost:5665/v1/actions/schedule-downtime?filter=host.name==%22${TARGET}%22&type=Host" -d '{ "start_time": "1528239116", "end_time": "1528325561", "duration": 1000, "author": "root", "comment": "downtime on $TARGET" }' | jq .
curl -k -s -u root:foobar -H 'Accept: application/json' -X POST "https://localhost:5665/v1/actions/remove-downtime?filter=host.name==%22${TARGET}%22&type=Host" | jq .
Right now the only issue with this I am having is how to pass in variables for the start and stop dates. Attempting this keeps resulting in the following error:
{
"status": "Invalid request body: Error: lexical error: invalid char in json text.\n { \"start_time\": $current_time,\n (right here) ------^\n\n",
"error": 400
}

How to get Rancher scripts code when add agent to nodes hosts?

Normally, get that code on master host's dashboard:
$ sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.2 http://192.168.0.100:8080/v1/scripts/5D8B3FD489C00C7F361A:2483142400000:WvMClyNFLXQnT9pLuii3D0sYA
If want to deploy multiple nodes automatic to other hosts, it's necessary to get this code from master:
5D8B3FD489C00C7F361A:2483142400000:WvMClyNFLXQnT9pLuii3D0sYA
Then every node just add agent with this code is good. Is it right?
But, how to get it by cli from master?
Rancher has API, which enables you to interact with it remotely. What you require is called registrationTokens. Now, how to access them.
First, set up API tokens in your Rancher. Go to API -> Keys -> Add Account API Key and create the keys. If you can't find the buttons, your URL would be 192.168.0.100:8080/env/1a5/api/keys.
Now you know the keys and from remote host you can do something like this:
curl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
-X GET \
'http://192.168.0.100:8080/v2-beta/projects/1a5/registrationtokens'
Your result will be a JSON with required data:
{
...
"data": [
{
"id": "1c3",
"type": "registrationToken",
"links": {
...
},
"actions": {
...
},
...
"command": "sudo docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.2 http://192.168.0.100:8080/v1/scripts/AAAAAAAAAAAAAAAAAAAA:0000000000000:ZZZZZZZZZZZZZZZZZZZZZZZZZZ",
...
}],
...
}

Resources