How to get containers list via rancher REST API

This Rest api tutorial help to access all containers list using rancher API. Rancher Labs develops open source software that makes it easy to deploy and manage Docker containers and Kubernetes in production on any infrastructure.Rancher is complete container management platform.Rancher provides infrastructure services such as multi-host networking, global and local load balancing, and volume snapshots.

You can manage docker containers, services and organisation easily using Rancher Rest service.You can get more information from official Rancher rest service docs.

What is Rancher

Rancher is an open source software platform that enables organizations to run containers in production. With Rancher, organizations no longer have to build a container services platform from scratch using a distinct set of open source technologies. Rancher supplies the entire software stack needed to manage containers in production.You can get more information from here.

Get Rancher Containers List Using Rancher Api

I am using guzzle rest client to access containers list from rancher host using rest API. We need to call rest api end point with rancher API authentication username and password. We will create client object using like below,


require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$token = base64_encode('username': 'password');

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'restapi url', //https://api.domain.com/v1/
    // You can set any number of default request options.
    'timeout'  => 2.0,
	'headers' => ['Authorization' => "Basic " . $token, 'Content-Type' => 'application/json', "Accept" => "application/json"],
    //ssl false
    'verify' => false
]); 

Please change token string as per your REST API credentials.The parameters are:

  • $token : This variable will contains combination of username and password string of rancher rest api
  • base_uri : The Rancher API host api url path
  • headers : This will contains request header parameters
  • timeout : Request timeout in sec.
  • verify : SSL verification false

How to Fetch Rancher Containers list using Rancher API

We will create Rancher rest client using authentication string, we will use Rancher Rest API end points which will use to get rancher containers informations. I am using GET HTTP request to access Rancher API.This rest call will return json object of rancher containers including running, stopped etc.

require_once 'vendor/autoload.php';
use GuzzleHttp\Client;

$response = $client->get('projects/12sq/containers');
echo "
";
echo $response->getBody()->getContents();

Where as : 12sq : is the rancher organisation environment id.

I hope its help you access Rancher Rest API using Guzzle rest client.