27 Mar 2015 15:36
Tags configuration docker image nodejs
This is to follow the example from Docker and I'd like to summarize the essential pieces to get an exsiting nodejs app runing in docker.
Dockerfile
FROM centos:centos6
# Enable EPEL for Node.js
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# Install Node.js and npm
RUN yum install -y npm
# Bundle app source, to /src
COPY . /src
# Install app dependencies
RUN cd /src; npm install
EXPOSE 8080
CMD ["node", "/src/app.js"]
Build Image
$ sudo docker build -t <your username>/centos-node-hello .
$ sudo docker images
# Example
REPOSITORY TAG ID CREATED
centos centos6 539c0211cd76 8 weeks ago
<your username>/centos-node-hello latest d64d3505b0d2 2 hours ago
Run the image
Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port in the container. Run the image you previously built:
$ sudo docker run -p 49160:8080 -d <your username>/centos-node-hello
Print the output of your app:
# Get container ID
$ sudo docker ps
# Print app output
$ sudo docker logs <container id>
# Example
Running on http://localhost:8080
Test
To test your app, get the port of your app that Docker mapped:
$ sudo docker ps
# Example
ID IMAGE COMMAND ... PORTS
ecce33b30ebf <your username>/centos-node-hello:latest node /src/index.js 49160->8080
In the example above, Docker mapped the 8080 port of the container to 49160.
Now you can call your app using curl (install if needed via: sudo apt-get install curl):
$ curl -i localhost:49160
If you use Boot2docker on OS X, the port is actually mapped to the Docker host VM, and you should use the following command:
$ curl $(boot2docker ip):49160