May 27, 2017
4 min read

Local Development Environment with Docker for PHP (Part 2)

Setting MySQL, Adminer and Redis

In our last post we installed docker in our machine and setup our environment with several containers. We added nginx container as a server, a php-fpm container and and installed a laravel application. Now we’ll install a mysql database to persist our application data.

Install MySQL

Before we create a mysql container we’ll create a volume to persist the database data and map it to a directory in the docker host. These volume will persist our data even if we remove the mysql container so we’ll not lose our data. Go to the volumes page in portainer dashboard and create a volume with any name you want

Create a volume to persist db dataCreate a volume to persist db data

Now let’s create a mysql container

Create a mysql containerCreate a mysql container

Give the container a name and let’s use the official mysql image. We don’t need to expose any ports publicly because our application will talk to the db and it can because it’ll be in the same network. Attach the newly created volume to /var/lib/mysql directory in docker host

Attach db-data volume to docker hostAttach db-data volume to docker host

Also select the php network in the network tab. In the Env tab add the below environment varibles

Add environment variable for our containerAdd environment variable for our container

MYSQL_ROOT_PASSWORD create a password for root mysql user, MYSQL_DATABASE creates database with the name and it’s optional so you can omit this if you want. MYSQL_USER and MYSQL_PASSWORD create a new mysql user and its password and these are optional too. Now create the container by hitting start container button.

Let’s check our application can connect to the database. First configure our database connection in env file of our project.

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=example
DB_USERNAME=hasnayeen
DB_PASSWORD=secret

Now connect to php container bash console from portainer dashboard and you’ll be in the /var/www directory. cd into the example directory and run migration command

Test our application can talk to db containerTest our application can talk to db container

Our application has successfully connected to the db container and migrated the default database tables.

Install Adminer

We can use the mysql cli in the dbcontainer to interact with database but we like to do it through a graphical interface. Many people use phpmyadmin as a database manager but I personally use adminer so I’m going to create a container using the official adminer image

Create a adminer containerCreate a adminer container

I named the container adminer and published all exposed port. Docker is going to assign a random port to 8080 of the container but if you want to assign a specific port on your host then you can map it through the Port mapping option. Let’s create the container

Our adminer container is running and mapped to a random portOur adminer container is running and mapped to a random port

You can see docker has assigned 32768 port to container 8080 port. Click the link and we see the login page of adminer

Login page of adminerLogin page of adminer

Fill all the field with proper value and hit login. Remember the server name is the name of the container of our mysql container which is in our case db.

Install Redis

Let’s now create a redis container to use as a cache memory.

Create a redis containerCreate a redis container

Attach the container to the php network and to persist our data we can use the db-data volume and mapped it to /var/lib/redis directory of docker host. Finally add a command in the command tab to run redis with appendonly mode and create the container

Add a command to run redis with appendonly modeAdd a command to run redis with appendonly mode

Now we need to configure our .env file in our application

CACHE_DRIVER=redis

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

Cache driver will be redis and redis host name is the name of our container which is redis. We’ll also need to predis/predis package to use redis in our application. So connect to the php container bash terminal and install the package

$ composer require predis/predis

After predis has been installed let’s check it’s working. We’ll add to route to our routes file, one for putting a value in the cache and one to get the value, so add the below code in the routes/web.php file

Route::get('get', function() {
    return \Illuminate\Support\Facades\Cache::get('hello');
});

Route::get('put', function() {
    return \Illuminate\Support\Facades\Cache::put('hello', 'world', 5);
});

Now first visit the put routes to put world as a value of hello key. It’ll return nothing but if we now visit the get route we’ll see it retrieve the value of hello key from the cache and return it to us.

Checking our redis container is workingChecking our redis container is working

Conclusion

So we have added few more containers and our machine is ready to build awesome applications with laravel.

In next post we’ll see how we can use different PHP versions and databases to test our application without messing with our host machine.

If you liked the content please consider to share the knowledge and don’t forget to subscribe for more.

Part-1

Part-3

Comments:
If you found any error or have any suggestion let me know in the comments.