I can make cUrl calls to Docker API but if I run the same script from a container (docker-compose), I can't reach any API endpoint.
My requests looks like :
$ curl --unix-socket /var/run/docker.sock -X POST http:/localhost/containers/json
The reason could be the use of localhost but I can't find the solution for now. Any suggestions?
EDIT : Here's a brief summary of the stack i'm using (Api-Platform https://api-platform.com/). Every container is attached to an "api_default" bridge network :
- php = Symfony based application
- nginx = NGINX web server
- psql = Hosts the main database
- mssql = Used to sync datas
- ...
I do have a shell script in my "php" container which is meant to reach the Docker API. To summarize, I request the "mssql" container to sync with an external service and then I ask to "psql" container to copy those datas (export/import done with csv files through shared volumes).
The problem is :
- If I do
docker-compose exec php bin/console app:sync
(Symfony command which runs the script), everything works fine : the containers communicate through docker.sock with the Docker API and I can sync my databases. - But if I run the same script with an api endpoint (localhost:8080/api/sync) or via a VueJS app (Axios), the Docker API never return any response.
Below is a part of my script :
curl -XPOST -H 'Content-Type: application/json' \
--unix-socket /var/run/docker.sock http:/localhost/containers/mssql/exec \
-d '{
"AttachStdout":true,
"Tty":true,
"Cmd":["/opt/mssql-tools/bin/sqlcmd", ...]
}'
And here is the way I call my Symfony command in the api endpoint Controller :
/**
* @Route("/api/sync", name="sync", methods={"GET"})
*/
public function sync(KernelInterface $kernel)
{
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array(
'command' => 'app:sync'
));
$output = new BufferedOutput();
$application->run($input, $output);
$content = $output->fetch();
return new Response($content);
}
Maybe should I replace "localhost" with "api_default" to reach the good network, but that doesn't work ... ?