Orie Chinedu Emmanuel

Orie Chinedu EmmanuelBlockedUnblockFollowFollowingJan 5Laravel Dockerization out of the box!IntroDocker is an open source platform that makes it possible for software developers to Build, Ship, and Run any App anywhere.

In a high-level explanation, a dockerized App can be likened to a flower growing in a bucket whereby the bucket contains all that the flower needs to survive notwithstanding the environment.

It basically places (containerizes) the App in a container that houses all that the App needs to run.

Docker container analogy, SourceThis article assumes that the reader already understands how to use Laravel, our business is how to dockerize it.

RequirementsTo get a Laravel app running on Docker, the following must be put in place:Laravel installed on your local machine.

See the official documentation for steps to installing Laravel.

Docker installed on your local machine.

Check here to install the appropriate distribution for your operating systemDocker Compose installed on your local machine.

Knowledge of the command line, if you’re a Linux user, you should check out this Linux tutorial to understand basic Linux commandsIf you’ve met the requirements above, then you’re ready and good to go.

Let’s dive deep into the main business.

The sample project used in this article can be cloned hereGetting StartedBefore we proceed, it’s important we explain some basic concepts that we’ll be using in this tutorials.

Most prominent among them are images and containers.

Images: The filesystem and metadata needed to run containers.

They can be thought of as an application packaging format that includes all of the dependencies to run the application, and default settings to execute that application.

The metadata includes defaults for the command to run, environment variables, labels, and healthcheck command.

Containers: An instance of an isolated application.

A container needs the image to define its initial state and uses the read-only filesystem from the image along with a container specific read-write filesystem.

A running container is a wrapper around a running process, giving that process namespaces for things like filesystem, network, and PIDs.

-Stackoverflow.

comAnother analogy that explains images and containers is class and objects in object-oriented programming.

An image is a class while a container is an object of the class.

Explaining further; in order to run a Laravel app on our local machine, we install LEMP, WAMP, XAMP, or MAMP as the case may be as well as phpmyadmin if it’s needed.

These pieces of software are bundled as images in docker environment.

This means, to dockerize a Laravel app, we need to create an image that contains each of those packages, or we create different images for each of them and internetwork them inside the docker container.

In this article, we’ll not be creating images from scratch, we are going to pull already built images from docker hub.

Below are the images we are going to pull from docker hub to enable us dockerize our app:creativitykills/nginx-php-server -php and nginx maintained by Neo Ighodaromysql:5.

7phpmyadmin/phpmyadminClick on each of the images to read details about them.

Having established the necessary theories about what we are going to do, let us begin the main processes of dockerizing the app following the steps below.

Step 1— Create the Laravel Project to be dockerizedCreate the Laravel project that you want to dockerized.

Open your terminal and cd into the root of the project.

ned@Ned-PC:/var/www/html/laravel-dockerization$Step 2— Create the docker-compose.

yaml file$ touch docker-compose.

yamlNext up, edit the docker-compose.

yaml file as follows:We’ve finished what we need to do, but before we summarize, let me explain what is happening in the yaml file above.

In line 1, we specified the version of docker-compose to be used.

In line 2, we declared the beginning of the definition of our services, services here literally means images.

In line 3, we declared the first service name core_services; note that the name is up to you to decide.

This service would be an image comprising the Nginx and PHP interpreter and other dependencies such as composer.

In line 4, we specified the name of the image to be pulled from the docker hub.

In line 5, we specified the container name, that is the name for any instance of the core_services image.

Note that the name is up to you to decide.

In line 6–7, we specified the port through which we can access the app on the browser.

It means we can access the app by visiting 0.

0.

0.

44678 on the browser.

In line 8–9, we mapped the directory of our project to /var/www inside the docker container.

What it means is, copy the entire project located in /var/www/html/laravel-dockerization to /var/www inside the container.

With this, any change we make externally in the project directory will be reflected inside the docker container and vice versa.

In line 10–11, we specified the network to be used by the container so that any other container connected to the same network and communicate with each other.

If you do not specify a network, docker creates one an attach the container to it.

It’s advisable you specify the network to be used so that you can have full control of its behavior.

From line 13–23, we created the mysql service.

We specified the version to be used as 5.

7, this can change depending on your choice.

We specified the MYSQL_ROOT_PASSWORD.

This ${DB_PASSWORD} means replace this with the value of DB_PASSWORD in the .

env file.

So, ensure that the value of DB_PASSOWRD in the .

env is the password you want to use for mysql.

In line 20–21, we mapped the volume from the external directory to the mysql container volume.

This volume ensures that data stored in the database is persisted in the local machine even if the container is deleted.

You can name the directory what you choose to but ensure you create it before the mapping.

In line 22, we mapped the external directory .

/db/mysql/initdb.

d to docker-entrypoint-initdb.

d.

This volume is used importing data into the database.

For instance, if you want to import an SQL dump into the database, you need to copy the sql dump file into the db/mysql/initdb.

d and then access the file from docker-entrypoint-initdb.

d inside the db_mysql container.

We’ll demonstrate how to do this later in this article.

You can read more about docker-entrypoint here.

From line 24–34, we created the phpmyadmin service with the container name pma.

We exposed port 44679 which means we can visit 0.

0.

0.

44679 to access the phpmyadmin.

In line 30, you’ll notice we specified the PMA_HOST as the MySQL container db_mysql, this is very important.

If the PMA_HOST is not in sync with the mysql container, you’ll not be able to access your phpmyadmin as the PMA_HOST will default to localhost.

We also specified the MYSQL_ROOT_PASSWORD and MYSQL_DATABASE to use the DB_PASSWORD and DB_DATABASE in the .

env respectively.

Note that MYSQL_DATABASE is optional.

From line 36–38, we defined the backend network that we connected the containers in the previous lines.

When docker encounters the backend network, it will look for where it’s defined and create it first before continuing.

Next up, we confirm that the correct values are passed to the docker-compose from the .

env by running the command below:$ docker-compose configdocker-compose config outpoutBelow is the look of my .

env file:.

DB_CONNECTION=mysqlDB_HOST=db_mysqlDB_PORT=3306DB_DATABASE=laravel_dockerizationDB_USERNAME=rootDB_PASSWORD=secret.

Notice that the DB_HOST is set to the name of the mysql container defined in the docker-compose.

yaml.

Step 3 — Building the containersNow that we have set up the docker-compose.

yaml, we are set to build the containers.

Run the command below at the root of your project:$ docker-compose up -dThe command above will build the containers and start them up.

The flag -d starts the containers in interactive mode, the processes run in the background and you’ll be able to run other commands subsequently, but if you’d want to monitor the build processes, then just run:$ docker-compose upNext up, we need to give full permission to the storage directory so that docker would be able to read and write to it, run the command below:$ sudo chmod 777 -R .

/storageNow, if the docker-compose command above build successfully, we confirm that the containers are running as supposed by running the command below:$ docker psThe output of running docker ps commandThe screenshot above shows that all the containers are running as expected.

If all of the three containers are not running, it means something has gone wrong.

You need to drop the containers and rebuild again removing the -d flag.

This will make the build process verbose and you can easily point what went wrong and get it fixed.

To do this run the command below:$ docker-compose down && docker-compose upNow head to the browser and visit http://0.

0.

0.

0:44678 and voila!.our app is up!http://0.

0.

0.

0:44678phpmyadmin: http://0.

0.

0.

0:44679Note: if the database is not created automatically, you can manually create it using the PMA GUI or via MySQL command line.

Step 4 — Interacting with the containersNow that the app is up and running, you might want to interact with the container to performs actions like running migration or importing SQL dump into the database.

To enter the container, run the command below:$ docker exec -it core bashRemember that core is the name of the php and nginx container.

You can now run migration inside the container.

$ php artisan migrateTo enter the mysql container, run:$ docker exec -it db_mysql bashHow to import SQL dump file into the databaseThere are situations that you might want to import large SQL dump file into your database, in this case, you might want to do it via the terminal.

Follow the steps below to achieve that:First copy the SQL dump into db/mysql/initdb.

dEnter the mysql container using docker exec -it db_mysql bashrun cd docker-entrypoint-initdb.

d$ ls // to be sure the sql file is there$ mysql -h localhost -u root -p <database_name> < sql_dump_file.

sqlYou’ll be prompted to enter your mysql passwordNote that, in copying the SQL dump file to the initdb.

d, if you don’t have root access on your machine, you might want to use sudo command to copy the file via the terminal as shown below:$ sudo cp /path/to/sql_dump_file.

sql db/mysql/initdb.

dSteps to importing SQL dump file into the MySQL containerThe screenshot above shows the steps above being applied.

ConclusionIn this article, we have demonstrated how to dockerize a Laravel application using already built docker images from the docker hub.

We tried to explain what goes on in the docker-compose.

yaml and also showed how we can interact with the containers and as well as showed how we can import SQL dump into the MySQL container.

In the next tutorial, I will show how we can create our own docker image from scratch.

When that is done I’ll write on how to dockerize a microservice based Laravel application.

Stay tuned!Should you have any contribution or question for me, reach out here or via twitter.

And if you benefited from the article, a few taps on the ????button would be appreciated ????.. More details

Leave a Reply