-1

This is my Dockerfile contents:

from ubuntu:latest

#ARG DEBIAN_FRONTEND=noninteractive
COPY entrypoint.sh /sbin/entrypoint.sh
COPY info.php /var/www/html/
ENV TZ=Asia/Tehran
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update && apt-get upgrade -y && apt-get install apache2 php -y
ENV APACHE_CONF_DIR=/etc/apache2
RUN chmod 755 /sbin/entrypoint.sh
CMD ['/sbin/entrypoint.sh']

And this is my entrypoint.sh contents:

#!/bin/bash

source /etc/apache2/envvars
exec apache2 -D FOREGROUND

Image makes fine with no issue, and it also starts without any issues, but the file entrypoint.sh does not start. When I exec -it into the container and run this file manually, apache2 works fine and my info.php is accessible through the web.

I create container with this command (to make it running):

docker run -dp80:80 -v /logs:/var/log a2 bash

Then I go to the container docker exec -it NAME, then run /sbin/entrypoint.sh and I see there's no problem.

I create another container docker run -dp81:80 a2, but my file does not run. When I use docker run -dp81:80 a2 /sbin/entrypoint.sh, then my file runs and my info.php is show in the internet browser.

Would you please help me where is my issue that without using my file name at the end of command, container does exit immediately after started?

Saeed
  • 3,255
  • 4
  • 17
  • 36

1 Answers1

1

EDIT

From the CMD docs, when using the exec format (specifying args in []) you need to use double quotes " instead of single quotes ' since args are parsed as a JSON array. I think you should change your command to:

CMD [ "/sbin/entrypoint.sh" ]

See this answer comparing CMD vs ENTRYPOINT for more information.

You override the container's CMD when you specify the command to run when you start the container. An example is putting bash after the image name: docker run -d -p 80:80 -v /logs:/var/log a2 bash. Instead of running the /sbin/entrypoint.sh script, it's running bash.

You can consider using ENTRYPOINT instead of CMD if you want to make sure the entrypoint script is run on container startup and cannot be overridden.

ENTRYPOINT ["/sbin/entrypoint.sh"]

As David Maze points out in the comments, you could also modify the entrypoint script to only set environment variables and use CMD exec "$@" in the Dockerfile to run apache as a main container process.

cam
  • 4,409
  • 2
  • 24
  • 34
  • Thanks, but I use `docker run ... bash` only to debug if the file runs with absolute path. I'm creating another image with `ENTRYPOINT`, but why then https://github.com/romeOz/docker-apache-php/blob/master/7.3/Dockerfile works fine? I tried to create a simple image like this, but did not work. Using `ENTRYPOINT` did not work in `Dockerfile`. – Saeed Feb 16 '21 at 10:23
  • 1
    @Saeed My bad, I see. And to clarify, what had happened when you ran `docker run -d -p 80:80 -v /logs:/var/log a2`, specifically without the `bash` command? By specifying arguments after the `run` command, you'll override the `CMD` you wrote. – cam Feb 16 '21 at 10:31
  • The container exits when I check `docker ps` and `docker ps -a`. Only when I give him an argument like `bash` or `/sbin/entrypoint.sh`, it will be running. But the link I provided in comment works fine without any argument. – Saeed Feb 16 '21 at 11:24
  • 1
    @Saeed Per the docs, you should use double quotes around words in `CMD` when using the exec format (in `[]`). Can you try that instead? I will edit my response to match. – cam Feb 16 '21 at 11:56
  • 2
    (I generally recommend using `CMD` instead of `ENTRYPOINT` because it's easier to override at `docker run` time. This question also would work well with a pattern of an `ENTRYPOINT` setup wrapper script that initialized the environment variables and then `exec "$@"` to run the `CMD` as the main container process.) – David Maze Feb 16 '21 at 13:06
  • 1
    Thank you for bringing that up, that's a good point @DavidMaze. I remember you mentioned this on another question and I forgot when answering this. – cam Feb 16 '21 at 13:09