Docker and how to make your life easier

docker moby

Docker is a new way to use virtual machines (someone expert that read this, not hates me, it’s a way to show what it is). Docker represents an easy step to install services on our machine without installing it. This means, is like a virtual machine using your own hardware instead of virtualizing it and where you can install things. It’s complex to explain because Docker is a half-path between your own computer and Virtual Machine.

The best way to understand what is and why use it is creates a basic example. We are web developers, the best is to create an Apache Image with PHP and execute some code.

Tabla de contenidos

Install Docker

Let’s try it. First, install Docker, this is important. If you don’t install the examples in this article couldn’t work!

You can download a copy of Docker here: Docker Engine installation

Install and …. continue.

Now it’s time to create containers. Docker has containers, a container is a service installed and you can pile containers to add more services. Let’s go to work and stop rare words!!

Create Image

This tutorial talks about creating an image that you can use and reuse. Time to create. Move to a new folder and create a file called Dockerfile and add the next lines:

It’s a two-line file. Simple instructions:

  • FROM chialab/php:5.6-apache: tell what docker image you want to use. I’m choosing chialab images because have a lot of libraries installed. You can choose another one if you want. Feel free, I always use it because have all I need for development. Search images here: Hub docker
  • ENV APACHE_LOG_DIR /var/www/html : Tell image internal environment variable.

Time to build. We have a simple Dockerfile and now need to build our image, open the terminal window and move to the Dockerfile folder and execute the command:

We built an apache image, execute the next command to see your images:

Use Image

Now it’s time to use it. Then, create an index.php page with phpinfo() instructions.

Only one more step and we are done. Time to execute the magic command:

What’s the meaning of commands?

  • docker run: tells to execute image
  • -v: is for mount volumes. I’m telling that folder $PWD (it’s system variable for actual folder) maps to /var/www/html inside image
  • -p: map port 80 on our computer to port 80 in an image.
  • apache: Docker image name

If you aren’t using Mac Os X or any Linux based OS you can change $PWD for another folder:

And finally, open your browser and write https://localhost You will see a phpinfo() information:

docker-phpinfo

You are running a web server now with apache. Thanks to docker it’s easy to create environments without installing any software on our computers.