Skip to content

Instantly share code, notes, and snippets.

@deviantony
Last active September 4, 2024 02:41
Show Gist options
  • Save deviantony/77026d402366b4b43fa5918d41bc42f8 to your computer and use it in GitHub Desktop.
Save deviantony/77026d402366b4b43fa5918d41bc42f8 to your computer and use it in GitHub Desktop.
Portainer HTTP API by example

DEPRECATION NOTICE

This gist is now deprecated in favor of our official documentation: https://documentation.portainer.io/api/api-examples/ which contains up to date examples!

THE FOLLOWING DOCUMENTATION IS DEPRECATED

Please refer to the link above to get access to our updated API documentation and examples.

Introduction

This document presents a simple way to manage your Docker resource by using Portainer as a gateway (HTTP queries against the Portainer API).

The API documentation is available here: https://app.swaggerhub.com/apis/deviantony/portainer/

WARNING: This documentation is valid for Portainer >= 1.18.0.

NOTE: I'm using httpie to execute HTTP queries from the CLI.

Deploy a Portainer instance

# Note: I'm bind-mouting the Docker socket to be able to manage the local engine where Portainer is running.
# You can skip the bind-mount if you want to manage a remote environment.

$ docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer

Initialize the admin password

$ http POST :9000/api/users/admin/init Username="admin" Password="adminpassword"

Authenticate against the API using the admin account

$ http POST :9000/api/auth Username="admin" Password="adminpassword"

The response is a JSON object containing the JWT token inside the jwt field:

{
  "jwt":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE"
}

You need to retrieve this token. You will need to pass this token inside the Authorization header when executing an authentication query against the API.

The value of the Authorization header must be of the form Bearer <JWT_TOKEN>.

Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE

NOTE: This token has a 8 hour validity, you'll need to generate another token to execute authenticated queries once this one expires.

Create a new endpoint

Here, I'll show how to create 3 different types of endpoints:

  • Local endpoint using Docker socket communication
  • Remote endpoint using TCP communication
  • Remote endpoint using TCP communication secured via TLS

Local endpoint via the Docker socket

This query will create an endpoint called test-local and will use the Docker socket to communicate with this environment.

NOTE: This example requires to you bind-mount the Docker socket when running Portainer.

$ http --form POST :9000/api/endpoints \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
Name="test-local" EndpointType=1

The response is a JSON object representing the endpoint:

{
    "AuthorizedTeams": [], 
    "AuthorizedUsers": [], 
    "Extensions": [], 
    "GroupId": 1, 
    "Id": 1, 
    "Name": "test-local", 
    "PublicURL": "",
    "Type": 1,
    "TLSConfig": {
        "TLS": false, 
        "TLSSkipVerify": false
    }, 
    "Type": 1, 
    "URL": "unix:///var/run/docker.sock"
}

Retrieve the value of the Id property, it will be used to execute queries against the Docker engine for that endpoint.

Remote endpoint

This query will create an endpoint called test-remote and will communicate with this environment over TCP using the IP address 10.0.7.10 and port 2375 (these are example values, ensure that you're using the correct IP & port).

NOTE: The Docker API must be exposed on that IP address & port. Please refer to the Docker documentation to check how to configure this.

$ http --form POST :9000/api/endpoints \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
Name="test-remote" URL="tcp://10.0.7.10:2375" EndpointType=1

The response is a JSON object representing the endpoint:

{
    "AuthorizedTeams": [], 
    "AuthorizedUsers": [], 
    "Extensions": [], 
    "GroupId": 1, 
    "Id": 1, 
    "Type": 1,
    "Name": "test-remote", 
    "PublicURL": "", 
    "TLSConfig": {
        "TLS": false, 
        "TLSSkipVerify": false
    }, 
    "Type": 1, 
    "URL": "tcp://10.0.7.10:2375"
}

Retrieve the value of the Id property, it will be used to execute queries against the Docker engine for that endpoint.

Remote endpoint secured using TLS

This query will create an endpoint called test-remote-tls and will communicate with this environment over TCP (secured with TLS) using the IP address 10.0.7.10 and port 2376 (these are example values, ensure that you're using the correct IP & port).

NOTE: The Docker API must be exposed on that IP address & port. Please refer to the Docker documentation to check how to configure this.

$ http --form POST :9000/api/endpoints \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
Name="test-remote" URL="tcp://10.0.7.10:2376" EndpointType=1 TLS="true" TLSCACertFile@/path/to/ca.pem TLSCertFile@/path/to/cert.pem TLSKeyFile@/path/to/key.pem

The response is a JSON object representing the endpoint:

{
    "AuthorizedTeams": [], 
    "AuthorizedUsers": [], 
    "Extensions": [], 
    "GroupId": 1, 
    "Id": 1, 
    "Type": 1,
    "Name": "test-remote", 
    "PublicURL": "", 
    "TLSConfig": {
        "TLS": true, 
        "TLSCACert": "/data/tls/1/ca.pem", 
        "TLSCert": "/data/tls/1/cert.pem", 
        "TLSKey": "/data/tls/1/key.pem", 
        "TLSSkipVerify": false
    }, 
    "Type": 1, 
    "URL": "tcp://10.0.7.10:2376"
}

Retrieve this ID, it will be used to execute queries against the Docker engine for that endpoint.

Execute Docker queries against a specific endpoint

By using the following Portainer HTTP API endpoint /api/endpoints/<ENDPOINT_ID>/docker, you can now execute any of the Docker HTTP API requests.

This Portainer HTTP API endpoint acts as a reverse-proxy to the Docker HTTP API.

NOTE: You can refer to the Docker API documentation to get more information on how you can query the Docker engine.

As an example, here is how you can list all the containers available in a specific endpoint:

$ http GET :9000/api/endpoints/1/docker/containers/json \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
all==true

The response is exactly the same as returned by the ContainerList operation of the Docker API, see the documentation for the ContainerList operation.

Create a container

Here is how you can create a container in a specific endpoint using the Portainer HTTP API as a gateway.

This query will create a new Docker container inside the endpoint using the ID 1. The container will be named web01, use the nginx:latest Docker image and publish the container port 80 on via the 8080 port on the host.

See the link below to retrieve more information on how you can create a container using the Docker HTTP API.

$ http POST :9000/api/endpoints/1/docker/containers/create \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
name=="web01" Image="nginx:latest" \
ExposedPorts:='{ "80/tcp": {} }' \
HostConfig:='{ "PortBindings": { "80/tcp": [{ "HostPort": "8080" }] } }'

The response is exactly the same as returned by the ContainerCreate operation of the Docker API, see the documentation for the ContainerCreate operation.

Example response:

{
    "Id": "5fc2a93d7a3d426a1c3937436697fc5e5343cc375226f6110283200bede3b107",
    "Warnings": null
}

Retrieve the ID of the container, you will need it to execute actions against that container.

Start a container

You can now start the container that you previously created using the endpoint /api/endpoints/<ENDPOINT_ID>/docker/containers/<CONTAINER_ID>/start (ensure you retrieved the ID of the container created previsouly):

$ http POST :9000/api/endpoints/1/docker/containers/5fc2a93d7a3d426a1c3937436697fc5e5343cc375226f6110283200bede3b107/start \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE"

The response is exactly the same as returned by the ContainerStart operation of the Docker API, see the documentation for the ContainerStart operation.

Delete a container

You can create a container using the following endpoint /api/endpoints/<ENDPOINT_ID>/docker/containers/<CONTAINER_ID>/remove:

$ http DELETE :9000/api/endpoints/1/docker/containers/5fc2a93d7a3d426a1c3937436697fc5e5343cc375226f6110283200bede3b107 \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE" \
force==true

The response is exactly the same as returned by the ContainerDelete operation of the Docker API, see the documentation for the ContainerDelete operation.

Manage Docker stacks in a Swarm environment

By using the following Portainer HTTP API endpoint /api/stacks, you can deploy and remove stack inside a specific Docker environment (Portainer endpoint).

In the following instructions, I'll assume that the endpoint using the ID 1 is connected to a Swarm cluster.

Before trying to create any stack, you need to retrieve the identifier of your Swarm cluster. It will be used to create stacks in the next steps.

$ http GET :9000/api/endpoints/1/docker/swarm \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE"

In the response of this query, you'll find the identifier of the Swarm cluster inside the ID property:

{
  "CreatedAt": "2018-06-05T15:03:30.226627346Z", 
  "ID": "k427pd86wfkgp40ksrmbojrwq",
}

Deploy a new stack from a public Git repository

This query will create a new stack inside the endpoint using the ID 1. The stack will be named myTestStack, use the Stack file stored in the public Git repository https://github.com/portainer/templates under the path stacks/cockroachdb/docker-stack.yml.

$ http POST ':9000/api/stacks?method=repository&type=1&endpointId=1' \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUyOTY3NDE1Mn0.wyyck_iktvOrYsuH0Xrvgifoi83emsszipSTYkDvuaE" \
Name="myTestStack" \
SwarmID="k427pd86wfkgp40ksrmbojrwq" \
RepositoryURL="https://github.com/portainer/templates" \
ComposeFilePathInRepository="stacks/cockroachdb/docker-stack.yml"

The response is a JSON object representing the newly created stack:

{
    "EndpointId": 1, 
    "EntryPoint": "stacks/cockroachdb/docker-stack.yml", 
    "Env": null, 
    "Id": 1, 
    "Name": "myTestStack", 
    "ProjectPath": "/data/compose/\u0002", 
    "SwarmId": "tdv7rl1u3965ml272o2q6d96z", 
    "Type": 1
}

Retrieve the stack identifier in the Id property, you'll need it to manage the stack.

Remove an existing stack

This query will remove the existing stack with the identifier 1 inside the endpoint using the ID 1.

$ http DELETE :9000/api/stacks/1?endpointId=1 \
"Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE"
@McTristan
Copy link

McTristan commented Apr 22, 2021

How would I update a service member access / owner access when the stack has not been created by portainer? The UI, though it says that managing is limited, allows changes to the access settings. Another option would be to simply inherit the settings from the endpoint - so a user which access to the endpoint could also access all stacks and services within this endpoint.

In my case I'm creating stacks via terraform and the only feature I'm missing is the access control. Each time we redeploy I have to assign the members again to each service. If there is no inheritance functionality in portainer I thought I might be able to automate this via the portainer api.

@deviantony
Copy link
Author

@McTristan as the stack was created outside of Portainer we don't have much control over it so you won't be able to set access control on the stack itself to be inherited by all the components of the stack.

What you can do is set access control on each component of the stacks via the use of the Portainer API and the resource_controls operations.

Here is an example of request with payload you could send to set the access control on a service to a specific user:

POST /api/resource_controls

{
  AdministratorsOnly: false
  Public: false
  ResourceID: "m6nacjain1sb2n5ygmr8tudcd" (replace with the ID of the Docker Swarm service)
  SubResourceIds: []
  Teams: []
  Type: "service"
  Users: [2] (replace with the ID or IDs of the targeted user(s))
}

@McTristan
Copy link

McTristan commented Apr 23, 2021

Thank you!
Sounds like a way to go - however I have some problems understanding the meaning of the property ResourceID.

We have several endpoints, most of them are Edge-Endpoints (which are deployed via terraform with an edge agent connecting to the main portainer instance). I can query them with /api/endpoints and a filter - it will return the correct endpoints.

However there seems to be no way of querying the services for (such?) an endpoint with the API thus I don't really know the ID of the docker swarm service or am I'm missing something here?

@deviantony
Copy link
Author

@McTristan indeed you will need the service identifier (as provided by Docker Swarm) if you want to apply a resource control on a Docker resource.

This can be retrieved through the UI by inspecting the resource and retrieving the ID from the URL but it can also be done via API, although to do so you will need to use the Docker API.

See https://docs.docker.com/engine/api/v1.41/#operation/ServiceList

This requires you to send a GET request to /services, you can access the Docker API through Portainer via the following API endpoint:

/api/endpoints/<ENDPOINT_ID>/docker (replace <ENDPOINT_ID> with the actual ID of the endpoint that you want to query).

Ultimately, you'll want to query something like this:

GET <PORTAINER_URL>/api/endpoints/1/docker/services

That should return the Swarm services associated to that endpoint.

@McTristan
Copy link

Excellent answer, thank you. I can successfully fetch the services now and hope to automate the rest via httpie / jq magic. Let's see.

@McTristan
Copy link

it works!

@greenmind-sec
Copy link

Hello friends, i am build one integration using python for auth.
My problem is for create the container.

import requests

argumentos = {
    'Username':'admin',
    'Password':'MyPasswordAdmin'
}
url='http://127.0.0.1:9000/api/auth'

x = requests.post(url,json=argumentos)
print(x.status_code)
print(x.json())

@deviantony
Copy link
Author

@greenmind-sec what's your problem?

@MisderGAO
Copy link

MisderGAO commented May 19, 2021

Hello,
the field stackFileContent is strange, the example of API doc does not work.
how can I convert a yaml/json file to set this field?
or can you provide another field in 'PUT /stacks​/{id}' so that we can update a stack via a yaml file (eg, from git )?

@denzuko
Copy link

denzuko commented May 21, 2021

@deviantony sounds like @greenmind-sec's issue is English is not his first language so reading the docs might not be easy for him.

@greenmind-sec
Try adding pyswagger into your code and keep in mind that once your authenticated one can then talk to the underlining docker (swarm and engine APIs)[ https://docs.docker.com/engine/api/v1.30/#operation/ContainerCreate] under the /endpoints/{id}/docker Portainer API endpoint which is a pass through end point from portainer to the container's docker socket.

@deviantony
Copy link
Author

Just an update for everyone following this feed, the work on our side to make it up to date and put this inside our documentation has made some progress and I'll keep you posted here when it is available!

@lmjlopes
Copy link

lmjlopes commented May 23, 2021

Hi all,

I've successfully build some python code to start and stop containers using this API. But I've struggling without success to send commands to a running container. I didn't find anywhere an example to help me.
I'm trying to emulate in Python the behaviour of the Portainer page "Container Console", selecting "Use custom command".
Am I doing something wrong, missing something or is it not even possible?

`def get_container_exec(self):

    r=requests.post(self.portainer_endpoint+"/endpoints/2/docker/containers/" + self.portainer_container + "/exec", 
        headers={
            "Authorization": "Bearer {}".format(self.token),
            "Content-Type": "application/json"},
        json = {
            "Id": self.portainer_container,
            "AttachStderr": True,
            "AttachStdin": True,
            "AttachStdout": True,
            "Tty": True,
            "Cmd": ["python3", "test.py"]},
        verify=self.verifySSL,
        timeout = 5)`

@deviantony
Copy link
Author

@lmjopes container exec will be harder to automate, it is using the websocket protocol not classic REST HTTP API.

@lmjlopes
Copy link

@deviantony
Based on your response I rerouted this in another way. I managed sucessfully to do what I needed ( start, stop and send commands to a running container) using another container to act as a gateway, based on the container from "philhawthorne/ha-dockermon".
I was trying to make it simple and use just what I already had, Portainer.
Thanks anyway for your reply.

@filosof86
Copy link

filosof86 commented Jun 1, 2021

Hi @deviantony

Retrieving the user info (getting the list or the user's info), I do not see his password although the password field is described as the one which has to be returned https://app.swaggerhub.com/apis/deviantony/Portainer/2.0.1#/users/UserList

Parsed response

$VAR1 = { 
          'success' => 1,
          'response' => { 
                          'PortainerAuthorizations' => undef,
                          'Role' => 2,
                          'Id' => 2,
                          'EndpointAuthorizations' => undef,
                          'Username' => 'filosof'
                        },
          'return_code' => 200
        };

I just wanted to implement auto-login into the Portainer WI previously comparing the predefined list of the user names with the list defined in Portainer and if some username exists - take his credentials to log in.

Any chance for me to do that?

Thanks!

@deviantony
Copy link
Author

DEPRECATION NOTICE

This gist is now deprecated in favor of our official documentation: https://documentation.portainer.io/api/api-examples/ which contains up to date examples!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment