2

I've a repo with angular and nodejs. I performed in jenkins:

# install globally
npm install -g bower
npm install -g gulp

# install
bower install
npm install

# build dist folder
gulp build

Now I have in my root:

Dockerfile.nginx  Dockerfile.nodejs  README.md  bower.json  dist  gulp.config.js  gulpfile.js  node_modules  package.json  server.js  src

I'm copying the dist folder inside my nginx container. So I'm hosting the angular. (with a dockerfile)

FROM nginx
# copy folder
COPY dist /usr/share/nginx/html/dist

I'm copying: gulp.config.js gulpfile.js node_modules server.js to my nodejscontainer. (also with a dockerfile)

FROM node

# Create app directory
RUN mkdir -p /usr/src/www
WORKDIR /usr/src/www 

# copy 
COPY node_modules /usr/src/www/
COPY gulpfile.js /usr/src/www/
COPY gulp.config.js /usr/src/www/
COPY server.js /usr/src/www/

 EXPOSE 8080
CMD [ "node", "server.js" ]

I run the 2 containers but the nginx does not communicate with the nodejs

EDIT1: Start containers:

docker run -d -p 8888:8888 --name "nodejs" localhost:5000/test/nodejs:1

docker run -d -p 80:80 --name "nginx" localhost:5000/test/nginx:1

EDIT2: My nginx.conf looks like this:

http {

        upstream node-app {
              least_conn;
              server nodejs:8888 weight=10 max_fails=3 fail_timeout=30s;
        }

        server {
              listen 80;
              location /dist {
                alias /usr/share/nginx/html/dist/;
               }

              location ~* /api {
              #location / {
                proxy_pass http://node-app;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
              }
        }
}

My server.js looks like:

app.get('/api/hello', requestProxy({
  url: xxx + "/hello"
}));

2 Answers2

2

You need to expose the port of the node.js container to which nginx(angular) container will connect. See the Connect using network port mapping section of docker documentation.

Update : I think, you need to configure the nginx configuration file to the node container. This question has sample nginx file related to your use case (although, not related to containers).

Edit : To map the node app with the nginx, you first need to link the node container with nginx container.

docker run -d -p 80:80 --name "nginx" --link nodejs:nodejs localhost:5000/test/nginx:1

When you link the node container with the nginx container, the node container's address will be saved in the /etc/hosts. So the nginx container can access the node's address from there.

So, in nginx configuration file, the nodejs will be accessible as nodejs' container address:

http {

        upstream node-app {
              server nodejs:8888 weight=10 max_fails=3 fail_timeout=30s;
        }

        server {
              listen 80;

              location / {
                proxy_pass http://node-app;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
              }
        }
}
Community
  • 1
  • 1
manish
  • 966
  • 2
  • 11
  • 35
  • @manish Do you always need to configure nginx.conf to let nodejs and nginx work together? – lvthillo Jan 19 '16 at 12:03
  • PS: I am also kind of beginner in docker and nginx, so some information might be wrong.. Also, I found the following to be very helpful: http://anandmanisankar.com/posts/docker-container-nginx-node-redis-example/ – manish Jan 19 '16 at 13:35
  • I saw that post too. I tried your proposal and I have the same issue as explained at the answer under yours. ( I don't know what I have to write here: location / ). The api is running at url:8888/api –  Jan 19 '16 at 14:21
  • 1
    @Jenson I'm also nginx newbie, but I think you can set alias to the angular files in the nginx configuration file and then use that path in the node app. See http://stackoverflow.com/a/29384842/3785351 So, you will also need to link nginx to node container. – manish Jan 19 '16 at 14:43
  • I've edited my nginx.conf but still not working (see question) –  Jan 19 '16 at 14:52
  • Thanks, now I'm able to see my static files again. I can see url:80/dist/... (and I can see my static files), on url:80/api I can see my 'backend'. But they aren't linked. Still doing something wrong? on url:80/dist/page1 I have to see the data which is provided from my api but it isn't there –  Jan 19 '16 at 14:59
  • Maybe my server.js can help you? –  Jan 19 '16 at 15:20
1

One solution is to link both containers as described in @manish's answer.

But be aware that this is the legacy way of connecting containers together.


From now on, you can use the new docker network feature to create a virtual network and connect both containers to that network:

docker network create mynetwork
docker run -d --net=mynetwork -p 8888:8888 --name "nodejs" localhost:5000/test/nodejs:1
docker run -d --net=mynetwork -p 80:80 --name "nginx" localhost:5000/test/nginx:1

With such a setup, your nginx config file must use

server nodejs:8888 weight=10 max_fails=3 fail_timeout=30s;

as you now refer to other container by their name.

Community
  • 1
  • 1
Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
  • I will try. But it's sure I have to link my containers so it wouldn't be done automatically? (because I saw it once working in my vm) (now I'm on ec2 of amazon). –  Jan 19 '16 at 13:57
  • I performed your steps and when I'm going to url:80/api i see the same as url:8888/api. So that seems to work. But I'm unable to see my angular code at url/dist/index.html# (it gave a 404) my nginx.config is has: location ~* /api { (because there is my api, am I doing it wrong? –  Jan 19 '16 at 14:10
  • I changed /api to /. Now I'm getting Cannot GET /dist/... at each page –  Jan 19 '16 at 14:13