Node.js Koa app and CouchDB in single container
I have a koa.js app which I want to run in a docker container. This loa app requires couchdb to run, which I want to ship in the same container. I know this is not best practise but it is indeed the best way for my users to get started.
# DOCKER-VERSION 1.2.0 FROM centos:centos6 # Enable EPEL for CouchDB and Node.js RUN yum -y update; yum clean all RUN yum -y install epel-release; yum clean all RUN yum -y install tar # Install Node.js and CouchDB RUN yum -y install couchdb; yum clean all RUN service couchdb start RUN yum install -y nodejs RUN yum install -y npm # Bundle app source ADD . . # Install app dependencies RUN npm install RUN npm install -g n RUN n 0.11.12 # Expose port and run EXPOSE 8080 CMD ["npm", "start"]
which works fine, app gets launched but it can’t connect to the couchdb. throwing in a
RUN service couchdb start
response with OK, so it seems to work, but
curl -X GET 127.0.0.1:5984
curl: (7) couldn't connect to host
same for the koa.js app:
error: error stack=Error: connect ECONNREFUSED at exports._errnoException (util.js:745:11) at Object.afterConnect [as oncomplete] (net.js:995:19), code=ECONNREFUSED, errno=ECONNREFUSED, syscall=connect
someone knows what I am missing or what I am doing wrong?
One Solution collect form web for “Node.js Koa app and CouchDB in single container”
The only command that is run when you start this image is what is in the CMD line. Every line before that creates a read only, non-running image. Thus, the line
RUN service couchdb start will start the service for an instant, until it is marked as successful, then docker will stop that image, save it, and move on to the next line. The “running” state of the service doesn’t persist.
It is a common misconception, and one I fell into when I started.
The three options, top being fastest yet most hacky, and last being most work but most proper:
service couchdb start && npm startas your CMD line.
- Create a startup.sh script, do all the “running” that you need to do in there, and call that as your CMD line
- Use a service designed to do this stuff for you, supervisord is often recommended.
This is a common issue, so if you have a read through google results searching for “start service in docker” you’ll see more information around the subject.