How to restart apache2 without terminating docker container?
Asked Answered
Q

7

29

I am using as a base the php docker container with the tag:

php:5.6-apache

When I try to restart the apache2 inside the container, the container stops:

root@phalconapp:/var/www/html# service apache2 restart
Restarting web server: apache2Terminated
root@phaclonapp:/var/www/html#
me@myLocalComputer:

How to restart apache2 without stopping the container?

I want to play around with the container and customize it, before I put my changes into the dockerfile. I want to install some extension and for them to work I need to restart apache for the changes to take effect.

This is the log file via:

Attaching to dltasklight_phlaconapp_1
phlaconapp_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
phlaconapp_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
phlaconapp_1 | [Mon May 30 10:19:24.556154 2016] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.22 configured -- resuming normal operations
phlaconapp_1 | [Mon May 30 10:19:24.556181 2016] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
phlaconapp_1 | [Mon May 30 10:21:11.754993 2016] [mpm_prefork:notice] [pid 1] AH00169: caught SIGTERM, shutting down
dltasklight_phlaconapp_1 exited with code 0
Quibbling answered 30/5, 2016 at 10:25 Comment(0)
A
54

If you use apache as the primary service to keep your running container, you can NOT reboot it. Simply because you built the image and sets the CMD with it.

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

Try to reload without restart a service:

/etc/init.d/apache2 reload
Atypical answered 4/6, 2016 at 1:13 Comment(4)
This is what I ended up doing while developing the dev stack. Thanks for writing it out and +1. I am not accepting this solution as running the reload was not a requirement in the end as if the extensions are installed before the apache starts, it's no requirement for it to restart it.Quibbling
Accepted this answer, as the other incomming answer are going besides the point. Restarting was not a requirement, reloading the config was, and this helped me built a proper Dockerfile.Quibbling
Good. I do not remember having another command that restarts only as apache configurations. This command are really useful for this problem :}Atypical
Hello @MagnoC, what is your base image of Dockerfile?Atypical
T
5

My solution to this was to exit my bash shell into the container, and just restart the container outside of Docker. Because Apache is set as the primary service, this also restarts Apache, and doesn't crash the container.

docker restart <container>
Temperate answered 18/7, 2017 at 4:5 Comment(1)
This command restarts the container but are really useful in other cases :)Atypical
S
3

sudo docker kill --signal="USR1" your_appache_container

Other signals that you can use to achieve the following :

Stop Now Signal: TERM

Graceful Restart Signal: USR1

Restart Now Signal: HUP

Graceful Stop Signal: WINCH

From: this website

Sezen answered 30/5, 2016 at 10:29 Comment(3)
This doesn't work for me, I think you need to give more information in your answer. I get the following error, Error response from daemon: Cannot kill container –signal=USR1: No such container: –signal=USR1. I made sure to enter correct container by ID and Name, both same error.Effluence
Please replace the character before "signal" with two dashes (it war probably autocorrected by stackoverflow's input field).Periwinkle
I faced with issue that for some reason it's not enough to send signal once, it's needed to send it twice #55262985Norrisnorrv
S
1

I want to customize the container, I need to install some extension and for them to work I need to restart apache for the changes to take effect.

This is against the Docker's immutable infrastructure principle. IMHO, you are using the docker container similar to a full blown VM. Instead, I would suggest you to treat the docker image as the final artifact and version it. Note: This is just my humble opinion, you may have a valid usecase which I am not aware of, which I am curious to find out.

Shine answered 18/7, 2017 at 16:2 Comment(1)
This was while trying to setup a development stack inside a docker container without running docker build. In the end, I ended up with a dockerfile, I just wanted to try something out. See my comment on Cauê Alves answer.Quibbling
R
1

Inside docker, I use a command like this:

exec /usr/sbin/httpd -D FOREGROUND
Reconstructionism answered 19/11, 2021 at 11:23 Comment(2)
in what linux distro & version?Moncada
in DockerFile: FROM alpine:3.13Reconstructionism
P
0

But before going, does your apache fails while doing the restart? Thats how it exits? In that case, please do make it run by setting right configurations and having a look at those logs.

One method you can try is login to the container (to bash) and you could always have a docker commit at a point where it works. You can then change your base container image to that.

I had workarounds for similar situations by building new images from these committed ones using different ENTRYPOINT's in my Dockefile.

Passion answered 18/7, 2017 at 6:10 Comment(0)
P
0

Inside the container

All command variants here are for running inside the container.

Gentler method:

Lets currently busy requests finish before having them exit.

apachectl graceful

or

httpd -k graceful

The USR1 or graceful signal causes the parent process to advise the children to exit after their current request (or to exit immediately if they're not serving anything). The parent re-reads its configuration files and re-opens its log files. As each child dies off the parent replaces it with a child from the new generation of the configuration, which begins serving new requests immediately.

Harsher method:

apachectl restart

or

httpd -k restart

Sending the HUP or restart signal to the parent causes it to kill off its children like in TERM, but the parent doesn't exit. It re-reads its configuration files, and re-opens any log files. Then it spawns a new set of children and continues serving hits.

Source: https://httpd.apache.org/docs/2.4/stopping.html

Pringle answered 2/6 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.