Wednesday, December 31, 2014

Linux Docker containers - LAMP for web development


This is a straight forward guide for configuring Docker to have a Debian LAMP workstation:
This guide should work on any Debian-based distro.
Please read carefully the entire post if you are new to docker.

Install docker: sudo apt-get install docker 
Download debian Docker image: sudo docker pull debian 
Run root shell inside container: sudo docker run -i -t debian /bin/bash
Now you're inside the Debian image, execute the following commands:
apt-get update && apt-get install apache2
apt-get install mysql-server mysql-client
apt-get install php5 php5-mysql libapache2-mod-php5
apt-get install openssh-server
apt-get install vim
# I'm used to vim, you can omit this step
apt-get install git # I use git for version control. You can install subversion or omit this step
# Now let's autostart the services
echo service ssh start >> /etc/bash.bashrc
echo service mysql start >> /etc/bash.bashrc
echo service apache2 start >> /etc/bash.bashrc
 
# Back to your host
exit

Now you can commit your changes to the image
Get container id: sudo docker ps -a
Commit the changes: sudo docker commit <id> <descr> # Replace <id> with the container id from previous step and <descr> with description of image.
Get image name: sudo docker images
Assign port forwarding for apache and ssh (replace port numbers as you see fit): sudo docker run -i -t -p 800:80 -p 2222:22 <image_name> /bin/bash
Commit again: sudo docker commit <id> <descr> 
     

That's it, to start the container again you need it's id (that can be obtained with the command "sudo docker ps -a")
sudo docker start <container id>
sudo docker attach <container id>

I use this one liner:
id=<container id>; sudo docker start $id && sudo docker attach $id

You don't necessarily need to attach to the container, but if you are going to commit changes and edit files in the container you'll need to.

It's important to note that "docker run" creates a new image without uncommitted changes. What you want to use most of the times is start & attach.

To access it's webserver, on a browser type "localhost:800", to log in via ssh on host/ip type localhost and use port 2222. This way you don't need to setup a static ip on the container, and if you're working alone it's the most convenient way to do it. You'll need to create a user for ssh login.

I made an alias of that one liner to start docker, so the development environment is up in the time it takes me to type a single word.






No comments:

Post a Comment