A quick how-to for using common docker
, docker-compose
and docker-machine
commands in an example NodeJS app which has different builds for development and
production and is deployed to a remote Docker host. Does NOT require uploading
your app images to any damn public registries! Or to any registry at all, for
that matter!
The commands below assume that your dev compose file runs your app build tasks
(gulp
, etc.). See example project files below.
You'll need the following .bashrc
aliases:
alias dclean='docker rm $(docker ps -f status=exited -a -q); docker rmi $(docker images -f dangling=true -a -q); docker volume rm $(docker volume ls -f dangling=true -q)'
alias dock='docker-compose'
alias dock-dev='docker-compose -f docker-compose.yaml -f docker-compose.development.yaml'
alias dock-ls='docker-machine ls'
alias dock-ssh='docker-machine ssh'
alias dock-scp='docker-machine scp'
alias dock-unuse='eval $(docker-machine env -u)'
function dock-use {
eval $(docker-machine env $1)
}
function dock-stop {
container=$1
if [ -z "$container" ]
then
echo "Stopping all..."
dock-dev down --remove-orphans
else
candidates=$(docker ps -q -f name=$container)
if [ -n "$candidates" ]
then
docker stop $candidates
else
echo "No such container: $container"
fi
fi
}
Including a SERVICENAME
in commands below tells Docker to apply commands
only to that named service and its dependencies; omitting it will apply commands
to all services in your compose file.
The docker-machine
commands require that the remote Docker host was
provisioned using docker-machine create
. Still waiting for Docker to implement
docker-machine export/import
...
All Docker commands should be run from root of your project (same directory as
your docker-compose.yaml
file).
dock-dev up [SERVICENAME]
dock-dev run --rm SERVICENAME bash
(run app build tasks; gulp
, etc.)
exit
(Ctrl-d)
dock-dev build --no-cache [SERVICENAME]
dock-stop [CONTAINERNAME]
dock build --no-cache [SERVICENAME]
Bump app version and build a new prod image (with clean local git; see example package.json
, etc. below)
npm version RELEASELEVEL
dock build SERVICENAME
dock config
(prod containers)
dock-dev config
(dev containers)
See official Docker docs.
dock-ls
dock-ssh HOSTNAME
dock-use HOSTNAME
dock up -d [SERVICENAME]
dock-unuse
dock-use HOSTNAME
(connect to remote Docker host)
(your Docker command here)
dock-unuse
(return to local Docker)
docker ps
docker images
dock down
dclean
Basic directory structure for this example looks like:
my-app/
dist/
src/
gulpfile.js
package.json
Dockerfile
Dockerfile-development
docker-compose.yaml
docker-compose.development.yaml
.env
And some example files...
# docker-compose.yaml
version: '2'
services:
myapp:
build:
context: ./my-app
container_name: myapp
environment:
- NODE_ENV=production
- AUTH_SECRET
- AUTH_CALLBACK
image: "myapp:${MY_APP_VERSION}"
ports:
- '5006:5000'
restart: always
mongodb:
container_name: mongodb
image: mongo:latest
ports:
- '27017:27017'
volumes:
- /data/db:/data/db
- /data/backups:/data/backups
restart: always
redis:
container_name: redis
image: redis:latest
ports:
- '6379:6379'
restart: always
# my-app/Dockerfile
FROM node:6.9.5
RUN useradd --user-group --create-home --shell /bin/false app
ENV HOME=/home/app
COPY . $HOME/my-app
RUN chown -R app:app $HOME/*
USER app
WORKDIR $HOME/my-app
RUN npm install --production --silent
CMD ["node", "dist/app.js"]
# docker-compose.development.yaml
version: '2'
services:
myapp:
build:
context: ./my-app
dockerfile: Dockerfile-development
command: bash -c 'gulp clean build watch'
environment:
- NODE_ENV=development
- AUTH_SECRET
- AUTH_CALLBACK
image: myapp-dev
volumes:
- ./my-app:/home/app/my-app
- /home/app/my-app/node_modules
# my-app/Dockerfile-development
FROM node:6.9.5
RUN useradd --user-group --create-home --shell /bin/false app && \
npm install --silent --global gulp-cli
ENV HOME=/home/app
COPY . $HOME/my-app
RUN chown -R app:app $HOME/*
USER app
WORKDIR $HOME/my-app
RUN npm install --silent
# my-app/package.json
{
"name": "my-app",
"version": "0.0.1",
"main": "dist/app.js",
"scripts": {
"version": "docker-env-version ../.env"
},
"devDependencies": {
"docker-env-version": "0.0.7",
"gulp",
...
},
"dependencies": {
...
}
You may find these Bash scripts make your Docker life easier.