1

I am attempting to setup an nginx container that serves as a proxy to another container I have setup. I would like to automate this setup as I need to deploy a similar setup across several servers. For this I am using Ansible.

Here is my nginx.conf:

events {
  worker_connections 1024;
}

http {
  server {
    listen 8080;
    location / {
      proxy_pass http://192.168.1.14:9000;
    }
  }
}

Here is the relevant part of my Ansible YAML file:

- name: Install Nginx
      docker:
        name: nginx
        image: nginx
        detach: True
        ports:
            - 8080:8080
        volumes:
            - /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro

When I first run my playbook, nginx is running but is not bound to 8080 as seen here:

6a4f610e86d nginx "nginx -g 'daemon off" 35 minutes ago   Up Less than a second  80/tcp, 443/tcp nginx

However, if I run the nginx container directly with:

docker run -d -v /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -p 8080:8080 nginx

nginx and my proxy runs as expected and is listening on 8080:

c3a46421045c nginx "nginx -g 'daemon off" 2 seconds ago Up 1 seconds        80/tcp, 443/tcp, 0.0.0.0:8080->8080/tcp determined_swanson

Any idea why it works one way but not the other?

Update

Per the guidance given in the selected answer, I updated my YAML file thusly:

- name: Install Nginx
  docker:
    name: nginx
    image: nginx
    detach: True
    ports:
        - 8080:8080
    expose:
        - 8080
    volumes:
        - /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro

1 Answer 1

1

First, you need to make sure your nginx image EXPOSE the port 8080, and you can specify directly in your ansible yaml file:

expose
(added in 1.5)

List of additional container ports to expose for port mappings or links. If the port is already exposed using EXPOSE in a Dockerfile, you don't need to expose it again.

Then, the only other difference I see when considering the Ansible docker module is that the port are inside double-quotes:

ports:
    - "8080:9000"

Also, if you want to prexypass to another container in the same docker daemon, you might want to use a link instead of a fixed IP address.

links:
    - "myredis:aliasedredis"

That way, your nginx.conf includes a fixed rule:

proxy_pass http://aliasedredis:9000;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - I went ahead and tried the quotes just to make sure, but it still doesn't bind to port 8080. Any other ideas?
@Louis you need to be sure your nginx image does EXPOSE the port 8080
Yup expose was exactly what I needed. If you want to update your answer, I'll accept it.

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.