I am trying to create a container for my Node app. This app uses MongoDB to ensure some data persistence. So I created this Dockerfile:
FROM ubuntu:latest
# --- Installing MongoDB
# Add 10gen official apt source to the sources list
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list
# Hack for initctl not being available in Ubuntu
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -s /bin/true /sbin/initctl
# Install MongoDB
RUN apt-get update
RUN apt-get install mongodb-10gen
# Create the MongoDB data directory
RUN mkdir -p /data/db
CMD ["usr/bin/mongod", "--smallfiles"]
# --- Installing Node.js
RUN apt-get update
RUN apt-get install -y python-software-properties python python-setuptools ruby rubygems
RUN add-apt-repository ppa:chris-lea/node.js
# Fixing broken dependencies ("nodejs : Depends: rlwrap but it is not installable"):
RUN echo "deb http://archive.ubuntu.com/ubuntu precise universe" >> /etc/apt/sources.list
RUN echo "deb http://us.archive.ubuntu.com/ubuntu/ precise universe" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y nodejs
# Removed unnecessary packages
RUN apt-get purge -y python-software-properties python python-setuptools ruby rubygems
RUN apt-get autoremove -y
# Clear package repository cache
RUN apt-get clean all
# --- Bundle app source
ADD . /src
# Install app dependencies
RUN cd /src; npm install
EXPOSE 8080
CMD ["node", "/src/start.js"]
Then I build and launch the whole thing through:
$ sudo docker build -t aldream/myApp
$ sudo docker run aldream/myApp
But the machine displays the following error:
[error] Error: failed to connect to [localhost:27017]
Any idea what I am doing wrong? Thanks!
Do you actually
docker run aldream/myApp
? In that case, with the Dockerfile that you provided, it should run MongODB, but not your app. Is there anotherCMD
command, or another Dockerfile, or are you runningdocker run aldream/myApp <somethingelse>
? In the latter case, it will override theCMD
directive and MongoDB will not be started.If you want to run multiple processes in a single container, you need a process manager (like e.g. Supervisor, god, monit) or start the processes in the background from a script; e.g.:
Redefine your Dockerfile as follows;
supervisord.conf will contain the following;