Posted by on Jul 25, 2014 in Blog, Server, Tech | 8 comments

KuDr42X_ITXghJhSInDZekNEF0jLt3NeVxtRye3tqcoSo I have a lot of websites on my server and when the last time my server crashed, I lost everything. Even though I had a git repo but the latest mysql updates etc. were all lost. And on top of all I had to go through so much trouble to bring all my sites back up as I had to reinstall everything on a new server.

So I switched to Docker.  I created a Docker repo (first one is for free) and installed docker on my Macbook.

Click here to install: https://docs.docker.com/

Create a repo on docker after you sign up there. Just select create repo and enter a name for your repo. I used a digital ocean droplet, that costs me around $5 a month and they have native support for docker in their containers. You don’t have to install “Docker” if you use a “Docker image” on Digital Ocean.

Installation

On you PC or mac, enter the following commands:

  • docker login (enter the info asked)
  • docker pull ‘imshashank/mysql’  (This was the name of my docker repo ‘username/repo-name’)
  • docker run -t -i imshashank/shashank /bin/bash (To login to your docker via ssh)
  • Install all requirements all requirements in the Docker Container including apache2, mysql, etc.
  • Enter ‘exit’ to exit the shell.
  • Now enter ‘docker ps’ to see list of running images

You will see something like

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0942asd9701f7 e130cc2141d1 /bin/bash ….

Copy the “container ID”, we are now going to push our changes to server.

  • Enter a ‘docker commit docker commit 0942asd9701f7 imshashank/shashank’ (this will add all changes to a layer and save as a commit)
  • Enter ‘docker push imshashank/shashank’  (this will push all changes to the docker container)

Now go to the console on docker website and your repo should say “updated just now”. Now we know all the changes are being saved to our repo.

Running the Server

Now Testing that my docker container is working all fine.

  • So to see docker server on localhost, you need to explicitly declare ports on docker.
  • Enter “docker ps” and copy the docker container id
  • Enter “docker stop <container id>
  • We have stopped the running container, now we are going to run this on localhost
  • Enter “sudo docker run -i -t -p 8800:80 imshashank/shashank /bin/bash”
  • Start the ‘apache’ and ‘mysql’ server
  • Open localhost:8800 on your machine and you would be able to see the sites.

But as soon as you exit command line, the website will be offline. The trick is to exit the shell “Press Ctrl+P+Q” to exit the ‘bash’, and even if you close terminal your site will not go down.

Putting it all together on the server

Now login to your server, install docker and pull the docker container. Make sure you have not installed anything on your server except docker, not even apache or mysql as they all are already installed inside your docker container.

Now run the docker image by

  • sudo docker run -i -t -p 80:80 imshashank/shashank /bin/bash
  • Start the apache,mysql etc. services and exit the shell (Use Ctrl + P +Q)
  • Just visit your IP or domain, you don’t have to declare the port (like we did before i.e. 8800)

The site should be up and running. You now have a working docker container working and serving your sites.

Cron the commits

So I wanted to make sure that all changes to mysql were pushed to my docker container every hour, so I developed a small trick.

  • On your docker machine enter “docker ps”
  • Copy the container ID.
  • Enter “crontab -e” and select “vim” or other editor you prefer
  • Enter ‘0 * * * * docker commit 094abc19701f7 imshashank/shashank && docker push imshashank/shashank’
  • This will commit changes every hour and push to my docker repo.

Remove garbage (untagged images)

Now after using docker for a long time, I realized I was having a lot of untagged images being saved with every commit. So to clear all previous commits from local machine, I used a new command. This runs every 24 hours and frees extra space on my machine which is being used up by old docker images.

  • Enter crontab -e
  • Enter “30 2 * * * docker rmi $(docker images | grep “^<none>” | awk “{print $3}”)”

This will remove all old images at 2:30 am.

This is all we need to do . I hope the tutorial was useful, any feedback is really appreciated.