DEV Community

Cover image for Use docker compose to create laravel/nginx/mysql environment
lechat
lechat

Posted on • Updated on

Use docker compose to create laravel/nginx/mysql environment

As you know it is pretty easy to create an environment if you use docker and docker compose. Of course this environment can be used for development, and more importantly, this environment can even be used in production environment! wow! It would simplify the deployment of web application! That's why people love docker.

(If your production environment isn't docker, maybe you can use vagrant for development.)

In this post, let's create laravel/nginx/mysql environment by docker-compose.

Install docker

At first, you need to install Docker as follows:

  1. Use Docker for Windows for Windows. Or Docker toolbox.
  2. Use Docker for Mac for Mac.
  3. If you use Ubuntu 18 If you are using Ubuntu, you can as follows 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And voilà! We can see the laravel's top page: http://localhost:10080/

Alt Text

What happened?

You cloned my git repository that has docoker-compose file like the following:

version: '3.5'

x-services-volume:
  &laravel-volume
  type: bind
  source: ./laravel
  target: /laravel

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-volume
            # 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-volume
        # 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:
Enter fullscreen mode Exit fullscreen mode

And all other things were automatically done by docker and and docker-compose to create a virtual environment following what this file says. This environment can be used in production also. For deployment of docker, maybe look at other posts.

It was originally written on my blog. Please see it too.

Conclusion

Docker is nice and easy. Creating an environment was used to be very time consuming, but docker and docker-compose can simplify it.

If you like this kind of technology, you should check kubernetes or Docker-swarm too!

Delete unnecessary Docker containers

If you want to delete unnecessary Docker images/volumes, please use this command.

$ docker-compose down -v --rmi all
Enter fullscreen mode Exit fullscreen mode

Top comments (0)