Search code examples
mysqllinuxapachedockerzfs

dockerfile - Unable to connect error


So here's what i have to do: i need to set up some containers automatically using docker. One of them is liek this: Debian Squeeze with limited CPU shares and limited memory (1 cpu share and 512 mb memory),preinstalled apache2,build-essential,php5,mysql-server-5.5,openssh-server and with some ports opened (8000 for Apache and 1500 for MySQL). So i created the following dockerfile :

FROM debian:squeeze

MAINTAINER Name < email : >

# Update the repository sources list

RUN apt-get update


# Install apache, PHP, and supplimentary programs. curl and lynx-cur are for debugging the container.
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 build-essential php5 mysql-server openssh-server libapache2-mod-php5 php5-mysql php5-gd php-pear php-apc php5-curl curl lynx-cur

# Enable apache mods.
RUN a2enmod php5
RUN a2enmod rewrite

# Manually set up the apache environment variables
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid

EXPOSE 80

# Copy site into place.
ADD www /var/www/site

# Update the default apache site with the config we created.
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf

# By default, simply start apache.
CMD /usr/sbin/apache2ctl -D FOREGROUND

#CMD [ "mysqladmin -u root password mysecretpasswordgoeshere"]

EXPOSE 3306

the content of apache-config.conf is this:

<VirtualHost *:80>

  ServerAdmin [email protected]
  DocumentRoot /var/www/site

  <Directory /var/www/site/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order deny,allow
      Allow from all
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

and in www folder i put a php file with this code:

<?php
        $connect=mysql_connect("localhost:1500","root","") or die("Unable to Connect");
?>

to test the connection to the mysql server

then

i build all this into an image like this:

sudo docker build --rm --tag="tag_name" .

and then i run the image like this

sudo docker run -c=1 -m="512m" --net=bridge -p 8000:80 -p 1500:3306 -d --name="container_name" tag_name

It seems to work,the apache server works when i access localhost:8000/site in my browser but is shows "Unable to connect". what am i doing wrong? And another problem is that,the contaienr is running but i can't attach to it.I run this command

sudo docker attach CONTAINER_ID 

and then nothing happens,can't do anythign else from there,What am i doing wrong?

I have to build few more dockerfiles similar to this to create containers.All those must be hosted on a ZFS file system and i have to configure a container repository of 50gb based on it,what does this mean and how do i do that?

I'm sorry for my english,it's not my native language :( Thank you in advance


Solution

  • MySQL issue

    in the PHP code

    $connect=mysql_connect("localhost:1500","root","") or die("Unable to Connect");
    

    localhost refers to the container IP address. Since there is no MySQL server running in that container the connection will fail.

    In this gist, I've changed a bit your example to have the container start both MySQL and Apache (I assume this was your first intent) using the following instruction: CMD bash -c '(mysqld &); /usr/sbin/apache2ctl -D FOREGROUND' and changed the PHP code to connect to the MySQL server on localhost:3306.

    Docker attach

    The docker attach command is meant to allow you to interact with the process currently running in the foreground of a container. Unless that process is a shell, it won't provide you with a shell in that container.

    Take this example:

    Start a container running a shell process

    docker run -it --rm base bash
    

    You are now in interactive mode in your container and can play around with the shell running in the foreground in that container:

    root@de8f16a13571:/# ls
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  selinux  srv  sys  tmp  usr  var
    

    if you now exit the shell by typing exit the shell process will end, and as that was the process running in the foreground in the container, that container will stop.

    root@de8f16a13571:/# exit exit $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

    Now start a new container named test running bash again:

    docker run -it --name test base bash
    

    verify you can interact with it and detach from it by hitting keys Ctrl+p+q. You end up back in the docker host shell.

    verify that the container named test is still running:

    $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 81f0f1094f4a base:latest "bash" 6 seconds ago Up 5 seconds test

    You can then use the docker attach command to attach to the bash program in the container:

    docker attach test
    
    root@81f0f1094f4a:/# ls
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  selinux  srv  sys  tmp  usr  var
    

    ZSH

    And regarding ZSH, I don't know what all that means either. Also note that having 3 questions at once makes it difficult for the community to come up with a single answer that would answer all 3 ; maybe consider posting a new question for those.

    Please comment if my assumptions about how you run MySQL or what your intent is with docker attach are wrong.