How to start apache2 automatically in a ubuntu docker container?

后端 未结 3 1022
情歌与酒
情歌与酒 2020-12-05 05:05

I am trying to create a Dockerfile that will start apache automatically. Nothing has worked. But If I log into the container and run service apache2 start it wo

相关标签:
3条回答
  • 2020-12-05 05:21

    My project was slightly different where I installed a bunch of other stuff, but the apache start portion matched above. Once I built this image and used it, my server started fine.

    FROM ubuntu:latest
    
    #install all the tools you might want to use in your container
    RUN apt-get update
    RUN apt-get install curl -y
    RUN apt-get install vim -y
    #the following ARG turns off the questions normally asked for location and timezone for Apache
    ARG DEBIAN_FRONTEND=noninteractive
    RUN apt-get install apache2 -y
    
    #change working directory to root of apache webhost
    WORKDIR var/www/html
    
    #copy your files, if you want to copy all use COPY . .
    COPY index.html index.html
    
    #now start the server
    CMD ["apachectl", "-D", "FOREGROUND"]
    
    0 讨论(0)
  • 2020-12-05 05:28

    For me last line with CMD was wrong:

    # it helped me
    CMD ["apachectl", "-D", "FOREGROUND"]
    
    0 讨论(0)
  • 2020-12-05 05:30

    The issue is here: CMD service apache2 start When you execute this command process apache2 will be detached from the shell. But Docker works only while main process is alive.

    The solution is to run Apache in the foreground. Dockerfile must look like this: (only last line changed).

    FROM ubuntu
    
    # File Author / Maintainer
    MAINTAINER rmuktader
    
    # Update the repository sources list
    RUN apt-get update
    
    # Install and run apache
    RUN apt-get install -y apache2 && apt-get clean
    
    #ENTRYPOINT ["/usr/sbin/apache2", "-k", "start"]
    
    
    #ENV APACHE_RUN_USER www-data
    #ENV APACHE_RUN_GROUP www-data
    #ENV APACHE_LOG_DIR /var/log/apache2
    
    EXPOSE 80
    CMD apachectl -D FOREGROUND
    
    0 讨论(0)
提交回复
热议问题