A basic workflow for Docker (Compose) with MEAN stack
Interestingly, I found a lack of articles illustrating a basic workflow for Docker or Docker Compose with the MEAN stack, so I decided to provide some insight.
For the uninformed, the MEAN stack refers to MongoDB, ExpressJS, AngularJS, Node.js.
I know you probably can find some docker files at the mean.io or mean.js repositories, but interestingly they are still pretty limited in terms of usage. What if you wanted for more than development? And besides at this time of writing, the mean.js docker files are not foolproof enough. The docker workflow for mean.js causes permission issues with additional plugins such as grunt-contrib-imagemin. As such, I have created my basic workflow on top of these files to provide for a more complete workflow using docker
and docker-compose
.
As a sidenote, my project stems from the Yeoman generator at https://github.com/DaftMonk/generator-angular-fullstack, which provides a much more practical project framework compared to mean.io and mean.js though.
Development / Staging
So for the start, here are the 2 files for the complete development/staging workflow.
Just copy docker-compose.yml and Dockerfile into your project directory and run docker-compose up
to get it started.
Note: I assume that you already know how to install docker-compose
(https://docs.docker.com/compose/install/).
docker-compose.yml:
Dockerfile:
Production
Just as a sidenote, you need to understand that docker
is meant to function for a single host, so this workflow will only work on a single machine. If you need to run distributed applications on separate machines, that will be a lot more troublesome, so we’ll have that discussion another day.
Do note that the directories if necessary should be created on the host machine before docker-compose
is run.
To get the docker image built with docker
easily up onto the production machine, let’s use Docker Hub.
This is much easier than trying to create your own Docker Registry and then trying to wrap it up with TLS. The Docker daemon is really unstable, and all my attempts to incorporate TLS failed terribly.
Create an account at Docker Hub and then create a repository (public/private), e.g. app.
If you named it as “app”, the image will be named username/app, where the username is your Docker Hub username.
To start, build the application to prepare to be transferred over to production. Use the following Dockerfile to build.
Dockerfile:
Run the following command in the project directory.
docker build -t="username/app" .
If you have not logged in to Docker Hub yet, do this and type in your credentials/details:
docker login
Now upload the built docker image to the remote docker repo at Docker Hub.
docker push username/app
Now onto the production server.
You will need to install docker
and docker-compose
.
Prepare the necessary directories on the production server as stated as volumes in the docker-compose.yml file.
Similarly, do a docker login
to login to Docker Hub.
You will need to have the docker-compose.yml file and production.env file containing your production environment variables.
Run docker-compose up -d
to start running all the docker containers in detached mode.
docker-compose.yml:
Once you have done all these steps, note how relatively easy it is to push your updated application to production.
Just use docker
to rebuild the app image, push the image up to the repo, use server to pull image from repo and use docker to rerun the image.