3

I am downloaded a debian image for docker and i have created a container from it. I haver successfully installed apache and mysql on this container (from /bin/bash).

I want to make this docker container running in background.

I have tried a lot of tutorials (i have created images with Dockerfile) but nothing really works. Apache and mysql were run as root...

So i have launched this command:

     docker run -d -p 80:80 myimagefile /bin/bash -c "while true; do sleep 10; done"

Then i have attached a /bin/bash with exec command and i started manually mysql and apache2 (/etc/init.d/ scripts). When i type CTRL-D, the bash is killed but the container stills in background, with mysql and apache alive !

I am wondering if this method is correct or is it something ugly ? Is there a best way to do this ? I do not want to write a Dockerfile that describes how to install apache and mysql. I have made my own image, with my application and all prerequisites. I just want to start a container from my image and start automatically apache and mysql.

I have a second question: With my method, the container is not reloaded if i reboot physical computer. How can i start it automatilcy with persistence of data ?

Thanks

Bob5421
  • 7,757
  • 14
  • 81
  • 175

1 Answers1

2

I would suggest using running mysql and apache in separate containers. Additionally the docker hub already has container images that you could re-use:

The following is an example of a docker-compose file that describe how to launch Drupal

version: '2'
services:
  db:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=letmein
      - MYSQL_DATABASE=drupal
      - MYSQL_USER=drupal
      - MYSQL_PASSWORD=drupal
    volumes:
      - /var/lib/mysql
  web:
    image: drupal
    depends_on:
      - db
    ports:
      - "8080:80"
    volumes:
      - /var/www/html/sites
      - /var/www/private

Run as follows

$ docker-compose up -d
Creating dockercompose_db_1
Creating dockercompose_web_1

Which exposes Drupal on port 8080

$ docker-compose ps
       Name                     Command             State          Ports         
--------------------------------------------------------------------------------
dockercompose_db_1    docker-entrypoint.sh mysqld   Up      3306/tcp             
dockercompose_web_1   apache2-foreground            Up      0.0.0.0:8080->80/tcp 

Note:

  • When running the drupal installer, configure it to connect to a host called "db", which is the mysql container.
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • in fact i want to store my application files (php) too and my empty database structure in the images. – Bob5421 Sep 23 '16 at 19:04
  • @Bob5421 That's where the volume mappings come in. You can configure where the storage is located. Storing this inside the image is less efficient and not advised. – Mark O'Connor Sep 23 '16 at 19:31
  • Do you mean a should for example create a partition for /var/www then mount this partition to /var/www and keep an officiel apache2 image ? – Bob5421 Sep 23 '16 at 20:18
  • Use the httpd image and mount a volume @ /usr/local/apache2/htdocs. See the image doco: https://hub.docker.com/_/httpd/ – Mark O'Connor Sep 23 '16 at 21:00
  • I have a cloud solution that gives me the opportunity to create volumes which works with openslack. Do you think i can work with this kind of volume for my htdocs and /var/lib/mysql folders ? – Bob5421 Sep 24 '16 at 09:08
  • RexyRay is an example of a plugin that is compatible with Openstack: https://github.com/emccode/rexray – Mark O'Connor Sep 25 '16 at 10:42
  • I do not understand something in your docker-compose sample file, about the volumes: You do not specify a mount point. What does that mean ? I have tried a find on my full disk and i did not find where the volume are mounted by default... – Bob5421 Sep 26 '16 at 20:33
  • Normally stored under "/var/lib/docker". Can be customized. For more information see: http://stackoverflow.com/questions/19234831/where-are-docker-images-stored-on-the-host-machine – Mark O'Connor Sep 26 '16 at 20:51