0

I'm trying to pull nginx from docker hub using python docker library. 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).

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:

client.containers.list()

currently there is nothing happened, got nothing in http://localhost:8080. what do i missed? how to configure this nginx in the docker with the python library?


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"
But now when i'm trying to send POST request i got "405 Not Allowed".
In a quick search in google i found that i need to configure nginx.config in this way:

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? Is this the right solution? replace 405 with 200??

Thanks.

2 Answers 2

1

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.

I recommend you going through the API reference first: https://docker-py.readthedocs.io/en/stable/containers.html

Sign up to request clarification or add additional context in comments.

1 Comment

I have a question I am trying to tag my image container and then push it to my docker hub , can you help me out? as I struggled to find a solution via documentation for the API
0

You have to set the port mapping during containers.run() setting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.