Saturday, January 11, 2020

Docker and Docker-compose

What is Docker

What is Docker?
Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers.
-"Docker (software)" of Wikipedia
If we use "Docker", we can deploy an web application with its virtual environment. Each container is different virtual environment suited for the application inside, so we don't need to worry about the difference between the devlopment and the production. Just use Docker in both environments, then the application works as expected in both environments. It makes deployments/maintenance easier, so docker is loved by developers.

Install Docker

If you don't use Ubuntu/Linux
At first, you need to install Docker as follows:
If you use Ubuntu 18
If you don't use ubuntu18, you don't need to do this section. You must do this section to install Docker only if you are using Ubuntu.
At fist, maybe we will delete old dockers (if necessary).
$ sudo apt-get remove docker docker-engine docker.io
And install Docker from the offcial repository:
$ sudo apt-get update
# To enable https connection.
$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
# To download GPG key.
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Install the docker repository.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable" 
$ sudo apt-get update
# Install Docker.
$ sudo apt-get install docker-ce
Verify if Docker was successfully installed
Verify if Docker was successfully installed on your terminal (terminal: cmd aka command prompt, powershell, bash etc).
docker --version
#Docker version 19.03.5, build 633a0ea838
If the version is displayed, the installation is success.

Docker-compose

Docker-compose is installed along with the installation of Docker. Verify if Docker-compose is installed correctly.
docker-compose -v
#docker-compose version 1.24.1, build 4667896b
If the version is displayed, the installation is success.

Use Docker and Docker-compose

We will use Docker and Docker-compose to create Laravel environment. Do the following commands:
$ git clone https://github.com/lechatthecat/laravel-nginx-mysql-example-en.git
$ cd laravel-nginx-mysql-example
$ docker-compose up -d --build
$ docker-compose exec laravel ash
# In the laravel container:
$ sh -x ../laravel_build.sh
And voilĂ ! We can see the laravel's top page: http://localhost:10080/ 
It is a lot easier than Vagrant et Virtual Box.



You can see how it is created in the repository: https://github.com/lechatthecat/laravel-nginx-mysql-example-en/blob/master/docker-compose.yml
It is like this:
version: '3'
services:
    # mysql 8.0
    mydb:
        # image name
        image: mysql:8.0
        # Password and user name of mysql
        environment:
            MYSQL_ROOT_PASSWORD: 'root'
            MYSQL_USER: 'root'
            MYSQL_PASS: 'root'
        # Which port should be exposed
        ports:
            - 13306:3306
        container_name: mydb
        volumes:
            # Save the data in named "Volumes" of Docker
            - db-store:/var/lib/mysql
            # Or use the local file
            # - ./docker_db_data/mysql:/var/lib/mysql
            # Where to save the mysql's log
            - ./logs:/var/log/mysql:z
            # Where to load the my.cnf
            - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf:z
        # Which network this container belongs to.
        networks:
            - app_net
    # The container of Laravel
    laravel:
        build:
            # Wnere to find the "Dockerfile".
            context: .
            dockerfile: docker/laravel/Dockerfile
        working_dir: /laravel
        volumes:
            # Where the source code should be saved.
            - ./laravel:/laravel
            # Where the bash file is (which is executed for the build)
            - ./docker/laravel/laravel_build.sh:/laravel_build.sh:z
            # Where to save laravel's log files
            - ./logs:/var/log/php
            # Where to load php.ini.
            - ./docker/laravel/php.ini:/usr/local/etc/php/php.ini
        # Wait until mydb container is ready.
        depends_on:
            - mydb
        container_name: laravel
        # Which network this container belongs to.
        networks:
            - app_net
    # nginx 1.17
    nginx:
        # image name
        image: nginx:1.17-alpine
        # Wait until "laravel" container is ready
        depends_on:
        - laravel
        # Which port should be exposed
        ports:
        - 10080:80
        volumes:
        # Where to find the files to serve
        - ./laravel:/laravel
        # Where to save nginx logs.
        - ./logs:/var/log/nginx
        # Where to load default.conf.
        - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
        container_name: nginx
        # Which network this container belongs to.
        networks:
            - app_net
networks:
    # Containers in same network can access each other by using its container name as host name
    app_net:
        driver: "bridge"
volumes:
    db-store: