skip to Main Content

I downloaded a docker image with mariadb and phpmyadmin,
then wrote two dockerfiles below..

# dockerfile A
FROM alfvenjohn/raspbian-buster-mariadb-phpmyadmin

CMD /etc/init.d/mysql start && /etc/init.d/apache2 start
# dockerfile B
FROM alfvenjohn/raspbian-buster-mariadb-phpmyadmin

CMD service mysql start && /usr/sbin/apachectl -D FOREGROUND 

dockerfile B worked well,

but dockerfile A failed.

I can build image from dockerfileA,

then spin-up container from it docker run -it -p 80:80 <img id> bash

the container up successfully,

but while I inside the container, I found the services of mariadb and apache2 not working.

After I execute /etc/init.d/mysql start && /etc/init.d/apache2 start,

mariadb and apache2 works!

Trying to get error messages by docker logs <container id>, but got nothing.

What my question is

"If I run the docker image without dockerfile just by commands,

like what I did in dockerfile A. The container works well. "

$ docker run -it -p 80:80 alfvenjohn/raspbian-buster-mariadb-phpmyadmin bash
$ /etc/init.d/mysql start && /etc/init.d/apache2 start

Why? Didn’t dockerfile A do the same thing, as I spin up my container with commands ?

2

Answers


  1. You need to remove the bash at this end of the command. This replace the command inside your dockerfile.

    docker run -d -p 80:80 <img id>
    

    You can use this command to connect inside the container afterward:

    docker exec -it <container_id> bash 
    
    Login or Signup to reply.
  2. A Docker image runs a single command only. When that command exits, the container exits too.

    This combination usually means a container runs a single server-type process (so run MySQL and Apache in separate containers), and it means the process must run in the foreground (so the lifetime of the container is the lifetime of the server process).

    In your setup where you launch an interactive shell instead of the image CMD, you say you can start the servers by running

    /etc/init.d/mysql start && /etc/init.d/apache2 start
    

    This is true, and then you get a command prompt back. That means the command completed. If you run this as an image’s CMD then "you get a command prompt back" or "the command completed" means "the container will exit".

    Generally you want to launch separate database and Web server containers; if you have other application containers you can add those to the mix too. Docker Compose is a common tool for this. You may want to look through Docker’s sample applications for some examples, or other SO questions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search