7

Consider the following Dockerfile:

FROM ubuntu:16.04

RUN apt-get update && \
    apt-get install -y apache2 && \
    apt-get clean

ENTRYPOINT ["apache2ctl", "-D", "FOREGROUND"]

When running the container with the command docker run -p 8080:80 <image-id>, then the container starts and remains running, allowing the default Apache web page to be accessed on https://localhost:8080 from the host as expected. With this run command however, I am not able to quit the container using Ctrl+C, also as expected, since the container was not launched with the -it option. Now, if the -it option is added to the run command, then the container exits immediately after startup. Why is that? Is there an elegant way to have apache run in the foreground while exiting on Ctrl+C?

Sam Herrmann
  • 6,293
  • 4
  • 31
  • 50
  • 1
    just out of curiosity have you tried using `CMD` instead of `ENTRYPOINT`? also.. you know that `-it` is not one command but two, `-i` and `-t`? – mad.meesh Jan 03 '18 at 23:35
  • yeah, I tried CMD and it yields the same outcome. I have also tried using the shell form of ENTRYPOINT instead of the exec form, and it also yields the same outcome. I am aware that -it is short for -i and -t, but as far as I know both are required to interact with the container. – Sam Herrmann Jan 03 '18 at 23:53
  • This all seems to work as you expect when I run it myself. I ran `docker run --rm -it image-id` and the container remains running until I send INT with CTRL+C. My version of docker is `Docker version 17.09.1-ce, build 19e2cf6` – Stephen Crosby Jan 03 '18 at 23:55
  • hmmm that's interesting, it's also working on my machine at home where I'm running `Docker version 17.12.0-ce, build c97c6d6`. I'll have to check tomorrow what version I am running at work where I was experiencing the issue. – Sam Herrmann Jan 04 '18 at 02:06
  • Did you receive any error messages or logs from the failing container? – BMitch Jan 04 '18 at 12:08
  • My machine on which I am experiencing the issue is also `Docker version 17.12.0-ce, build c97c6d6` (the same as my home machine). The obvious difference between this machine and my home machine is that this machine is running CentOS 7, whereas my home machine is running Ubuntu 16.04. I would hope that this does not influence the container. @BMitch, there are no error messages and I have no logs. The container exits with status code 0. I will keep digging. – Sam Herrmann Jan 04 '18 at 14:57
  • Can you post a `docker inspect` of the failing container? – BMitch Jan 04 '18 at 15:03
  • @BMitch here is the docker inspect: https://gist.github.com/samherrmann/0a2919f696ccf5825cba8a4a99755aae – Sam Herrmann Jan 04 '18 at 15:50
  • Thanks. Looked over the inspect and everything looks right from this side. Only other thing I can think of with CentOS is SELinux. And before you spend too long debugging, give it a reboot (I hate that answer too). – BMitch Jan 04 '18 at 15:59
  • Still no luck with reboot. I also tried it on another CentOS 7 machine with the same result. I may have to park this for now since it's currently not a blocking issue for me. – Sam Herrmann Jan 04 '18 at 16:46
  • I just found that the same question was asked [here](https://stackoverflow.com/questions/46109796/why-httpd-container-exits-immediately-without-any-error-in-docker). The one provided answer there does not solve the problem for me though. – Sam Herrmann Jan 10 '18 at 03:55

5 Answers5

5

This behaviour is caused by Apache and it is not an issue with Docker. Apache is designed to shut down gracefully when it receives the SIGWINCH signal. When running the container interactively, the SIGWINCH signal is passed from the host to the container, effectively signalling Apache to shut down gracefully. On some hosts the container may exit immediately after it is started. On other hosts the container may stay running until the terminal window is resized.

It is possible to confirm that this is the source of the issue after the container exits by reviewing the Apache log file as follows:

# Run container interactively:
docker run -it <image-id>

# Get the ID of the container after it exits:
docker ps -a

# Copy the Apache log file from the container to the host:
docker cp <container-id>:/var/log/apache2/error.log .

# Use any text editor to review the log file:
vim error.log

# The last line in the log file should contain the following:
AH00492: caught SIGWINCH, shutting down gracefully

Sources:

Sam Herrmann
  • 6,293
  • 4
  • 31
  • 50
  • 3
    "Due to the lack of other signal numbers and given the fact that httpd normally runs detached from a terminal the SIGWINCH signal was chosen to instruct httpd to do a graceful shutdown." (from the apache bugzilla) Like, isn't SIGTERM thought for exact that case? SIGKILL is the "stop now, don't shutdown cleanly" command after all – DBX12 Jun 25 '19 at 11:51
4

All that you need to do is pass the -d option to the run command:

docker run -d -p 8080:80 my-container
yamenk
  • 46,736
  • 10
  • 93
  • 87
  • 2
    Thanks for responding yamenk, unfortunately this does not answer the question. Using the `-d` option runs the container in the background, not in the foreground as desired. – Sam Herrmann Jan 04 '18 at 14:22
  • That would mean it keeps running in the detached mode, the user wants it to in run in "not in detached" mode. – Majid Ali Khan Feb 12 '20 at 12:44
  • Right. This answer means the container (and apache) will not output to the host's stdout and Ctrl-C will not work to stop the container. – Jason Oct 02 '21 at 13:32
3

As yamenk mentioned, daemonizing works because you send it to the background and decouple the window resizing.

Since the follow-up post mentioned that running in the foreground may have been desirable, there is a good way to simulate that experience after daemonizing:

docker logs -f container-name

This will drop the usual stdout like "GET / HTTP..." connection messages back onto the console so you can watch them flow.

Now you can resize the window and stuff and still see your troubleshooting info.

jmcsagdc
  • 31
  • 1
  • 5
  • 1
    I think this is a good alternative. It won't allow Ctrl-C, but I would suggest using `docker stop container-name` to stop the container and possibly `docker container rm container-name` depending on your workflow. I have scripts I wrote called `start.sh` and `stop.sh` in my dev directory. The `start.sh` will both start the container with `-d` and start showing the logs to stdout. The `stop.sh` will stop the container and remove it. – Jason Oct 02 '21 at 13:37
0

I am also experiencing this problem on wsl2 under Windows 10, Docker Engine v20.10.7

Workaround:

# start bash in httpd container:
docker run --rm -ti -p 80:80 httpd:2.4.48 /bin/bash
# inside container execute:
httpd -D FOREGROUND

Now Apache httpd keeps running until you press CTRL-C or resize(?!) the terminal window.

After closing httpd, type:

exit

to leave the container

anneb
  • 5,510
  • 2
  • 26
  • 26
0

A workaround is to pipe the output to cat:

docker run -it -p 8080:80 <image-id> | cat

NOTE: It is important to use -i and -t.

Ctrl+C will work and resizing the terminal will not shut down Apache.

xOneca
  • 842
  • 9
  • 20