How to Deploy Containerized Laravel Application from Repository to Ubuntu Server

In this blog, it will show us how to deploy containerized Laravel application in ubuntu by cloning it by its Github repository. Having a github repository of a containerized application is an advantage as we don’t need to make it from scratch.

In my case, I cloned a containerized application from a private repository. Here are the steps to take and some errors you may encounter in the process:

Step 1: Clone the Github repository by using git clone command

Go to directory /var/www/html then clone the repository:

$ git clone https://github.com/nucleiotechnologies/laravelapp.git

Step 2: Create .env file

Go to /var/www/html/laravelapp directory and copy .env.example file to the newly created .env file

$ cd laravelapp
$ sudo cp .env.example .env

Input the password and edit the .env file according to the docker-compose.yml file database. Change the DB_HOST into db and DB_DATABASE into laraapp_db

$ sudo nano .env

To close the file, hold CTRL + x then y and hit enter.

Step 3: Change permission of respective files:

Storage and bootstrap are needed to be under www-data instead of root.

$ sudo chown -R www-data:www-data bootstrap/
$ sudo chown -R www-data:www-data storage/ 

it should look like this inside the folder

Step 4: Access the Laravel application in browser:

Start docker containers by typing the following command:

$ docker-compose up -d

Then check if the docker containers are running by typing:

$ docker ps

Once it is running, try accessing the Laravel application in your browser using port 8080 by typing the following:

localhost:8080

If it shows the Laravel application, you deployed the containerized Laravel successfully in your ubuntu. But if it is not, proceed to the following steps

Step 5: Possible Errors

If the error shows like the image above, try changing the permission by typing the following:

 $   sudo chmod 666 /var/run/docker.sock 

Reload the page. If it is still not resolved, try the following command:

$   sudo chmod o+w ./storage/ -R

Reload the page again and if it shows the following image, you can generate your encryption key by using the following command inside the container

No Application Encryption Key Has Been Specified - Stack Overflow

To go inside the docker container, you need to know the container name of ID

$ docker ps

Once you can see the container name and ID, type the following command

$ docker exec -it <container_name_or_ID> bash

When you are inside the container, type this command to generate app key:

# php artisan key:generate

Now you can got to your browser and access Laravel application by typing:

localhost:8080

You have successfully deployed your containerized application once it loads like the image above.

Leave a Comment

Your email address will not be published. Required fields are marked *