GNU/make and docker, terminate the running process












0















I'm using GNU/make (linux) to wrap some docker command, I don't use docker everyday so I'll forget the usage soon, one of those wrapper is make serve, it run docker run with php -S (built-in server) as command, it start and listen for connections.



To stop it I should use ^C , I believe (but I'm not sure) the problem to be make intercepting the signal, the make's target fail but the php process (and the docker instance) remain running, I've to manually stop it.



I tried passing --sig-proxy=true and false but with no effect, but I'm running the instance with a pseudo terminal (-t) so this was expected.



I wrote a bash wrapper to echo some message when some signal is trapped (EXIT TERM SIGTERM SIGQUIT KILL SIGKILL) and exec php -S , but I don't see any message.



I'm not sure what happen when ^C is pressed on a running make, if the signal (and what) is propagated only to the forked sh -c, and why that wouldn't work with docker run.



I'd like to know if someone else had this problem and solved or if someone can think of some alternative solution.



edit



Makefile:



all:

.PHONY: docker-build serve

docker-build:
docker build -f Dockerfile -t sigtest:v1 .

serve:
docker run -t --rm sigtest:v1 gosu ubuntu:ubuntu php7.0 -S 0.0.0.0:8081 -t /home/ubuntu


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu


test with make docker-build and then make serve



2nd edit



I managed to wrap docker run with a shell script (I'm not sure about trap syntax in posix shells, eventually I'll take a look)



make seems to be sending INT the first time and and then EXIT



#!/bin/sh
pid=$$
trap 'trap - INT EXIT; echo " signal received, wait..."; docker stop -t 0 sig${pid}; exit' INT EXIT
docker run --name=sig${pid} "$@"


and then in the Makefile



serve:
-./docker-run.sh -t --rm sigtest:v1 gosu ubuntu:ubuntu php7.0 -S 0.0.0.0:8081 -t /home/ubuntu


I was hoping to not have to name the instances but that's the simplest way I can think of.










share|improve this question

























  • Could you post a simplified version of your Makefile which reproduces the issue?

    – Danila Kiver
    Nov 21 '18 at 16:16











  • Hello @DanilaKiver, see the edit, thanks

    – Alex
    Nov 21 '18 at 17:11











  • Had kind of similar issue before and switched to docker-compose. It has commands for managing the whole lifecycle including starting and stopping services.

    – Alex
    Nov 21 '18 at 17:47











  • Alex, sure, perhaps almost any solution removing make remove the issue, but that's the point, I been using make for years now, I'd like to continue. But you gave me an idea, maybe wrapping docker run with a script outside of the docker instead of wrapping php inside... I'll give a try, thank you

    – Alex
    Nov 21 '18 at 18:40
















0















I'm using GNU/make (linux) to wrap some docker command, I don't use docker everyday so I'll forget the usage soon, one of those wrapper is make serve, it run docker run with php -S (built-in server) as command, it start and listen for connections.



To stop it I should use ^C , I believe (but I'm not sure) the problem to be make intercepting the signal, the make's target fail but the php process (and the docker instance) remain running, I've to manually stop it.



I tried passing --sig-proxy=true and false but with no effect, but I'm running the instance with a pseudo terminal (-t) so this was expected.



I wrote a bash wrapper to echo some message when some signal is trapped (EXIT TERM SIGTERM SIGQUIT KILL SIGKILL) and exec php -S , but I don't see any message.



I'm not sure what happen when ^C is pressed on a running make, if the signal (and what) is propagated only to the forked sh -c, and why that wouldn't work with docker run.



I'd like to know if someone else had this problem and solved or if someone can think of some alternative solution.



edit



Makefile:



all:

.PHONY: docker-build serve

docker-build:
docker build -f Dockerfile -t sigtest:v1 .

serve:
docker run -t --rm sigtest:v1 gosu ubuntu:ubuntu php7.0 -S 0.0.0.0:8081 -t /home/ubuntu


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu


test with make docker-build and then make serve



2nd edit



I managed to wrap docker run with a shell script (I'm not sure about trap syntax in posix shells, eventually I'll take a look)



make seems to be sending INT the first time and and then EXIT



#!/bin/sh
pid=$$
trap 'trap - INT EXIT; echo " signal received, wait..."; docker stop -t 0 sig${pid}; exit' INT EXIT
docker run --name=sig${pid} "$@"


and then in the Makefile



serve:
-./docker-run.sh -t --rm sigtest:v1 gosu ubuntu:ubuntu php7.0 -S 0.0.0.0:8081 -t /home/ubuntu


I was hoping to not have to name the instances but that's the simplest way I can think of.










share|improve this question

























  • Could you post a simplified version of your Makefile which reproduces the issue?

    – Danila Kiver
    Nov 21 '18 at 16:16











  • Hello @DanilaKiver, see the edit, thanks

    – Alex
    Nov 21 '18 at 17:11











  • Had kind of similar issue before and switched to docker-compose. It has commands for managing the whole lifecycle including starting and stopping services.

    – Alex
    Nov 21 '18 at 17:47











  • Alex, sure, perhaps almost any solution removing make remove the issue, but that's the point, I been using make for years now, I'd like to continue. But you gave me an idea, maybe wrapping docker run with a script outside of the docker instead of wrapping php inside... I'll give a try, thank you

    – Alex
    Nov 21 '18 at 18:40














0












0








0


0






I'm using GNU/make (linux) to wrap some docker command, I don't use docker everyday so I'll forget the usage soon, one of those wrapper is make serve, it run docker run with php -S (built-in server) as command, it start and listen for connections.



To stop it I should use ^C , I believe (but I'm not sure) the problem to be make intercepting the signal, the make's target fail but the php process (and the docker instance) remain running, I've to manually stop it.



I tried passing --sig-proxy=true and false but with no effect, but I'm running the instance with a pseudo terminal (-t) so this was expected.



I wrote a bash wrapper to echo some message when some signal is trapped (EXIT TERM SIGTERM SIGQUIT KILL SIGKILL) and exec php -S , but I don't see any message.



I'm not sure what happen when ^C is pressed on a running make, if the signal (and what) is propagated only to the forked sh -c, and why that wouldn't work with docker run.



I'd like to know if someone else had this problem and solved or if someone can think of some alternative solution.



edit



Makefile:



all:

.PHONY: docker-build serve

docker-build:
docker build -f Dockerfile -t sigtest:v1 .

serve:
docker run -t --rm sigtest:v1 gosu ubuntu:ubuntu php7.0 -S 0.0.0.0:8081 -t /home/ubuntu


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu


test with make docker-build and then make serve



2nd edit



I managed to wrap docker run with a shell script (I'm not sure about trap syntax in posix shells, eventually I'll take a look)



make seems to be sending INT the first time and and then EXIT



#!/bin/sh
pid=$$
trap 'trap - INT EXIT; echo " signal received, wait..."; docker stop -t 0 sig${pid}; exit' INT EXIT
docker run --name=sig${pid} "$@"


and then in the Makefile



serve:
-./docker-run.sh -t --rm sigtest:v1 gosu ubuntu:ubuntu php7.0 -S 0.0.0.0:8081 -t /home/ubuntu


I was hoping to not have to name the instances but that's the simplest way I can think of.










share|improve this question
















I'm using GNU/make (linux) to wrap some docker command, I don't use docker everyday so I'll forget the usage soon, one of those wrapper is make serve, it run docker run with php -S (built-in server) as command, it start and listen for connections.



To stop it I should use ^C , I believe (but I'm not sure) the problem to be make intercepting the signal, the make's target fail but the php process (and the docker instance) remain running, I've to manually stop it.



I tried passing --sig-proxy=true and false but with no effect, but I'm running the instance with a pseudo terminal (-t) so this was expected.



I wrote a bash wrapper to echo some message when some signal is trapped (EXIT TERM SIGTERM SIGQUIT KILL SIGKILL) and exec php -S , but I don't see any message.



I'm not sure what happen when ^C is pressed on a running make, if the signal (and what) is propagated only to the forked sh -c, and why that wouldn't work with docker run.



I'd like to know if someone else had this problem and solved or if someone can think of some alternative solution.



edit



Makefile:



all:

.PHONY: docker-build serve

docker-build:
docker build -f Dockerfile -t sigtest:v1 .

serve:
docker run -t --rm sigtest:v1 gosu ubuntu:ubuntu php7.0 -S 0.0.0.0:8081 -t /home/ubuntu


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu


test with make docker-build and then make serve



2nd edit



I managed to wrap docker run with a shell script (I'm not sure about trap syntax in posix shells, eventually I'll take a look)



make seems to be sending INT the first time and and then EXIT



#!/bin/sh
pid=$$
trap 'trap - INT EXIT; echo " signal received, wait..."; docker stop -t 0 sig${pid}; exit' INT EXIT
docker run --name=sig${pid} "$@"


and then in the Makefile



serve:
-./docker-run.sh -t --rm sigtest:v1 gosu ubuntu:ubuntu php7.0 -S 0.0.0.0:8081 -t /home/ubuntu


I was hoping to not have to name the instances but that's the simplest way I can think of.







docker signals gnu-make






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:04







Alex

















asked Nov 21 '18 at 15:15









AlexAlex

2,53511429




2,53511429













  • Could you post a simplified version of your Makefile which reproduces the issue?

    – Danila Kiver
    Nov 21 '18 at 16:16











  • Hello @DanilaKiver, see the edit, thanks

    – Alex
    Nov 21 '18 at 17:11











  • Had kind of similar issue before and switched to docker-compose. It has commands for managing the whole lifecycle including starting and stopping services.

    – Alex
    Nov 21 '18 at 17:47











  • Alex, sure, perhaps almost any solution removing make remove the issue, but that's the point, I been using make for years now, I'd like to continue. But you gave me an idea, maybe wrapping docker run with a script outside of the docker instead of wrapping php inside... I'll give a try, thank you

    – Alex
    Nov 21 '18 at 18:40



















  • Could you post a simplified version of your Makefile which reproduces the issue?

    – Danila Kiver
    Nov 21 '18 at 16:16











  • Hello @DanilaKiver, see the edit, thanks

    – Alex
    Nov 21 '18 at 17:11











  • Had kind of similar issue before and switched to docker-compose. It has commands for managing the whole lifecycle including starting and stopping services.

    – Alex
    Nov 21 '18 at 17:47











  • Alex, sure, perhaps almost any solution removing make remove the issue, but that's the point, I been using make for years now, I'd like to continue. But you gave me an idea, maybe wrapping docker run with a script outside of the docker instead of wrapping php inside... I'll give a try, thank you

    – Alex
    Nov 21 '18 at 18:40

















Could you post a simplified version of your Makefile which reproduces the issue?

– Danila Kiver
Nov 21 '18 at 16:16





Could you post a simplified version of your Makefile which reproduces the issue?

– Danila Kiver
Nov 21 '18 at 16:16













Hello @DanilaKiver, see the edit, thanks

– Alex
Nov 21 '18 at 17:11





Hello @DanilaKiver, see the edit, thanks

– Alex
Nov 21 '18 at 17:11













Had kind of similar issue before and switched to docker-compose. It has commands for managing the whole lifecycle including starting and stopping services.

– Alex
Nov 21 '18 at 17:47





Had kind of similar issue before and switched to docker-compose. It has commands for managing the whole lifecycle including starting and stopping services.

– Alex
Nov 21 '18 at 17:47













Alex, sure, perhaps almost any solution removing make remove the issue, but that's the point, I been using make for years now, I'd like to continue. But you gave me an idea, maybe wrapping docker run with a script outside of the docker instead of wrapping php inside... I'll give a try, thank you

– Alex
Nov 21 '18 at 18:40





Alex, sure, perhaps almost any solution removing make remove the issue, but that's the point, I been using make for years now, I'd like to continue. But you gave me an idea, maybe wrapping docker run with a script outside of the docker instead of wrapping php inside... I'll give a try, thank you

– Alex
Nov 21 '18 at 18:40












1 Answer
1






active

oldest

votes


















1














The problem is not in using make but in the Signal handling. Here is an article describing it.



As I said before docker-compose manages the whole lifecycle of your services.



Here is a basic example of your app using docker-compose. By make up and make down(including ^C ), so you can start and stop your service.
Makefile:



all:

.PHONY: docker-build serve

build:
docker-compose build

up:
docker-compose up

down:
docker-compose down


docker-compose.yml:



version: '3.3'

services:
my_service:
build: .
ports:
- "8080:8080"


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu

RUN mkdir -p /home/ubuntu
RUN php7.0 --version
ENTRYPOINT php7.0 -S 0.0.0.0:8080





share|improve this answer


























  • That would be nice but I've some other target in the makefile, for instance to build from some source and exit (without listening), with different configurations (dev and prod at least), can docker-compose handle the scenario?

    – Alex
    Nov 21 '18 at 19:18











  • docker-compose build just builds service and docker-compose up will start php server by executing ENTRYPOINT php7.0 -S 0.0.0.0:8080 line from Dockerfile. There are more complicated examples available docs.docker.com/compose/gettingstarted

    – Alex
    Nov 21 '18 at 19:24













  • thanks for the effort

    – Alex
    Nov 21 '18 at 20:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415116%2fgnu-make-and-docker-terminate-the-running-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The problem is not in using make but in the Signal handling. Here is an article describing it.



As I said before docker-compose manages the whole lifecycle of your services.



Here is a basic example of your app using docker-compose. By make up and make down(including ^C ), so you can start and stop your service.
Makefile:



all:

.PHONY: docker-build serve

build:
docker-compose build

up:
docker-compose up

down:
docker-compose down


docker-compose.yml:



version: '3.3'

services:
my_service:
build: .
ports:
- "8080:8080"


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu

RUN mkdir -p /home/ubuntu
RUN php7.0 --version
ENTRYPOINT php7.0 -S 0.0.0.0:8080





share|improve this answer


























  • That would be nice but I've some other target in the makefile, for instance to build from some source and exit (without listening), with different configurations (dev and prod at least), can docker-compose handle the scenario?

    – Alex
    Nov 21 '18 at 19:18











  • docker-compose build just builds service and docker-compose up will start php server by executing ENTRYPOINT php7.0 -S 0.0.0.0:8080 line from Dockerfile. There are more complicated examples available docs.docker.com/compose/gettingstarted

    – Alex
    Nov 21 '18 at 19:24













  • thanks for the effort

    – Alex
    Nov 21 '18 at 20:02
















1














The problem is not in using make but in the Signal handling. Here is an article describing it.



As I said before docker-compose manages the whole lifecycle of your services.



Here is a basic example of your app using docker-compose. By make up and make down(including ^C ), so you can start and stop your service.
Makefile:



all:

.PHONY: docker-build serve

build:
docker-compose build

up:
docker-compose up

down:
docker-compose down


docker-compose.yml:



version: '3.3'

services:
my_service:
build: .
ports:
- "8080:8080"


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu

RUN mkdir -p /home/ubuntu
RUN php7.0 --version
ENTRYPOINT php7.0 -S 0.0.0.0:8080





share|improve this answer


























  • That would be nice but I've some other target in the makefile, for instance to build from some source and exit (without listening), with different configurations (dev and prod at least), can docker-compose handle the scenario?

    – Alex
    Nov 21 '18 at 19:18











  • docker-compose build just builds service and docker-compose up will start php server by executing ENTRYPOINT php7.0 -S 0.0.0.0:8080 line from Dockerfile. There are more complicated examples available docs.docker.com/compose/gettingstarted

    – Alex
    Nov 21 '18 at 19:24













  • thanks for the effort

    – Alex
    Nov 21 '18 at 20:02














1












1








1







The problem is not in using make but in the Signal handling. Here is an article describing it.



As I said before docker-compose manages the whole lifecycle of your services.



Here is a basic example of your app using docker-compose. By make up and make down(including ^C ), so you can start and stop your service.
Makefile:



all:

.PHONY: docker-build serve

build:
docker-compose build

up:
docker-compose up

down:
docker-compose down


docker-compose.yml:



version: '3.3'

services:
my_service:
build: .
ports:
- "8080:8080"


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu

RUN mkdir -p /home/ubuntu
RUN php7.0 --version
ENTRYPOINT php7.0 -S 0.0.0.0:8080





share|improve this answer















The problem is not in using make but in the Signal handling. Here is an article describing it.



As I said before docker-compose manages the whole lifecycle of your services.



Here is a basic example of your app using docker-compose. By make up and make down(including ^C ), so you can start and stop your service.
Makefile:



all:

.PHONY: docker-build serve

build:
docker-compose build

up:
docker-compose up

down:
docker-compose down


docker-compose.yml:



version: '3.3'

services:
my_service:
build: .
ports:
- "8080:8080"


Dockerfile:



FROM ubuntu:16.04

RUN apt-get update &&
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends php7.0-cli gosu

RUN useradd -ms /bin/bash ubuntu

RUN mkdir -p /home/ubuntu
RUN php7.0 --version
ENTRYPOINT php7.0 -S 0.0.0.0:8080






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 19:18

























answered Nov 21 '18 at 19:07









AlexAlex

1,1021711




1,1021711













  • That would be nice but I've some other target in the makefile, for instance to build from some source and exit (without listening), with different configurations (dev and prod at least), can docker-compose handle the scenario?

    – Alex
    Nov 21 '18 at 19:18











  • docker-compose build just builds service and docker-compose up will start php server by executing ENTRYPOINT php7.0 -S 0.0.0.0:8080 line from Dockerfile. There are more complicated examples available docs.docker.com/compose/gettingstarted

    – Alex
    Nov 21 '18 at 19:24













  • thanks for the effort

    – Alex
    Nov 21 '18 at 20:02



















  • That would be nice but I've some other target in the makefile, for instance to build from some source and exit (without listening), with different configurations (dev and prod at least), can docker-compose handle the scenario?

    – Alex
    Nov 21 '18 at 19:18











  • docker-compose build just builds service and docker-compose up will start php server by executing ENTRYPOINT php7.0 -S 0.0.0.0:8080 line from Dockerfile. There are more complicated examples available docs.docker.com/compose/gettingstarted

    – Alex
    Nov 21 '18 at 19:24













  • thanks for the effort

    – Alex
    Nov 21 '18 at 20:02

















That would be nice but I've some other target in the makefile, for instance to build from some source and exit (without listening), with different configurations (dev and prod at least), can docker-compose handle the scenario?

– Alex
Nov 21 '18 at 19:18





That would be nice but I've some other target in the makefile, for instance to build from some source and exit (without listening), with different configurations (dev and prod at least), can docker-compose handle the scenario?

– Alex
Nov 21 '18 at 19:18













docker-compose build just builds service and docker-compose up will start php server by executing ENTRYPOINT php7.0 -S 0.0.0.0:8080 line from Dockerfile. There are more complicated examples available docs.docker.com/compose/gettingstarted

– Alex
Nov 21 '18 at 19:24







docker-compose build just builds service and docker-compose up will start php server by executing ENTRYPOINT php7.0 -S 0.0.0.0:8080 line from Dockerfile. There are more complicated examples available docs.docker.com/compose/gettingstarted

– Alex
Nov 21 '18 at 19:24















thanks for the effort

– Alex
Nov 21 '18 at 20:02





thanks for the effort

– Alex
Nov 21 '18 at 20:02




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415116%2fgnu-make-and-docker-terminate-the-running-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents