Skip to content

Instantly share code, notes, and snippets.

@lorecrafting
Last active October 23, 2018 05:53
Show Gist options
  • Save lorecrafting/d66f2e5a51b3e2d0c2a3596832c14322 to your computer and use it in GitHub Desktop.
Save lorecrafting/d66f2e5a51b3e2d0c2a3596832c14322 to your computer and use it in GitHub Desktop.
Containerizing http-socket-server

Install Docker

Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04

OSX: https://store.docker.com/editions/community/docker-ce-desktop-mac

Ubuntu users create Dockerhub account at: https://hub.docker.com/ For Mac users, your Dockerhub credentials should have been created when you registered for Docker Store.

Create Dockerfile:

touch Dockerfile in project root directory. Add these contents to your Dockerfile:

# Built from Node from official Node base image
FROM node:10.0

# Specify an optional argument with a default value
ARG app_directory=/app

# Set the app directory as the context for all commands and entry to the container
WORKDIR ${app_directory}

# ONLY copy over the package.json to install NPM packages
COPY package.json .

# Install node module dependencies
RUN npm install

# Add the rest of the project files(most builds will start from here based on cache)
COPY . .

# Start the node application as you normally would
CMD ["node", "server.js"]

Add .dockerignore file

touch .dockerignore Add these contents to your .dockerignore file:

.git
.idea
**/node_modules
.DS_Store
.data

Build Container Locally

docker build -t <username>/<appname> . docker images to check your new image is in local registry

Run Image Locally

docker run -p 6969:6969 -d <username>/<appname> Smoke test: curl localhost:8080

Push image to Docker Hub

Associate local docker client to Docker Hub account: docker login --username=yourhubusername --email=youremail@company.com Push image to public registry on Docker Hub: docker push <username>/<appname>

Remember that these images are public! Meaning anybody can view them. Do not store sensitive information, secrets keys, etc!!!

Run image on EC2 instance

Make sure docker is installed on EC2 instance docker run -p 6969:8080 -d <username>/<appname>

Resources: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment