简体   繁体   English

如何从docker中提取nginx并仅在python脚本中运行

[英]How to pull nginx from docker and run it only in python script

I'm trying to pull nginx from docker hub using python docker library. 我正在尝试使用python docker库从docker hub拉nginx。 After pulling the nginx i want to run it and configure it to test some RestAPI requests - only basic ones to get response (200 OK). 拉动nginx之后,我想运行它并配置它以测试一些RestAPI请求-仅基本请求才能获得响应(200 OK)。

What i already have is the following: 我已经拥有以下内容:

img = client.images.pull('nginx:latest')
client.containers.run(img, detach=True)

After these lines i can see the new docker in the list got here: 在这些行之后,我可以看到列表中的新docker到达了这里:

client.containers.list()

currently there is nothing happened, got nothing in http://localhost:8080 . 目前没有任何反应, http:// localhost:8080没有任何反应。 what do i missed? 我错过了什么? how to configure this nginx in the docker with the python library? 如何使用python库在docker中配置此nginx?


Update and more info: 更新和更多信息:
The solution of the port was very helpful. 该端口的解决方案非常有帮助。
Now i can send GET request and get a response with "200 OK" 现在,我可以发送GET请求并获得“ 200 OK”响应
But now when i'm trying to send POST request i got "405 Not Allowed". 但是现在当我尝试发送POST请求时,出现“ 405 Not Allowed”。
In a quick search in google i found that i need to configure nginx.config in this way: 在Google的快速搜索中,我发现我需要以这种方式配置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;

    # ...
}

My question now is how can i config this file with the docker API? 我的问题现在是如何使用docker API配置此文件? Is this the right solution? 这是正确的解决方案吗? replace 405 with 200?? 用200代替405?

Thanks. 谢谢。

您必须在container.run()设置期间设置端口映射。

Port binding need to be set. 需要设置端口绑定。 Try the following command: 尝试以下命令:

client.containers.run(img, detach=True, ports={'80/tcp': 8080})

The ports parameter tells the Docker Daemon to expose port 80 inside the Nginx container as port 8080 on the host. ports参数告诉Docker守护进程将Nginx容器内的端口80公开为主机上的端口8080。

I recommend you going through the API reference first: https://docker-py.readthedocs.io/en/stable/containers.html 我建议您首先阅读API参考: https : //docker-py.readthedocs.io/en/stable/containers.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM