Getting started with Fig and Express.js
Let’s use Fig to set up and run a Expressjs/MongoDB app. Before starting, you’ll need to have Fig installed.
Let’s set up the four files that’ll get us started.
First, our app is going to be running inside a Docker container which contains all of its dependencies. We can define what goes inside that Docker container using a file called Dockerfile
. It’ll contain this to start with:
That’ll install our application inside an image with Nodejs installed alongside all of our Nodejs dependencies. For more information on how to write Dockerfiles
, see the Docker user guide and the Dockerfile reference.
Second, we define the dependencies in a file called package.json
:
3rd, this is all tied together with a file called fig.yml
. It describes the services that our app comprises of (a web server and database), what Docker images they use, how they link together, what volumes will be mounted inside the containers and what ports they expose.
See the fig.yml reference for more information on how it works.
Finally, we need a file called ‘app.js’ to start the expressjs app and connect the mongodb.
OK, everything has been ready.
Then, run fig up
:
In the background:
- Fig will help you to build a new image with your
Dockerfile
.2. Fig create two container (web and db). - Run a command
node app.js
(configed in fig.yml) on the web container.
And now your Expressjs app should be running at port 3000 on your docker daemon (if you’re using boot2docker, boot2docker ip
will tell you its address).
DONE
- all the code will be donwload on https://github.com/guolin/fig-expressjs-examples*
注:第一次写英文博客 :)