我试图使用python docker库从docker hub中拉出nginx。 拉动nginx后,我想运行它并进行配置 测试一些RestAPI请求 - 只有基本的请求才能获得响应(200 OK)。
我已经拥有以下内容:
img = client.images.pull('nginx:latest')
client.containers.run(img, detach=True)
在这些行之后,我可以看到列表中的新docker到达:
client.containers.list()
目前没有任何事情发生,http://localhost:8080没有任何结果。 我错过了什么? 如何使用python库在docker中配置这个nginx?
更新和更多信息:
港口的解决方案非常有用
现在我可以发送GET请求并获得响应" 200 OK"
但是现在当我试图发送POST请求时,我得到了#34; 405 Not Allowed"。
在谷歌的快速搜索中我发现我需要配置nginx.config
这样:
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 403 /403.html;
# To allow POST on static pages
error_page 405 =200 $uri;
# ...
}
我现在的问题是如何使用docker API配置此文件? 这是正确的解决方案吗?用200 ??
代替405感谢。
答案 0 :(得分:0)
您必须在containers.run()设置期间设置端口映射。
答案 1 :(得分:0)
需要设置端口绑定。请尝试以下命令:
client.containers.run(img, detach=True, ports={'80/tcp': 8080})
ports
参数告诉Docker守护进程将Nginx容器内的端口80暴露为主机上的端口8080。
我建议您首先浏览API参考:https://docker-py.readthedocs.io/en/stable/containers.html