我们可以使用命令docker images
列出我们在本地主机上的Docker镜像。
现在,我希望通过在Firefox或Chrome中发送HTTP GET请求,从远程服务器获取相同的信息。 Docker是否提供了一些REST API来执行此操作?
我做了很多搜索。例如: Examples using the Docker Engine SDKs and Docker API
它提供了这样的方式:
curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json
我对Unix套接字了解不多,我不认为这就是我想要的。 URL(http:/v1.24/containers/json)非常奇怪,甚至没有服务器名称。我不认为它可以在远程服务器上运行。 (它在本地服务器上运行。)
Docker是否提供了有关此主题的官方文档?
答案 0 :(得分:2)
您需要在端口上公开Docker守护程序。
您可以使用多个-H选项将Docker守护程序配置为同时侦听多个套接字:
使用默认的Unix套接字以及此主机上的两个特定IP地址进行监听。
$ sudo dockerd -H unix:///var/run/docker.sock -H tcp://192.168.59.106 -H tcp://10.10.10.2
Docker客户端将使用DOCKER_HOST环境变量来为客户端设置-H标志。使用以下命令之一:
https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-socket-option
您需要通过创建systemd dropin来执行此操作:
#include <vector>
#include <iostream>
#include <string>
namespace notstd
{
/* deduce the traits of a container argument, even if it's an rvalue-reference */
template<class T>
struct container_traits
{
static_assert(not std::is_pointer<T>(), "");
using without_reference_type = std::remove_reference_t<T>;
using base_type = std::remove_cv_t<without_reference_type>;
static constexpr auto is_const = std::is_const<without_reference_type>::value;
static constexpr auto is_volaile = std::is_volatile<without_reference_type>::value;
using base_value_type = typename base_type::value_type;
using value_type = std::conditional_t<is_const, std::add_const_t<base_value_type>, base_value_type>;
};
template<class Function, class...Args>
struct is_compatible_function
{
template<class FArg> static auto test(FArg&& f) -> decltype(f(std::declval<Args>()...), void(), std::true_type());
static auto test(...) -> decltype(std::false_type());
static constexpr auto value = decltype(test(std::declval<Function>()))::value;
};
}
/**
* define the 2-argument algorithm, plus provide function compatibility checks
*/
template<class Vector, class Function>
struct over_op_2
{
using arg_1_type = std::add_lvalue_reference_t<typename notstd::container_traits<Vector>::value_type>;
using arg_2_type = std::size_t;
static constexpr auto is_compatible_function = notstd::is_compatible_function<Function, arg_1_type, arg_2_type>::value;
template<class VectorArg, class FunctionArg>
void operator()(VectorArg&& vec, FunctionArg&& f) const
{
std::size_t i = 0;
for (auto &&x : vec) {
f(x, i);
++i;
}
}
};
/**
* define the 1-argument algorithm, plus provide function compatibility checks
*/
template<class Vector, class Function>
struct over_op_1
{
using arg_1_type = std::add_lvalue_reference_t<typename notstd::container_traits<Vector>::value_type>;
static constexpr auto is_compatible_function = notstd::is_compatible_function<Function, arg_1_type>::value;
template<class VectorArg, class FunctionArg>
void operator()(VectorArg&& vec, FunctionArg&& f) const
{
for (auto &&x : vec) {
f(x);
}
}
};
/**
* Choose op_2 if the Function type will allow it, otherwise op_1 if that's possible, otherwise void (error)
*/
template<class Vector, class Function>
struct select_over_op
{
using op_1 = over_op_1<Vector, Function>;
using op_2 = over_op_2<Vector, Function>;
using type = std::conditional_t
<
op_2::is_compatible_function,
op_2,
std::conditional_t
<
op_1::is_compatible_function,
op_1,
void
>
>;
static_assert(not std::is_same<type, void>(), "function signatures are incompatible");
;
};
/**
* iterate over a vector-like container, calling f(elem, i) if available or f(elem) if not.
* @param vec is a reference to a vector-like object
* @param f is a function which is compatible with one of:
* void([const]value_type&, std::size_t), or
* void([const]value_type&)
*/
template<class Vector, class F>
decltype(auto) over(Vector &&vec, F &&f)
{
auto op = typename select_over_op<decltype(vec), decltype(f)>::type();
return op(std::forward<Vector>(vec), std::forward<F>(f));
}
int main() {
std::vector<double> v{4.1,5.1,6.1};
over(v, [] (auto x, auto y) { std::cout << x << ", " << y << "\n"; });
over(v, [] (auto && x, auto&& y) { std::cout << x << ", " << y << "\n"; });
over(v, [] (auto const& x, auto const& y) { std::cout << x << ", " << y << "\n"; });
over(v, [] (auto x, auto&& y) { std::cout << x << ", " << y << "\n"; });
over(v, [] (auto && x, auto&& y) { std::cout << x << ", " << y << "\n"; });
over(v, [] (auto const& x, auto&& y) { std::cout << x << ", " << y << "\n"; });
over(v, [] (auto x) { std::cout << x << "\n"; });
over(v, [] (auto const& x) { std::cout << x << "\n"; });
over(v, [] (auto && x) { std::cout << x << "\n"; });
// converting to int ok (but meh)
over(v, [] (int x) { std::cerr << x << "\n"; });
// converting to string correctly fails
// over(v, [] (std::string x) { std::cerr << x << "\n"; });
// const vector...
const std::vector<double> vc{4.1,5.1,6.1};
over(vc, [] (auto && x, auto&& y) { std::cout << x << ", " << y << "\n"; });
// breaking const contract on the value_type also fails
// over(vc, [] (double& x, auto&& y) { std::cout << x << ", " << y << "\n"; });
return 0;
}
然后重新加载并重启Docker:
mkdir -p /etc/systemd/system/docker.service.d/
cat > /etc/systemd/system/docker.service.d/10_docker.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// -H tcp://0.0.0.0:2376
EOF
注意:这样你就会暴露你的主机,你不应该这样在生产中这样做。请在我之前分享的链接上阅读更多相关信息。