API usage examples
Portainer exposes an HTTP API that you can use to automate everything you do via the Portainer UI. You can also use Portainer as a gateway (HTTP queries against the Portainer API) to the underlying Docker/Kubernetes API.
The following examples use httpie to execute API calls against Portainer.
Initialize the admin password
On a fresh install of Portainer, you need to create an admin account to initialize Portainer. You will be asked for this when you visit the Portainer URL for the first time. You can achieve the same outcome using this API call:
Authenticate against the API using the admin account
The response is a JSON object containing the JWT token inside the jwt
field. 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>
:
This token is valid for 8 hours. Once it expires, you will need to generate another token to execute authenticated queries.
Adding a new environment
On a fresh install, Portainer has no environments configured. You will first need to add an environment for Portainer to manage.
You can add an environment to manage via the Portainer API, or via the web interface both during the initial setup and after setup is complete.
Execute Docker queries against a specific environment
The Portainer HTTP API endpoint acts as a reverse-proxy to the Docker HTTP API and can be used to execute any of the Docker HTTP API requests:
/api/endpoints/<ENVIRONMENT_ID>/docker
Read Docker's API documentation to learn how to query the Docker Engine.
List all containers
This call lists all of the containers available in a specific environment:
The response is identical to that returned by the ContainerList
operation of the Docker API. See Docker's documentation about this operation.
Create a container
You can create a container in a specific environment using the Portainer HTTP API as a gateway. The following query will create a new Docker container inside the environment using ID 1. The container will be named web01
and will use the nginx:latest
Docker image. It will publish container port 80
on port 8080
on the host.
The response is identical to that returned by the ContainerCreate
operation of the Docker API. See Docker's documentation about this operation.
Here is an example response:
You will need the container ID in order to execute actions against that container.
Start a container
Using the ID you retrieved previously, you can start your new container using this endpoint:
/api/endpoints/<ENVIRONMENT_ID>/docker/containers/<CONTAINER_ID>/start
The response is identical to that returned by the ContainerStart
operation of the Docker API. See Docker's documentation about this operation.
Delete a container
You can create a container using the endpoint /api/endpoints/<ENVIRONMENT_ID>/docker/containers/
:
The response is identical to that returned by the ContainerDelete
operation of the Docker API. See Docker's documentation about this operation.