How to run `httpd` in Docker in detached mode using CMD in Dockerfile?

Viewed 2878

This is my Dockerfile - The image builds successfully but it doesn't run, it stops. I want to access the website being served from the Apache server from Docker container.

# build environment
FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm ci --silent
RUN npm install react-scripts@3.4.1 -g --silent
COPY . ./
RUN npm run build:staging

# production environment
FROM httpd:latest
COPY --from=build /app/build /usr/local/apache2/htdocs

EXPOSE 80

CMD ["httpd"]
1 Answers

You should delete CMD ["httpd"], see this:

CMD ["httpd-foreground"]

There is already a foreground httpd there.

Finally, Why CMD ["httpd"] won't work?

The CMD defined in Dockerfile would be acting as PID1 of your container. In docker, if PID1 exits, then, the container will also exit.

  • If use CMD ["httpd-foreground"], the apache process will always be in front, so the process will not exit, then the container is alive.
  • If use CMD ["httpd"], the httpd will directly exit after executing, then PID1 exits, so the container exits.
Related