Docker restart entrypoint

HES_Xenon picture HES_Xenon · Aug 5, 2016 · Viewed 9.7k times · Source

I've started using docker about a month ago now and cannot find a satisfying solution to the following situation.

I want to deploy a NodeJS application and since using ENTRYPOINT is a best practice I'd prefer to use this command: ENTRYPOINT ["node", "src/start.js"].

However I have not found a way to restart the entrypoint process within the container, which means that everytime I change something inside the nodejs app I have to restart the whole container which is mildly annoying in a development environment with a shared volume.

A solution I thought of would be to use a process manager for this, and do something like ENTRYPOINT ["pm2", "src/start.js"] but using a process manager for a single process seems wrong to me.

I'd like to ask for an approach that gets me as close to hot-swapping as possible without changing the Dockerfile at all between my "Development Docker" and the "Production Docker".

TL;DR: It should be possible to NOT have NodeJS or anything the app requires installed on my development machine but run everything from within a docker container while being able to restart the node process in said container without having to restart the container itself. It is not an option for me to change the Dockerfile and I'd like to use ENTRYPOINT.

EDIT:
Dockerfile

FROM mhart/alpine-node:4.4.7

# add curl and bash
RUN apk add --update curl bash

#Add user
RUN addgroup websites && adduser -s /bin/bash -D -G websites user-api

#Copy app
WORKDIR /srv/app
ADD src ./src/
ADD node_modules ./node_modules

#Expose port
EXPOSE 3000

ENTRYPOINT ["node", "src/start.js"]

Building the image with

docker build -t app .

Running the container on my workstation with

docker run -dit -p 53017:3000 --name app -v c:/Users/hesxenon/Projects/app:/srv/app app:latest

Answer

MortenB picture MortenB · Dec 16, 2016

There is a great npm module for this pm2. Install it as a global package in your nodejs base image.

Start your app with ENTRYPOINT ["pm2-docker", "src/start.js"]

You can then enter the docker image with docker exec -ti <containerid> <shell> and stop app using pm2 stop 0, then reconfigure, and start it again with pm2 start 0. Whitout killing the container thus to pid1 dying.