When you hear people talking about Docker and/or Docker Compose, it sounds exciting but it sounds a bit like gibberish because everything is too technical.
The learning curve is not exactly smooth, although thanks to Youtubers like “Pelado Nerd” you can save yourself a lot of headaches.
I love Docker, and the more I know about this type of virtualization so close to the operating system (which has nothing to do with virtualizing machines with VirtualBox and similar), the more things I virtualize.
You can virtualize development environments, cms’s, online stores.
With Docker you can even virtualize applications (at least in Linux), you can find on the web wide world for example, how to virtualize the famous sublime text with docker compose.
When you do a docker build or docker up, the first thing Docker will do is build the images, and then the containers.
If your docker-compose file contains the ingredients for Apache, Nginx, WordPress, and Phpmyadmin, this will automatically build each of those images.
It will then create a container for each of those images.
The container is an instance of the image. As if the image were a template and the container an extension of it.
And if we clone this environment, or create another one that also uses Apache, it will use that same image.
That is, if we have 5 virtualized sites with Docker that use Apache, it will not install 5 Apaches, but it will install only 1 and it will be shared for all containers.
The magic of docker is such that you can run php 5 environments while your machine remains on version 7.2, or simply does not have php.
The way everything works in docker is by forming a network between containers, where they see and understand each other through ports.
For example, you install a mysql specifying port 8090.
You install 3 different cms’s, which are going to need mysql. So you tell them that their mysql is on port 8090. And that's it…
You install a software with a php 4, uuuuuuuuuuuuuuuuuu, for port 9091.
In the browser you will use localhost:9091 and it will look like the software is on your own machine.
You can also enter the container, and work inside and run things in it, which would be the analogue of being in the console of a virtualbox.
Well, if Docker is magic, docker compose is beyond words.
Docker compose is a little file, where you describe the list of images and containers you want to create, and how they interconnect with each other.
It's like a cooking recipe, where you declare what ingredients you use and how you mix them.
This is done with the docker-compose.yml command
And when you are in the folder of said file, you do «docker-compose up».
Then the thermo-mix acts 😀
Example of a wordpress docker-compose:
services:
php:
image: wordpress
restart: 'always'
ports:
- 0.0.0.0:80:80
depends_on:
- mysql
links:
- mysql
volumes:
- ./data/www:/var/www/html
mysql:
image: mysql
restart: 'always'
volumes:
- ./data/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: my_site
MYSQL_USER: root
MYSQL_PASSWORD: root
p>Where «volumes» These are volatile data.
Look at mysql, volumes. We are making the mysql data available. in a folder shared with the host. ('host' is our real machine, 'container' is the inside of docker)
So, when we zip the folder where the docker-compose.yml file is located, and take it somewhere else, the new container will 'hook' the mysql data again.
If you've never tried docker-compose, when you do it will be like eating sunflower seeds, you won't want to stop learning and using it more and more.