Skip to content

Instantly share code, notes, and snippets.

@xemasiv
Last active October 31, 2017 07:26
Show Gist options
  • Save xemasiv/4078c67850c06780c300482768cd1cfd to your computer and use it in GitHub Desktop.
Save xemasiv/4078c67850c06780c300482768cd1cfd to your computer and use it in GitHub Desktop.
Docker.md

(Click gist to view other files)

Interactive:

docker run -it <image> /bin/bash
# https://gist.github.com/mitchwongho/11266726

Keep it running:

docker run -d centos:latest sleep infinity
# https://stackoverflow.com/a/46898038/8805423
``

Contents

  • setting of workdir to ~/ (root home)
  • PM2 Installation
  • PM2 Authbind to allow pm2 access of pm2 process instances to ports 80 & 443
  • EXPOSE port 80 & 443

Usage:

  • Build & Push:
    • docker build -t xemasiv/image:latest .
    • docker push xemasiv/image:latest
  • Pull & Initialize:
    • docker pull xemasiv/image:latest
    • No mount:
      • docker run -it -d --name containerName -p 80:80 -p 443:443 xemasiv/image:latest
    • With mount:
      • docker run -it -d --name containerName --mount type=bind,src=~/projectDirectory,dst=~/projectDirectory -p 80:80 -p 443:443 xemasiv/image:latest
    • Optional:
      • --network=host
  • Run:
    • SSH keys:
      • docker container exec -i containerName /bin/bash -c 'mkdir -p ~/.ssh && touch ~/.ssh/id_rsa'
      • cat ~/.ssh/id_rsa | docker container exec -i containerName /bin/bash -c 'cat > ~/.ssh/id_rsa && cat ~/.ssh/id_rsa'
      • docker container exec -i containerName /bin/bash -c 'chmod 700 -R ~/.ssh/ && eval "$(ssh-agent)" ; ssh-add ~/.ssh/id_rsa'
    • Git-clone, npm-install & pm2-restart:
      • docker container exec -it containerName /bin/bash -c "cd ~/ && git clone <YOUR_REPO>"
      • docker container exec -it containerName /bin/bash -c "cd ~/projectDirectory && npm install"
      • docker container exec -it containerName /bin/bash -c "cd ~/projectDirectory && pm2 start index.js --name pm2-instance"
  • Git Pull & Restart:
    • docker container exec -it server-container /bin/bash -c "cd ~/projectDirectory && git pull && pm2 restart pm2-instance"

Dockerfile contents:

FROM xemasiv/node-env

WORKDIR ~/

# PM2 installation
RUN npm install pm2@latest -g

# PM@ authbind configuration
# http://pm2.keymetrics.io/docs/usage/specifics/
RUN sudo apt-get update && apt-get install authbind -y --no-install-recommends
RUN touch /etc/authbind/byport/80
RUN chown node /etc/authbind/byport/80
RUN chmod 755 /etc/authbind/byport/80
RUN touch /etc/authbind/byport/443
RUN chown node /etc/authbind/byport/443
RUN chmod 755 /etc/authbind/byport/443
RUN echo 'alias pm2="authbind --deep pm2"' >> ~/.bashrc
RUN authbind --deep pm2 update

# Expose ports 80 & 443 for HTTP & HTTPS
EXPOSE 80
EXPOSE 443

# Clean-up again
RUN apt-get autoremove -y && \
  apt-get autoclean && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

CMD [ "/bin/bash" ]

Summary:

It's a modified version of TailorBrands' version, with the following changes:

  • Base image is now pulled from official node:latest image, making the node easily interchangeable with older versions by just editing a single line.
  • Image is now inclusive with node-gyp dependencies for rebuilding libraries, so installed npm packages (thru Dockerfile, or real-time dev/prod) will build flawlessly
  • Image is also inclusive of 'sharp' npm package, npm-installed as a global library
  • Added tweaks that:
    • Allowed installation of global package in NPM (thru chown & sudo)
    • Allowed requiring of globally installed packages (thru ~/.bashrc fix)

Contents:

  • node:latest @ v8.8.1; 5.4.2
  • vips @ vips-8.5.5
  • gcc & g++ for node-gyp
  • npm install --global of 'sharp' library
  • chown to allow that npm --global install
  • ~/.bashrc fix to allow requiring of globally installed packages

Setup

  • docker pull xemasiv/node-env
  • docker run -it -d xemasiv/node-env
  • docker container ls --> to get the container name
  • docker attach <container_name>

Testing (now logged in as root):

  • node -v && npm -v && vips -v
  • node --> takes you to node cli
  • require('sharp') --> should return the sharp function itself.

Usage

  • attach your project directory as a docker volume.
  • expose a port to make your project accessible from the host machine & outside world.
  • this gives you an environment where vips and 'sharp' library is already accessible.

Credits:

  • Check the Dockerfile for the sources and the things you can modify.

Dev-ready environment:

FROM node:latest

# GCC @ https://github.com/wanglilong007/debian-jessie-gcc/blob/master/Dockerfile
# LIBVIPS @ https://github.com/TailorBrands
# node:latest @ https://github.com/nodejs/docker-node
# apt-utls @ https://github.com/phusion/baseimage-docker/issues/319#issuecomment-245857919

RUN apt-get update && apt-get install -y --no-install-recommends apt-utils

RUN apt-get update && \
  DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
  automake build-essential curl gobject-introspection \
  gtk-doc-tools libglib2.0-dev libturbojpeg1-dev libpng12-dev \
  libwebp-dev libtiff5-dev libgif-dev libexif-dev libxml2-dev \
  libpoppler-glib-dev swig libmagickwand-dev libpango1.0-dev \
  libmatio-dev libopenslide-dev libcfitsio3-dev libgsf-1-dev \
  fftw3-dev liborc-0.4-dev librsvg2-dev gobject-introspection \ 
  gcc wget make g++ openssl libssl-dev libpcre3 libpcre3-dev zlib1g zlib1g-dev \
  nano sudo
# install sudo & nano etc.

ENV LIBVIPS_VERSION_MAJOR 8

ENV LIBVIPS_VERSION_MINOR 5

ENV LIBVIPS_VERSION_PATCH 5
# We set this to 5 to match the sharp lib, but it's interchangeable anyways with later versions.

ENV LIBVIPS_VERSION $LIBVIPS_VERSION_MAJOR.$LIBVIPS_VERSION_MINOR.$LIBVIPS_VERSION_PATCH

RUN cd /tmp && \
  curl -L -O https://github.com/jcupitt/libvips/releases/download/v$LIBVIPS_VERSION/vips-$LIBVIPS_VERSION.tar.gz && \
  tar zxvf vips-$LIBVIPS_VERSION.tar.gz && \
  cd /tmp/vips-$LIBVIPS_VERSION && \
  ./configure --enable-debug=no --without-python $1 && \
  make && \
  make install && \
  ldconfig
# Build libvips

RUN chown -R node:node /usr/local/lib/node_modules && chown -R node:node /usr/local/bin
# We chown the folders so we can npm install --global flawlessly
# https://github.com/me-ventures/angular-cli-docker/blob/master/Dockerfile

RUN sudo -u node npm install sharp --global
# You can't switch to another user, but you can run as another user.
# https://unix.stackexchange.com/a/3572

RUN echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bashrc
# Setting node path in env is now mandatory export NODE_PATH thru ~/.bashrc
# https://askubuntu.com/a/438170
# https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package#comment31655957_15646750

RUN apt-get remove -y automake curl build-essential && \
  apt-get autoremove -y && \
  apt-get autoclean && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Clean-up.

CMD [ "/bin/bash" ]
@xemasiv
Copy link
Author

xemasiv commented Oct 29, 2017

docker pull xemasiv/node:latest
docker run -it -d xemasiv/node-env /bin/bash
docker container ls  # to get container name
docker attach <container name>

RUN apt-get install nano sudo
# install sudo so we can sudo.

RUN chown -R node:node /usr/local/lib/node_modules && chown -R node:node /usr/local/bin
# we chown the folders so we can npm install --global flawlessly
# https://github.com/me-ventures/angular-cli-docker/blob/master/Dockerfile

RUN sudo -u node npm install sharp --global
# you can't switch to another user, but you can run as another user.
# https://unix.stackexchange.com/a/3572

RUN echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bashrc
# setting node path in env is now mandatory export NODE_PATH thru ~/.bashrc
# https://askubuntu.com/a/438170
# https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package#comment31655957_15646750

USER node
#not necessary anymore

USER root
#back to root

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