Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker restart entrypoint

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
like image 437
HES_Xenon Avatar asked Aug 05 '16 10:08

HES_Xenon


People also ask

Does docker start run ENTRYPOINT?

Any Docker image must have an ENTRYPOINT or CMD declaration for a container to start. Though the ENTRYPOINT and CMD instructions may seem similar at first glance, there are fundamental differences in how they build container images. (This is part of our Docker Guide.

Does docker run override ENTRYPOINT?

ENTRYPOINT is the other instruction used to configure how the container will run. Just like with CMD, you need to specify a command and parameters. However, in the case of ENTRYPOINT we cannot override the ENTRYPOINT instruction by adding command-line parameters to the `docker run` command.

How do I restart my docker?

To do this, you must restart the docker service. If you edit the /etc/sysconfig/docker configuration file while the docker service is running, you must restart the service to make the changes take effect.

How do I resume a docker container?

Enter the docker start command with the Container ID in the command line to resume the Container. Note: This preserves environment variables set during the initial docker run statement.

How to see the entrypoint of a running container in Docker?

We want to see the entrypoint of a running container. We can do so by using the ‘inspect’ command or ‘docker ps’ command with the ‘format’ flag to get a cleaner output as below: – We want to override the entrypoint while starting the container.

How do I restart a container in Docker?

Use a restart policy. To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following: The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted.

What is the difference between'always restart'and'unless stopped'in Docker?

But the main difference between the two is that if you stop the containers with docker stop command and then restart the docker daemon, the container with always restart policy will start the container automatically but the container with unless-stopped policy won't be restarted. Let me show it with examples.

What happens when you stop a docker container?

If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This is another attempt to prevent a restart loop. Restart policies only apply to containers. Restart policies for swarm services are configured differently.


1 Answers

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.

like image 182
MortenB Avatar answered Oct 05 '22 03:10

MortenB