Skip to content

Instantly share code, notes, and snippets.

@zahid
Last active April 24, 2018 14:16
Show Gist options
  • Save zahid/c0287527e3d24df72e71eea86b8aa32f to your computer and use it in GitHub Desktop.
Save zahid/c0287527e3d24df72e71eea86b8aa32f to your computer and use it in GitHub Desktop.
Using Docker to virtualize your development environment
console.log("app started");
setInterval(() => {
console.log("hello there");
}, 2000);
# To build these services:
# docker-compose build
#
# To run these services container:
# docker-compose up
#
# To shut down these services:
# docker-compose down
version: '2'
services:
app:
container_name: app
build: .
volumes:
- .:/app
command: pm2-docker --watch app.js

What is a virtual environment

Docker is used to virtualize a development environment so that everyone has the same tools, software, and running source code.

Terms

Host: Your computer (macOS).

Guest: The virtual machine (and any containers inside of it).

Reasons to use it

  1. Have the environment start up identically everytime.

    $ docker-compose build
    ...
    $ docker-compose up
    
  2. Be able to SSH into guest operating system so I can interact with or debug things.

    docker exec -it app bash
    
  3. Have the files on my host filesystem so my IDE can work with them.

  4. When I make file changes, automatically restart the application so that development is seamless.

    • I don't have to deploy code locally.
  5. Have the same OS and configuration on development and remote environments (more on this later in docker-p2-deployment).

    • No more "it works on my computer" scenarios.

How

See the included docker-compose.yml, Dockerfile, and app.js.

# To build this image:
# docker build -t app .
#
# To run this image as a container:
# docker run --name app --rm -it app
FROM node:6
RUN npm install --global pm2
WORKDIR /app
COPY . /app
CMD node app.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment