Trabalhando com API parte I

Olá pessoal!

Hoje vamos mostrar como realizar as operações mais básicas do Docker através da API dele. Vamos ver como iniciar, parar, criar, excluir e coletar informações de containers através da API. Estarei utilizando a distribuição CentOS 7 em meu host onde vou criar os containers.

– Como habilito a API?

vim /etc/systemd/system/docker.service 

//Vamos alterar a linha que contém ExecStart=/usr/local/bin/docker -d -H fd:// por essa

ExecStart=/usr/local/bin/docker -d -H fd:// -H 0.0.0.0:4243

service docker restart

Agora estamos com nossa API habilitada e escutando na porta 4243.

– Iniciar

curl -X POST http://IPHost:4243/containers/IDContainer/start

– Parar

curl -X POST http://IPHost:4243/containers/IDContainer/stop

– Excluir

curl -X DELETE http://IPHost:4243/containers/IDContainer

– Criar

curl -X POST -H "Content-Type: application/json" http://IpHost:4243/containers/create -d '{
"Hostname":"",
   "User":"",
   "Memory":0,
   "MemorySwap":0,
   "AttachStdin":false,
   "AttachStdout":true,
   "AttachStderr":true,
   "PortSpecs":null,
   "Privileged":false,
   "Tty":false,
   "OpenStdin":false,
   "StdinOnce":false,
   "Env":null,
   "Dns":null,
   "Image":"busybox",
   "WorkingDir":""
}'

– Renomear

curl -X GET http://IPHost:4243/containers/IDContainer/rename?name=novo_nome

– Exibir containers que estão iniciado

curl -X GET http://IPHost:4243/containers/json

– Exibir todos os containers

curl -X GET http://IPHost:4243/containers/json?all=1

– Top

curl -X GET http://IPHost:4243/containers/IDContainer/top

– Stats

curl -X GET http://IPHost:4243/containers/IDContainer/stats

– Images

curl -X GET http://IPHost:4243/images/json?all=0

Por hoje era isso, fiquem atentos para as novidades e ajude divulgando o mundodocker.com.br!