Docker Installation
First, we need to install Docker and Docker Compose.
Docker is built on top of Linux container. So, docker can’t run natively on OSX. It’s required a virtual machine for running in OSX. Thanks for Boot2Docker, it’s a Linux virtual machine made for running docker on OSX.
So, there is different between running docker on Linux and OSX.
When running docker on Linux, your machine is both the localhost and the Docker host.
# OSX (Homebrew)
brew update
brew install docker
brew install boot2docker
# then, start boot2docker vm
boot2docker init
boot2docker start
$(boot2docker shellinit)
# Linux
sudo apt-get update
sudo apt-get install docker.io
ln -sf /usr/bin/docker.io /usr/local/bin/docker
Docker Compose Installation
Compose is a tool for defining and running complex applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running. – Docker.com
# OSX (Homebrew)
brew install docker-compose
# Linux
curl -L https://github.com/docker/compose/releases/download/1.2.0/docker-compose-`uname -s`-`uname -m` > /usr/local/docker-compose
chmod +x /usr/local/bin/docker-compose
Note: If you get a “Permission denied” error, your /usr/local/bin directory probably isn’t writable and you’ll need to install Compose as the superuser. Run sudo -i, then the two commands above, then exit.
Let’s go !
clone the project
https://github.com/sahat/hackathon-starter
Install all dependencies.
npm install
Create configuration for Docker Compose
Create file docker-compose.yml
and include content below.
app:
image: node:latest
working_dir: /app
command: node app.js
volumes:
- .:/app
ports:
- "3000:3000"
links:
- db
db:
image: mongo:latest
We told docker compose that we need 2 containers that are app container and db container.
App container
- use latest node image
- use command
node app.js
for run application - mount current directory to
/app
in container - use
/app
in container as working directory - map port 3000 container to port 3000 on host (my pc)
- link to db container to tell docker compose to create db container before app container and add environment variable of db container to app container
Db container
- use latest mongo database image
Connect mongo db to the application
we need to change db config in config/secrets.js
from
db: process.env.MONGODB || 'mongodb://localhost:27017/test',
to
db: 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_PORT || 'localhost') + ':27017/test',
Because address of db container change all the time it created. So, it need to use environment variable in app image instead of direct address.
Note: We can list environment variables in app container using
docker-compose run app env
Output
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=09f3d3c280cb
TERM=xterm
DB_PORT=tcp://172.17.0.91:27017
DB_PORT_27017_TCP=tcp://172.17.0.91:27017
DB_PORT_27017_TCP_ADDR=172.17.0.91
DB_PORT_27017_TCP_PORT=27017
DB_PORT_27017_TCP_PROTO=tcp
DB_NAME=/myproject_app_run_1/db
DB_ENV_MONGO_MAJOR=3.0
DB_ENV_MONGO_VERSION=3.0.3
DB_1_PORT=tcp://172.17.0.91:27017
DB_1_PORT_27017_TCP=tcp://172.17.0.91:27017
DB_1_PORT_27017_TCP_ADDR=172.17.0.91
DB_1_PORT_27017_TCP_PORT=27017
DB_1_PORT_27017_TCP_PROTO=tcp
DB_1_NAME=/myproject_app_run_1/db_1
DB_1_ENV_MONGO_MAJOR=3.0
DB_1_ENV_MONGO_VERSION=3.0.3
MYPROJECT_DB_1_PORT=tcp://172.17.0.91:27017
MYPROJECT_DB_1_PORT_27017_TCP=tcp://172.17.0.91:27017
MYPROJECT_DB_1_PORT_27017_TCP_ADDR=172.17.0.91
MYPROJECT_DB_1_PORT_27017_TCP_PORT=27017
MYPROJECT_DB_1_PORT_27017_TCP_PROTO=tcp
MYPROJECT_DB_1_NAME=/myproject_app_run_1/myproject_db_1
MYPROJECT_DB_1_ENV_MONGO_MAJOR=3.0
MYPROJECT_DB_1_ENV_MONGO_VERSION=3.0.3
NODE_VERSION=0.12.4
NPM_VERSION=2.10.1
HOME=/root
Run the application
docker-compose up
And see result on localhost:3000
on Linux, but on OSX, the containers we created run on boot2docker vm. So, we need ip address of boot2docker vm using boot2docker ip
. Then, use ip:3000
to see result.
Note: you can also run test using
docker-compose run app /app/node_modules/mocha/bin/mocha
Finish !!!
Give all credit to @ Amine who talked about this useful topic on Chiang Mai Beercamp.