简体   繁体   中英

Docker : Start mysql and apache from entrypoint or CMD

Building a docker image for development, I want to start automatically mysql and apache when I run the image.

If I log into the container and run "service apache2 start" and "service mysql start" it works. But if I put in entrypoint or CMD it fails. I was able to start apache by putting ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"] but I was not able to start mysql programmatically.

I tried many many things. Most of the time if fails silently in that the container is not running, other time I got : docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \\"/etc/init.d/mysql start\\": stat /etc/init.d/mysql start: no such file or directory"

This is what I have so far :

FROM debian:wheezy

RUN apt-get update && \
    apt-get install -y libmcrypt-dev \
    subversion ssl-cert nano wget unzip && \
    echo "deb http://packages.dotdeb.org wheezy-php56 all" >> /etc/apt/sources.list.d/dotdeb.list && \
    echo "deb-src http://packages.dotdeb.org wheezy-php56 all" >> /etc/apt/sources.list.d/dotdeb.list && \
    wget http://www.dotdeb.org/dotdeb.gpg -O- | apt-key add - && \
    echo mysql-server-5.5 mysql-server/root_password password yourpass | debconf-set-selections && \
    echo mysql-server-5.5 mysql-server/root_password_again password yourpass | debconf-set-selections && \
    apt-get update && \
    apt-get install -y \
    apache2 apache2-doc apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common libapache2-mod-php5 \
    openssl php-pear php5 php5-cli php5-common php5-curl php5-gd php5-mcrypt php5-mysql php5-memcache php5-readline \
    subversion ssl-cert nano wget unzip \
    mysql-server-5.5 mysql-client mysql-client-5.5 mysql-common && \
    /etc/init.d/mysql start && \
    mysql -u root -pyourpass -e "create database mydb;" && \
    rm -rf /var/lib/apt/lists/* && \
    rm /etc/apache2/sites-enabled/000-default && \
    mkdir -p /var/www/html && \
    chown www-data:www-data -R /var/www/html/

COPY conf/etc/ /etc/
COPY mydump.sql /var/www/html/mydump.sql

RUN /etc/init.d/mysql start && \
    mysql -u root -pyourpass -h localhost mydb < /var/www/html/mydump.sql && \
    rm /var/www/html/mydump.sql

VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2", "/var/lib/mysql"]

EXPOSE 80 443 3306

Your way of starting either Apache or Mysql looks wrong to me

If I look at the most popular Apache on hub.docker.com the Dockerfile shows how to start Apache. The last line of the Dockerfile is

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

For the reference Mysql, the last line of the Dockerfile is

CMD ["mysqld"]

So you can look at supervisor or any other similar tool like S6 or daemontools in order to start both Apache and Mysql in the Docker way.

A model often seen is to include a script (bash, shell, etc) in your Docker image, and then use that script as the entrypoint for your application. See that described in https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#entrypoint

So, put the things you're starting in a docker-entrypoint.sh script, COPY the script in, and reference it from the ENTRYPOINT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM