LEMP Environment with Docker and Vagrant on Ubuntu
I’ve been using Vagrant with Virtual Machines for a while now. Today I’ve decided to build development environments using Docker as the provisioner. Below there is an step by step tutorial how I got it up and running.
I’m using Ubuntu 15.04 as my host machine. My understanding is that any of those commands below should work for previous versions of Ubuntu.
Installing Vagrant and Docker
Once there are dozens of tutorials out there with great explanations of how to install those tools in your Linux, I wrote one ‘how to install Docker on Ubuntu’. More how-to references for Vagrant and Docker.
First things first. We need to write down the scripts to build either Vagrant and Docker. Luckily, I’ve already done it for you. Just git clone or download the files from the repo.
$ git clone https://github.com/medinadato/docker-vagrant
Now you should have a folder called docker-vagrant with the following structure:
The Vagrantfile does little, once most of the configuration is held by Docker. Regardless, you can see that we define the synced folder and the port to be used to communicate with the guess’ webserver.
Vagrant.configure(2) do |config| config.vm.synced_folder "./www", "/var/www" # Sync'd folder config.vm.provider "docker" do |d| d.build_dir = "./Docker" # specifies the path to the Dockerfile d.ports = ['8080:80'] # Forwards port 8080 from the host to the Docker Container port 80 end end
Now, let’s take a look into the file Dockerfile into the Docker folder:
# Ubuntu FROM ubuntu:15.04 MAINTAINER Renato Medina, email@example.com # Download and install php, nginx, and supervisor, hey, just linux for a change! RUN apt-get update RUN apt-get install -y software-properties-common RUN add-apt-repository ppa:nginx/stable RUN apt-get update RUN apt-get -y dist-upgrade RUN apt-get install -y php5-fpm nginx supervisor # Setup config files RUN echo "daemon off;" >> /etc/nginx/nginx.conf #ADD ./nginx/default /etc/nginx/sites-enabled/default ADD ./supervisor/supervisord.conf /etc/supervisor/supervisord.conf ADD ./php-fpm/php-fpm.conf /etc/php5/fpm/php-fpm.conf # Shared volume RUN mkdir -p /var/www VOLUME ["/var/www"] # Default command for container, start supervisor CMD ["supervisord", "--nodaemon"] USER root # Expose port 80 of the container EXPOSE 80
Here we have a bit more code to analyse. At the top we define which Ubuntu version we want to use. In my case here Ubuntu 15.04. Then, we install PHP, Nginx, MySQL and Supervisor.
Afterwards, it’s more about a housekeeping. Some settings are imported into your new project in order to have the same configuration in all the machines we’ll run it on.
Building the Container
Now it’s time to get it up and running. Via command line browse to the folder vagrant-docker and type:
$ vagrant up --provider=docker
It should take a while once Docker will download the Ubuntu 15.04 image to create the container.
Afterwards, every time you run this code, it should get completed a way faster once Docker saves the previous used images.
To check if you Nginx and PHP are running properly just type on your brower http://localhost:8080/index.php. By now you should be able to see something like that:
As you might or might not know, Docker has no ssh access out-of-the-box. But, if you still want to log into the container all it’s needed is two commands:
$ docker ps
The command above will give you a list of running containers and their ids. Now access the machine by typing:
$ docker exec -i -t CONTAINER_ID bash
The CONTAINER_ID is the one related to your machine shown in the first command ‘ps’. Right now you should be logged in your container.
If you really want to use SSH, there is a way around by using a container from a company called Phusion.
Docker and Vagrant can do great working together. The sky is the limit when comes to what you can do with them.
So far, you built a full environment with just a few commands. I tried to keep the configuration files as simple as possible. So, now on is up to you implementing more functionalities.