MrDeclare
MrDeclare

Reputation: 13

Docker networking reverse proxy without docker-compose

The challenge

As described, I want to accomplish the same goal with docker itself as I would with the help of docker-compose.
I want to get a deeper understanding of docker and enable the ability to work with docker on platforms, where docker-compose is not an option.

What I do currently (with docker-compose)

1)

I use this docker-compose file:

---
version: '3'

services:
    app:
        build: .

    proxy:
        build: docker/proxy
        ports:
            - "80:80"

The "app" service starts a container which runs node on port 3002 (is exposed in the dockerfile)

The "proxy" service starts a container which runs an nginx with - among others - the following conf:

server {
    listen 80;
    server_name app;

    location / {
        proxy_pass  http://app:3002;
    }
}

2)

Then I add this to the /etc/hosts of my host pc:

127.0.0.1   app

3)

Now I run docker-compose up and vist http://app , which hits the node app.

Nice and simple, right?


Now I want to do the same only with docker.

What I've tried

1 using the same nginx configuration.

2 Starting the containers with a bash script

To accomplish this I

  1. Created a network
  2. Add the network to both containers
  3. Setting up "app"-container hostname, network-alias and dns-search to "app" (because I hoped one of the options would help)

Here the script:

docker network create --driver bridge dockertest_nw

docker build -t dockertest_app .
docker create \
    --name dockertest_app_con \
    --network dockertest_nw \
    --hostname app \
    --network-alias=app \
    --dns-search=app \
    dockertest_app

docker build -t dockertest_proxy ./docker/proxy/
docker create \
    --name dockertest_proxy_con \
    --network dockertest_nw \
    --hostname proxy \
    --network-alias=proxy \
    --dns-search=proxy \
    -p 80:80 \
    dockertest_proxy

docker start dockertest_proxy_con
docker start dockertest_app_con

Unfortunately, this doesn't work.

I also know there is a dns service from docker which docker-compose somehow uses and I should also use it on some way?

Could any one give some suggestions?


Update: Just the info I got the following logs from the nginx container, which i would say shows the nginx doesn't can resolve "app" :

172.18.0.1 - - [13/Apr/2017:14:49:06 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36" "-"
2017/04/13 14:49:06 [error] 5#5: *13 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: app, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3002/", host: "app"

Upvotes: 0

Views: 1754

Answers (1)

King Chung Huang
King Chung Huang

Reputation: 5644

You're tripping yourself up with all those options. All you really need is --network-alias to set the short form names app and proxy in your containers, which will be available in addition to the container names dockertest_app and dockertest_proxy.

docker network create --driver bridge dockertest_nw

docker build -t dockertest_app .
docker create \
    --name dockertest_app \
    --network dockertest_nw \
    --network-alias=app \
    dockertest_app

docker build -t dockertest_proxy ./docker/proxy/
docker create \
    --name dockertest_proxy \
    --network dockertest_nw \
    --network-alias=proxy \
    -p 80:80 \
    dockertest_proxy

docker start dockertest_proxy
docker start dockertest_app

Upvotes: 1

Related Questions