advapi
advapi

Reputation: 3907

How to keep httpd docker image alive when executing a script

I've created an imaged based on httpd:2.4 and it was working. I had to add a script since I need to perform some action on ENTRYPOINT but now when the container run, it exits immediately.

On Nodes container I run node index.js inside the script so it stays alive, while here it quits. I think this happens since It executes the script, then for docker it's ok to quit (it doesn't know what the image has to do)

Here's my dockefile and then the startup.sh

dockerfile

FROM httpd:2.4 

RUN \
  apt-get update \
  && apt-get -y install gettext-base \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*
  
LABEL XXX

ENV FE_VERSION=1.0

# Create app directory
WORKDIR /usr/local/apache2/htdocs/xxx/

COPY . /usr/local/apache2/htdocs/xxx/
COPY ./docker/startup.sh .

#COPY .htaccess /usr/local/apache2/htdocs/xxx
COPY ./docker/my-httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./docker/httpd-vhosts.conf /usr/local/apache2/conf/httpd-vhosts.conf

RUN chmod -R 755 /usr/local/apache2/htdocs/

EXPOSE 80

ENTRYPOINT [ "/bin/sh", "startup.sh" ]

startup.sh

envsubst \$parameters_in_milliseconds,\$server,\$checkServerConnectionInterval,\$detailRefreshInterval,\$customMainPopupTimerInterval,\$customTimerSectionTimerInterval,\$machineParametersTimerInterval,\$workingTimeInterval,\$ioUnitResourcesInterval,\$loggedUsersInterval < ./params/params.json.empty > ./params/params.json

Any suggestion? Thanks in advance

Upvotes: 0

Views: 1835

Answers (2)

David Maze
David Maze

Reputation: 159445

If your Dockerfile has an ENTRYPOINT, that is the only process the container runs. If it also has a CMD, it gets passed as command-line arguments into the entrypoint.

It's a standard use of ENTRYPOINT to do some first-time setup, then run the CMD. To do this the script needs to end with exec "$@" to run the command that got passed as arguments.

#!/bin/sh
envsubst ...
exec "$@"

If you specify ENTRYPOINT in your Dockerfile, you also need to specify CMD, even if there was a CMD defined in the base image (see the "note" in the link above). The ENTRYPOINT declaration must use the JSON-array form or the CMD will get lost.

So I'd change the end of the Dockerfile slightly to

RUN chmod +x startup.sh
ENTRYPOINT ["./startup.sh"]
CMD ["httpd-foreground"]

(One thing it's common to want to do, especially while you're developing an image, is poke around inside what got built:

docker run --rm myimage ls -l xxx
# why is index.html executable?

This approach does the first-time setup and then runs the command, so you'll see the "real" config file. If you "bake" the command into the entrypoint script it's difficult to run an alternate command.)

Upvotes: 1

DannyB
DannyB

Reputation: 14816

If you are replacing the original entrypoint with your own script, and wish to retain the same functionality as the original, you need to add the original command to the end of your entrypoint.

For example, custom entrypoint for apache:

# entrypoint
#!/usr/bin/env bash
echo "===> Doing some important things and then starting the server"
httpd-foreground

Which will work nicely with this Dockerfile:

# Dockerfile
FROM httpd:2.4
COPY entrypoint /usr/local/bin/entrypoint
RUN chmod +x /usr/local/bin/entrypoint
ENTRYPOINT ["entrypoint"]

See the original Dockerfile and its entrypoint.

Upvotes: 0

Related Questions