pedroreinarojas.com
flag-english-language
flag-spanish-language

Docker and docker compose

cms-image-cover

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.

And what is Docker?

Docker is a software virtualizer.
To get it quickly.
It's like installing a virtualbox, but without installing the operating system, only the software in question. And it doesn't matter if you use Ubuntu or Windows, they will all work. exactly the same, isolated from your environment.
Docker installs images and containers, always using your operating system.
In Docker an image is the software, mysql, wordpress, drupal.
In Docker a container is the instance of an image.
I'll explain this last point better.
You can install 3 different wordpresses, each of these wordpresses will be the site or domain that you will upload to your hosting, and that you will probably prefer to create and test locally first.

Docker Images and Containers

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.

More about Docker

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.

And what is docker compose?

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.

Useful links for docker and docker compose

By the way, there is the official Docker site and its documentation
https://docs.docker.com/

If you want to install it, you can find on Google how to do it from your operating system.
For example, to install it from Ubuntu 18
https://www.digitalocean.com/community/tutorials/how-to-install-docker-compose-on-ubuntu-18-04-en

Advantages of dockerizing an environment.

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.

Document created by Pedro Reina Rojas
17 de April de 2020