docker moby

Docker and how to make your life easier

Docker is a new way to use virtual machines (someone expert that read this, not hate 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 half path between your own computer and Virtual Machine.

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

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: https://docs.docker.com/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 to create 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: https://hub.docker.com/
  • 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 terminal window and move to the Dockerfile folder and execute command:

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

Use Image

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

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.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *