Create your own installer

In this article we are going to make an easy installer step-by-step. At the end you’ll have the knowledge you need to build these containers. So at least what a installer needs to work is a docker-compose.yml file.

First step: the minimum installer

GorillaJS uses all the Docker containers to make work the apps and Docker compose to build those containers. So at least what a installer needs to work is a docker-compose.yml file.

The heart

To start we create a docker-compose.yml file, we pasty the next code and we save it on an empty folder. This folder will be the place where we’ll put all the installer’s files.

version: '3'

services:
  web:
    image: gorillajs/php7
    container_name: myproject.local

networks:
  default:
    external:
      name: gorillajs

This code serves to create the Apache web server that will be the entry point of the app and it’s composed of three parts: version, servers, networks.

  • version: it refers to the Docker compose version we want to use. GorillaJS only works with the 3 version that is the last recommended.
  • services: is the system where we’ll put the containers we want for our app and its configuration.
    • web.image: is the image’s name that Docker will take as support to build the container. In this example we use an image that Apache and hp have got installed.
    • web.container_name: is the container’s name. GorillaJS needs that the main container name of the app will be the same than the domain project name. For example, if the main url of the project is myproject.local the name of this container must be myproject.local.
  • networks: are the nets in which we want to be the containers. In order that GorillaJS could communicate to the app containers it needs that all the projects are in the same net: gorillajs.

Building for the first time

After configuring the installer, we can start the new project to test it. To do that we use the Gorilla’s build command. Before using the build command, we create a new folder (different from the installer) and we access from the terminal.

$ mkdir myproject

$ cd myproject

$ gorilla build

We choose “Local folder” in the installer selector and we insert the (absolute) path to the installer folder (it’s important not to make a mistake with the folder in which we are now, it’s the project folder).

Finally, we have to put the command from which we want to access to our project. Remember that it has to be the same than we have put in the container_name of  docker-compose.yml. file. In a few seconds the browser will be open with the project and the message: It works!, the Apache welcome message.

The project folder

According to the project folder, you’ll notice that .gorila. hidden directory has been created. This directory is used by GorillaJS to save the own configuration of the project and the “complicated” installer files (I’ll tell you later).

GorillaJS proxy

You might see that before loading the Apache welcome message, have appeared a wait page. All the calls to the projects created with GorillaJS get through a local proxy. If you open a new terminal window and you use the docker ps command, you will see a container called gorillajs. This proxy allows GorillaJS to do common tasks between projects that would be very difficult to manage in an individual way.

If you don’t want your project to use the proxy you’ll have to add a port to the main container. In that case, a web one. The docker-compose.yml file would be like that if you want to access from 3001 port:

version: '3'

services:
  web:
    image: gorillajs/php7
    container_name: myproject.local
    ports: 
       - "3001:80"
networks:
  default:
    external:
      name: gorillajs

If you build again your project you’ll be able to access thorugh http://myproject.local:3001 url.

Keep in your mind that the GorillaJS port will try to open through the proxy because it’s its performance.

At this moment, we have an easy funcional installer. We are going to complicate it just a little.

Second step: the app

You have noticed that before the starting of the application, the message “It works” is showed on the screen. That text is an html in the published folder of Apache server we have created with Docker container. We need to access to that published folder in order to use our own files. We do it with the Docker’s volumes. A volume is a shared folder between the container and our computer (hosts) in order to have the relevant data. We will turn the volume into an Apache published folder to add files of our app.

The app of the example uses php so we will create an index.php with this test:

 

We create a folder called “application” and we save the file on it. The project folder would be like this:

Now we come back to the installer folder and we modify docker-compose.yml file to add the volume.

version: '3'

services:
  web:
    image: gorillajs/php7
    container_name: myproject.local
    volumes:
      - ../../application:/var/www/localhost/htdocs
networks:
  default:
    external:
      name: gorillajs

We only have done one thing: adding the volume field in the web container. You’ll notice that the path to the app folder is relative. It’s because GorillaJS carries the docker-compose.yml file on, which is on your project folder.  The files which are in the installer are used as a template and before processing them, it saves a copy on .gorila folder of our project.

If you build again your project you’ll notice that is shown the text of your index.php file instead of showing the Apache welcome web.

$ gorilla build

Third step: the database

To complete the installer we have to add a second Docker container with a database. We will use the MariaDB official image: we open the docker-compose.yml file and we add the code:

version: '3'

services:
  web:
    image: gorillajs/php7
    container_name: myproject.local
    volumes:
      - ../../application:/var/www/localhost/htdocs
  mysql:
    image: mariadb
    environment:
      - "MYSQL_DATABASE=mydatabase"
      - "MYSQL_USER=myuser"
      - "MYSQL_PASSWORD=mypassword"
      - "MYSQL_ROOT_PASSWORD=myrootpassword"
networks:
  default:
    external:
      name: gorillajs

If we build the image again you’ll have a new container with the database. We can try the connection from the index.php file of the project with this code:

 

Fourth step: creating dynamic installers

GorillaJS allows you to use a labeling pseudo language with which we can complete values of our installer in a dynamic way. For example, if we don’t want that the connection data of our database are the same in all the projects, we could do something like this in the docker-compose.yml file:

version: '3'

services:
  web:
    image: gorillajs/php7
    container_name: myproject.local
    volumes:
      - ../../application:/var/www/localhost/htdocs
  mysql:
    image: mariadb
    environment:
      - "MYSQL_DATABASE={{database.name}}"
      - "MYSQL_USER={{database.user}}"
      - "MYSQL_PASSWORD={{database.password}}"
      - "MYSQL_ROOT_PASSWORD={{database.root_password}}"
networks:
  default:
    external:
      name: gorillajs

If you build your project again you’ll notice the appearance of new questions: one for each test we have enclosed between square brackets {{ }}. The name of this text is object. Objects must have valor1.valor2 format and GorillaJS saves them on the gorilla/gorillafile of our project.

Besides the objects you can create, there’re also some of them which are created automatically when we build a new project. You can look it up the same .gorilla/gorillafile file in which are saved the others. These objects are very useful. For example, if we use {{project.domain}} in the field container_name of the docker-compose.yml file we will stop worrying about changing this value when we create the project.

version: '3'

services:
  web:
    image: gorillajs/php7
    container_name: {{project.domain}}
    volumes:
      - ../../application:/var/www/localhost/htdocs
  mysql:
    image: mariadb
    environment:
      - "MYSQL_DATABASE={{database.name}}"
      - "MYSQL_USER={{database.user}}"
      - "MYSQL_PASSWORD={{database.password}}"
      - "MYSQL_ROOT_PASSWORD={{database.root_password}}"
networks:
  default:
    external:
      name: gorillajs

Is that dynamism what makes a installer powerful.

Fifth step: make to measure questions

To end the project we can make GorillaJS to show a personalized text whenever it finds an object rather than being the automatically one. According to the database example, we will create a config.json file in in the installer folder and we add this code:

{
  "messages": {
    "questions": {
      "database": {
        "name": "The database name.",
        "user": "The database user name",
        "password": "The database password.",
        "root_password": "The password for the root user"
      }
    }
  }
}

When the objects are in messages.questions GorillaJS will search for them in this file every time that it has to show a question on the scream.

Sixth step: next steps

  • Share the database container’s volume so that it could be persistent.
  • Create your own Dockerfile and install some framework like Laravel, Symfony, or similar.
  • Create a entrypoint so that the container would be different in every projects. It serves to configure the framework databases access we had installed from the Dockerfile.
  • You can make what you want and what you really like.

The NodeJS installer

The NodeJS template that comes as a default in GorillaJS uses the Express.js framework to start a server and return a response. This is optional, and once the application is running we can eliminate this server and use the application in any other way.

FAQ’s

Why do you need to free the port 80? Can you make your own installers? Can you use GorillaJS in a remote server?