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
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!!
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:
ENV APACHE_LOG_DIR /var/www/html
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:
$ docker build -t apache .
Sending build context to Docker daemon 58.88 kB
Step 1/2 : FROM chialab/php:5.6-apache
Step 2/2 : ENV APACHE_LOG_DIR /var/www/html
---> Running in 287a83bfeb9f
Removing intermediate container 287a83bfeb9f
Successfully built e698e101ee27
We built an apache image, execute the next command to see your images:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache latest e698e101ee27 3 minutes ago 499 MB
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:
$ docker run -v $PWD:/var/www/html -p 80:80 apache
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:
> docker run -v /c/Projects/php:/var/www/html -p 80:80 apache
And finally, open your browser and write https://localhost You will see a phpinfo() information:
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.