Lucas Tettamanti
Lucas Tettamanti

Reputation: 1810

Nginx doesn't run with my dockers containers

I'm trying to build my infrastructure with Docker and I build this script but Nginx doesn't work and I don't know why, what I'm doing wrong:

#!/usr/bin/env bash
#Remove current containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

#Run Mysql
docker run --name mysql1 -d erkules/galera:basic -p 3306:3306 -p 4567:4567 -p 4444:4444 -p 4568:4568 -v ~/data/mysql:/data --wsrep-cluster-address=gcomm:// --wsrep-node-address=10.10.10.10
docker run --name mysql2 -d erkules/galera:basic -p 3306:3306 -p 4567:4567 -p 4444:4444 -p 4568:4568 -v ~/data/mysql:/data --wsrep-cluster-address=gcomm://10.10.10.10 --wsrep-node-address=10.10.10.11
docker run --name mysql3 -d erkules/galera:basic -p 3306:3306 -p 4567:4567 -p 4444:4444 -p 4568:4568 -v ~/data/mysql:/data --wsrep-cluster-address=gcomm://10.10.10.10 --wsrep-node-address=10.10.10.12

#Run Redis
docker run --name redis1 -d -p 26379 -v ~/data/redis:/data -v ~/conf/sentinel:/src/usr/sentinel.conf -t redis redis-sentinel /src/usr/sentinel.conf
docker run --name redis2 -d -p 26379 -v ~/data/redis:/data -v ~/conf/sentinel:/src/usr/sentinel.conf -t redis redis-sentinel /src/usr/sentinel.conf
docker run --name redis3 -d -p 26379 -v ~/data/redis:/data -v ~/conf/sentinel:/src/usr/sentinel.conf -t redis redis-sentinel /src/usr/sentinel.conf

#Run NodeJS
docker run --name nodejs1   -d -v ~/code:/src/usr/app -w /src/usr/app --link redis1:redis1 --link redis2:redis2 --link redis3:redis3 --link mysql1:mysql1 --link mysql2:mysql2 --link mysql3:mysql3 -t lkzwieder/nodejs:latest nodemon app.js
docker run --name nodejs2 -d -v ~/code:/src/usr/app -w /src/usr/app --link redis1:redis1 --link redis2:redis2 --link redis3:redis3 --link mysql1:mysql1 --link mysql2:mysql2 --link mysql3:mysql3 -t lkzwieder/nodejs:latest nodemon app.js
docker run --name nodejs3 -d -v ~/code:/src/usr/app -w /src/usr/app --link redis1:redis1 --link redis2:redis2 --link redis3:redis3 --link mysql1:mysql1 --link mysql2:mysql2 --link mysql3:mysql3 -t lkzwieder/nodejs:latest nodemon app.js

#Run Nginx
docker run --name balancer -p 80:80 -v ~/conf/nginx:/etc/nginx/conf.d/default.conf --link nodejs1:nodejs1 --link nodejs2:nodejs2 --link nodejs3:nodejs3 -d nginx

But when I try to get the list of working containers sudo docker ps what I get is this:

CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                                NAMES
0b25395c3d77        lkzwieder/nodejs:latest   "nodemon app.js"         4 seconds ago       Up 3 seconds        8080/tcp                             nodejs3
34a7cf97b17c        lkzwieder/nodejs:latest   "nodemon app.js"         5 seconds ago       Up 4 seconds        8080/tcp                             nodejs2
8542bc9e5633        lkzwieder/nodejs:latest   "nodemon app.js"         5 seconds ago       Up 4 seconds        8080/tcp                             nodejs1
653cbc561336        redis                     "/entrypoint.sh redis"   5 seconds ago       Up 5 seconds        6379/tcp, 0.0.0.0:32814->26379/tcp   redis3
b14da9dbafcb        redis                     "/entrypoint.sh redis"   6 seconds ago       Up 5 seconds        6379/tcp, 0.0.0.0:32813->26379/tcp   redis2
b0c98f6db945        redis                     "/entrypoint.sh redis"   6 seconds ago       Up 5 seconds        6379/tcp, 0.0.0.0:32812->26379/tcp   redis1
e2cc1441520d        erkules/galera:basic      "mysqld -p 3306:3306 "   7 seconds ago       Up 6 seconds                                             mysql3
a053c9282be2        erkules/galera:basic      "mysqld -p 3306:3306 "   7 seconds ago       Up 6 seconds                                             mysql2
d9083d3f7daf        erkules/galera:basic      "mysqld -p 3306:3306 "   8 seconds ago       Up 7 seconds                                             mysql1

So, I don't have my nginx balancer.

My nginx configuration is:

worker_processes 4;
events {
    worker_connections 1024;
}

http {
    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;

    upstream node-app {
        least_conn;
        server nodejs1:8080 weight=10 max_fails=3 fail_timeout=30s;
        server nodejs2:8080 weight=10 max_fails=3 fail_timeout=30s;
        server nodejs3:8080 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;
        }
    }
}

Upvotes: 1

Views: 169

Answers (2)

Lucas Tettamanti
Lucas Tettamanti

Reputation: 1810

Well, I forgot this question but this is the result. I improved the script to launch all the environment adding the host to /etc/hosts automatically.

My problem was in the nginx configuration and the ports.

#!/usr/bin/env bash
read -p "Which is the host for nginx?: (dev.domain.com) " userHost
userHost=${userHost:-dev.domain.com}

#Remove current containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

# MySQL
docker run -v ~/mypath/to/data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=admin -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=databasename --name mysql0 -p 3306:3306 -d mysql/mysql-server

# NodeJS
docker run --name nodejs0 -v ~/mypath/to/code:/src/usr/app --link mysql0:mysql0 -w /src/usr/app -d lkzwieder/nodejs:nodemon
docker run --name nodejs1 -v ~/mypath/to/code:/src/usr/app --link mysql0:mysql0 -w /src/usr/app -d lkzwieder/nodejs:nodemon
docker run --name nodejs2 -v ~/mypath/to/code:/src/usr/app --link mysql0:mysql0 -w /src/usr/app -d lkzwieder/nodejs:nodemon

# Nginx
docker run --name balancer -v ~/mypath/to/conf/nginx:/etc/nginx/conf.d/server.conf -p 80:80 --link nodejs0:nodejs0 --link nodejs1:nodejs1 --link nodejs2:nodejs2 -d lkzwieder/nginx sh -c "service nginx start"

# Nginx ip into /etc/hosts
etchosts=`cat /etc/hosts | grep -v ${userHost}`
hostIp=`docker inspect balancer | grep "\"IPAddress\"" | cut -d ":" -f2 | cut -d "\"" -f2 | head -1`
echo -e "$etchosts" "\n${hostIp} ${userHost}" > /etc/hosts

Upvotes: 2

Phani
Phani

Reputation: 1901

you can run docker ps -a | grep balancer to know if your balancer had a stopped container! Looking at your script, it is most likely that your balancer had faced an nginx startup error and hence would have abruptly stopped.

If you see your balancer had a stopped container, first remove it using docker rm balancer, then start it in interactive mode using

docker run --name balancer -p 80:80 -v ~/conf/nginx:/etc/nginx/conf.d/default.conf --link nodejs1:nodejs1 --link nodejs2:nodejs2 --link nodejs3:nodejs3 -it nginx

Now you will be able to see the output of the container and can understand where the problem actually is! If you can post the output you receive here (this is what is asked by @tsturzl in his comment) then someone can help you.

Upvotes: 0

Related Questions