How to Create Docker Image with MySQL Database

How to Create Docker Image with MySQL Database

In this tutorial, you will learn how to create Docker images and using bind mounts to customize MySQL database

In this tutorial, you will learn how to create Docker images and using bind mounts to customize MySQL database

Many **developers **use **Docker **to spin up a local database. This makes it easy to test their code and write data without installing and configuring a lot of tools. In this piece, I’ll explain how you can **customize **your Docker database. While I’ll be explaining the steps for MySQL, it’s the same for PostgreSQL!

Follow along and let’s create a custom MySQL which contains your desired tables and data.

Creating SQL scripts

We will create **SQL **scripts which contain the **SQL **statements we want to execute on our database. Create a directory in which we will work. Also, create a subdirectory where we will store our .sql scripts.

$ mkdir -p ~/my-mysql/sql-scripts
$ cd ~/my-mysql/sql-scripts

I want to customize my **database **with a table called employees. The table needs to contain one row with an employee (first name, last name, department, and email).

Write a CreateTable.sql. This file contains the SQL statement to create a table called employees. We will add four columns to our table

CREATE TABLE employees (
first_name varchar(25),
last_name  varchar(25),
department varchar(15),
email  varchar(50)
);

Write a InsertData.sql. This file contains our statement to insert data in the table ‘employees’.

INSERT INTO employees (first_name, last_name, department, email) 
VALUES ('Lorenz', 'Vanthillo', 'IT', '[email protected]');

Execute the tree command to verify that your two scripts exist and are saved in the right directory.

$ cd ~/my-mysql
$ tree
└── sql-scripts
    ├── CreateTable.sql
    └── InsertData.sql

Creating a Docker Image for Your Customized MySQL Database

Now that the scripts are ready, we can write a Dockerfile to create our own Docker image (based on the official image of MySQL).

$ cd ~/my-mysql/
$ vi Dockerfile

Content of Dockerfile:

# Derived from official mysql image (our base image)
FROM mysql
# Add a database
ENV MYSQL_DATABASE company
# Add the content of the sql-scripts/ directory to your image
# All scripts in docker-entrypoint-initdb.d/ are automatically
# executed during container startup
COPY ./sql-scripts/ /docker-entrypoint-initdb.d/

Create your Docker image:

$ cd ~/my-mysql/
$ docker build -t my-mysql .
Sending build context to Docker daemon  4.608kB
Step 1/2 : FROM mysql
latest: Pulling from library/mysql
Digest: sha256:691c55aabb3c4e3b89b953dd2f022f7ea845e5443954767d321d5f5fa394e28c
Status: Downloaded newer image for mysql:latest
 ---> 5195076672a7
Step 2/2 : ADD sql-scripts/ /docker-entrypoint-initdb.d/
 ---> 25065c3d93c0
Successfully built 25065c3d93c0
Successfully tagged my-mysql:latest

And start your **MySQL **container from the image:

$ docker run -d -p 3306:3306 --name my-mysql \
-e MYSQL_ROOT_PASSWORD=supersecret my-mysql

Now we can verify. We will exec inside the container:

$ docker exec -it my-mysql bash
[email protected]:/# mysql -uroot -p
Enter password: (supersecret)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| company            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> use company;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| employees         |
+-------------------+
1 row in set (0.00 sec)
mysql> show columns from employees;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| first_name | varchar(25) | YES  |     | NULL    |       |
| last_name  | varchar(25) | YES  |     | NULL    |       |
| department | varchar(15) | YES  |     | NULL    |       |
| email      | varchar(50) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from employees;
+------------+-----------+------------+-------------------+
| first_name | last_name | department | email             |
+------------+-----------+------------+-------------------+
| Lorenz     | Vanthillo | IT         | [email protected] |
+------------+-----------+------------+-------------------+
1 row in set (0.01 sec)

It works! We have our customized MySQL database Docker image! This is a great solution for local development between multiple developers. By sharing the Docker image, every developer can use the **database **by just starting a container from the image.

It’s important to note, however, that this is not always the best solution:

  • If you insert a lot of data your image size will grow significantly
  • Need to build a new image when you want to update the data

That’s why there is another way to customize your Docker MySQL.

Use Bind Mounts to Customize Your MySQL Database in Docker

In this last section, we will simply mount the scripts inside the official MySQL Docker container.

$ docker run -d -p 3306:3306 --name my-mysql \
-v ~/my-mysql/sql-scripts:/docker-entrypoint-initdb.d/ \
-e MYSQL_ROOT_PASSWORD=supersecret \
-e MYSQL_DATABASE=company \
mysql

And we can verify again! Use the same steps as we did before: exec inside the container and check if the table and data exist!

This method is more flexible but it will be a little bit harder to distribute among the developers. They all need to store the scripts in a certain directory on their local machine and they need to point to that directory when they execute the docker run command.

Conclusion

In this piece, I’ve described how you can create your custom MySQL database in Docker. We’ve explored the two methods:

  • Custom MySQL Docker image
  • Bind mounts to mount scripts inside a container

We also discussed some of the advantages and disadvantages of each method.

We hope you enjoyed it!

WordPress in Docker. Part 1: Dockerization

WordPress in Docker. Part 1: Dockerization

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

This entry-level guide will tell you why and how to Dockerize your WordPress projects.

Introduction Docker With Spring Boot and MySQL

Introduction Docker With Spring Boot and MySQL

Introduction Docker With Spring Boot and MySQL - This article explains the basic concepts of Docker and includes a tutorial that explains how to setup MySQL.

Originally published by Sanjoy Kumer Deb at dzone.com

Docker is a technology where developers or DevOps teams can build, deploy, and manage applications by using containers. Docker is an open source software so that everyone can run this on their own operating system, which should support virtualization and Docker for Mac/Windows/Linux.

Docker also ships the ready images from one computer to another. Docker containers are sets of processes that are isolated from the rest of the processes in the host OS. Docker shares the kernel of the host operating system. On the other hand, VM is a technology that depends on the guest OS, which is completely separated from the host OS. Virtual machines communicate with the host OS through hypervisor. VM requires many hardware resources and processes. It's not a container-based technology.

Docker moves up the abstractions of resources from hardware level to the operating system level. That's why application portability and infrastructure separation is easier.

Container-based technology takes lower resources of the host system. Most of the time, container-based technology uses the kernel of the host machine. The main goal of this article is to implement Docker with a Spring Boot application and MySQL.

You might also enjoy: Spring Boot: Run and Build in Docker

Basic Concepts

Docker Engine works as a client-server architecture. Docker daemon works as a server, which is the core part of Docker and is run on the host operating system. This service exposes some rest APIs that can be used by the client. A command line interface (CLI) client uses the services provided by Docker daemon with Docker command.

Important Terms

Image: Image is the executable application package file inside Docker. It contains everything needed to run an application, including libraries, config files, runtime, etc. It's a snapshot of a container.

Container: An instance of an image is called a container. When an image is executed and takes place in memory, then the instance of this image is called a container. It executes in a completely isolated environment.

In OOP terms, Image is a class, and a container is an instance of this class — a runtime object.

**Registry: **A registry is a storage and content delivery system that stores Docker images. Docker Hub is a popular Docker registry. We can store different versions of images with different tag numbers to Docker Hub.

Dockerfile: It's a text file that contains all the commands to assemble an image.

Dockerfile  > (Build) >  Image  > (Run) >  Container

Installation

In this article, we will use Docker Hub as a Docker registry. Then, we will follow the instructions to set up a desktop Docker application. I am using Mac OS as my host machine. After installation, we can see that the Docker app is running. Then, I have to sign in using my Docker Hub credentials.

After Docker Desktop Install and Login

Now, we can open the terminal to check some Docker commands.

docker --version command should return the Docker version. In my case, this command returns 

Docker version 19.03.2, build 6a30dfc

docker info command should return detailed information of the installed Docker machine of the host machine. Initially, you have zero images and containers.

docker pull hello-world This command should pull a hello-world image from the Docker registry. Now again, if we run the Docker info command, we can see that the image count is 1. But the container count is still zero.

docker run hello-world This command should create an instance of the hello-world image. It will return a long message: 

Hello from Docker!

This message shows that your installation appears to be working correctly.

If we run the Docker info command, we can see that the image count is 1 and the container count is also one. So the Docker installation is complete.

Setup MySQL

Now we will create and run an image of the MySQL database. From our terminal, we will run the below command. Here, -d in this command indicates that the Docker command will run in detached mode.

docker run -d -p 6033:3306 --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=book_manager" mysql

Hopefully, the MySQL image is pulled and running as a container. To check this, we can run

docker image ls and docker container ls commands. In my case, the responses of these commands are:

mysql latest b8fd9553f1f0 3 days ago 445MB 
hello-world latest fce289e99eb9 8 months ago 1.84kB
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
5db66654ba6a mysql "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 33060/tcp, 0.0.0.0:6033->3306/tcp docker-mysql

Now we can check by logging in to MySQL.

 docker exec -it docker-mysql bash;

It will take us inside the docker-mysql container. Then we will log in to MySQL using mysql -uroot -p  using password root. Then, we will run the show databases; command to see if the database setup is complete or not. 

In my case, it returns the below result:

+--------------------+ | Database | +--------------------+ | book_manager | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)

So we can say that the book_manager database was created inside the docker container. We can externally use this database from our host machine by using port 6033. Now we have to import the database script to the Docker MySQL database. The SQL script is available here. Run the following command to import this script to docker-mysql.

docker exec -i docker-mysql mysql -uroot -proot book_manager <book_manager.sql 

Hopefully, the book_manager script executed successfully. You can confirm by executing the following command.

$ docker exec -it docker-mysql bash; 
[email protected]:/# mysql -uroot -p 
Enter password: 
mysql> show databases; 
+--------------------+ | Database | +--------------------+ | book_manager | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use book_manager Database changed 
mysql> show tables; 
+------------------------+ | Tables_in_book_manager | +------------------------+ | author | | book | | book_author | | book_publisher | | book_tag | | bookshelf | | publisher | | tag | +------------------------+ 8 rows in set (0.01 sec) mysql> 
Application Clone and Build Project

I have already pushed my code to my GitHub repository. Anyone can clone the codebase from here. I think your host machine has Gradle setup. So now we run the gradle build commandto build the project. So the executable jar file is created at the build/jar directory of your cloned project.

Now open the Dockerfile. We can see that the file contains the following commands:

FROM java:8
VOLUME /tmp 
EXPOSE 10222 
ADD /build/libs/book-manager-1.0-SNAPSHOT.jar book-manager-1.0-SNAPSHOT.jar 
ENTRYPOINT ["java","-jar","book-manager-1.0-SNAPSHOT.jar"]

This file contains sequential commands to execute in docker. It will create an image of java 8. and also it will copy jar file from host machine to docker image and then run command which is given at entrypoint arguments. Now we will build a docker image by using this Dockerfile.

docker build -f Dockerfile -t book_manager_app .

This command will create a Docker image named book_manager_app to the Docker machine. Here, the -f  command indicates the Docker file name. Now we will run this image as a container.

docker run -t --link docker-mysql:mysql -p 10222:10222 book_manager_app 

The --link command will allow the book_manager_app container to use the port of MySQL container and -t  stands for--tty, which will allocate a pseudo-terminal.

After running this command, we will hit http://localhost:10222/book from our host machine browser, and it will return a list of books.

That's all for introductory knowledge of Docker with a simple Spring Boot implementation and MySQL database.

Happy coding!

Further Reading

How To Set Up Laravel, Nginx, and MySQL with Docker Compose

Docker Basics: Docker Compose

Tutorial Laravel 6 with Docker and Docker-Compose

How To Set Up Laravel App on Docker, With NGINX and MySQL

How To Set Up Laravel App on Docker, With NGINX and MySQL

Deploying Your Laravel App on Docker, With NGINX and MySQL ... The original post was also uploaded here: Laravel Application on Docker.

Now Shall We

Docker, the new sheriff in town whose new gold boots is getting everyone excited. Why? because Docker is development bliss since it ensures consistency on both your development and production cycles therefore standardizing your environment

I assume you're as excited about docker as the rest of us and hence explains why you have stumbled upon this post. I'll be honest, docker at first, without any prior knowledge is a nightmare, as any other new concept without prior knowledge, but once you do get the hang of it, it'll save you tonnes of time you'll have otherwise invested during deployment.

This post is intended mostly for beginners who probably have little or no knowledge about docker. The motivation behind this post lies on the fact that I wasn't able to find any comprehensive Laravel Docker tutorials online. I was only able in the end to accomplish this task through continuous reading of multiple blog posts and combining all this into a massive comprehensive series of steps that I'll attempt to document in this post.

Now Onto The Good Stuff..

Before I start, I assume that you already have your Laravel Application ready with you. If you don't, you can jump on to the Laravel Documentation Page and build yourself an app, then head back here and continue reading.

I also assume that you already have Docker installed on your machine. In case you don't, you have the following options:

  1. Windows 10 Pro Users: Docker Desktop
  2. Windows 10 Version that's below Windows 10 Pro Users: Docker Toolbox .This is because of Docker Desktop system requirements. Docker Toolbox leverages on the functionalities of VirtualBox.
  3. Linux Users: Docker CentOS . You can choose your Linux distribution (if its not CentOS) from the side menu on the screen that comes up and follow those distribution specific instructions.
First Step: Creating your 'docker-compose' file

What is a docker-compose file? This is a file that defines all your multiple docker containers and all these containers can be spawned up by running a relatively simple command like:

docker-compose -f docker-compose.prod.yml up --build

Here, we'll be setting up a development environment (Which can also be used in your production environment, with a few minor changes that I'll document in my next post :-))

Create a file in your root directory and name it: docker-compose.yml

You'll be defining the containers in the next steps in this file.

In our docker-compose file, we define three containers: mysql, nginx and our laravel app.

So, for starters, our laravel app container will be defined as follows:

version: '2'

services:

The Application

app:
container_name: laravel_app
build:
context: ./
dockerfile: development/app.dockerfile
volumes:
- ./storage:/var/www/storage
env_file: '.env.prod'
environment:
- "DB_HOST=database"
- "REDIS_HOST=cache"

Overview of the Above Code:

  1. version - Feel free to change this to your choosing
  2. container_name - You'll use this name to refer to your container. i.e. if you'd want to close your container, you'll use this name to refer to it specifically. Also feel free to change it to your choosing.
  3. build - Used to build an image from a Dockerfile. Has the following additional options:
  • Context - Docker uses this context (basically, where your laravel files reside) to reference any files within it. In this case, the ./ refers to the root laravel folder assuming that the docker-compose file is stored in your laravel root folder.
  • dockerfile: docker images are built from Dockerfiles, which often contain additional commands that should be run inside the container. In this case, the dockerfile we use to build our appcontainer. Also note that we have used development/app.dockerfile .This means that our docker file is located in a 'development' folder on the root of our laravel app.
  1. volumes - Volumes are used by docker containers to share files between the host machine and the docker container that is running. The left hand side of the full colon represents our host machine and the right hand side represents our docker container. In this case, we're sharing all data in the storage folder on our laravel app with the docker container mounted at /var/www/storage
  2. env_file - This defines our laravel's .env file, in our case env.prod that we'll use to input docker container specific environment variables as we'll see later on this post.
  3. environment - This defines the environment variables that will be set on our docker machine. In this case, if we can execute a bash command inside our linux container and reference the environment variables we define here, i.e. echo $DB_HOST will print out: database

Our NGINX Container will be defined as follows:

# The Web Server
web:
container_name: nginx_server
build:
context: ./
dockerfile: development/web.dockerfile
volumes:
- ./storage/logs/:/var/log/nginx
ports:
- 8990:80

Overview of the Above Code:

  1. container_name - Again, the name of your container, which you can choose to change.
  2. build - Definition same as above. Here you can see that we define this container's dockerfile as web.dockerfile.
  3. volumes - Definition same as above. Here we share our laravel's logs folder with nginx's logs folder.
  4. ports - Here, we define the port in the host machine that our docker container will be listening on and the port on the virtual network created by docker during container deployment. This can be easily visualised by understanding that the left side of the colon defines the host machines, therefore ports on the host machine and the right side of the colon the docker container, therefore the ports on the docker container.

Our MySQL Container will be defined as follows:

# The Database
database:
container_name: mysql_database
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=Baly"
- "MYSQL_USER=phpmyadmin"
- "MYSQL_PASSWORD=phpmyadmin"
- "MYSQL_ROOT_PASSWORD=finallyJDBC2017."
ports:
- 8991:3306

Overview of the Above Code:

  1. container_name - Refer to above.
  2. image - In this case, we haven't defined a dockerfile to base our container build on, but rather an image. Our docker container will therefore be built from the image we've defined in this case mysql5:7image. You can switch this mysql version with the version you're developing with. Remember that, with reference to your laravel application, the newest versions of mysql may not work with your laravel app. This is because the newest versions MySQL use a different technique of authentication that may not be supported by either mysql or pdo php extensions. Therefore, beware when invoking mysql:latest instead of mysql:5.7.
  3. volumes - Still the same concept, except that now we've defined dbdata from our host machine that will map to /var/lib/mysql on the docker container.
  4. environment - Same concept as defined above, except that in this case, our mysql database will be initialized with the variables we have set. Therefore, our container after build, will automatically have a database named database , a user named secret identified by the password secret and a root password of secret_root. You can feel free to change these as you please. We define these settings in our env.prodfile so as not to collide our current .env file settings with our container file settings.
  5. ports - same as above, except that our mysql container will be listening on port 8991 on the host machine and 3306 (mysql's default port) on the container's network.

Defining your named volumes

Copy paste the following into your docker-compose.yml file:

volumes:
dbdata:

Ensure that you need to preserve the indenting in your docker-compose.yml file to ensure that docker-compose reads it correctly. In the end, your docker-compose file should look as follows:

version: '2'

services:

The Application

app:
container_name: laravel_app
build:
context: ./
dockerfile: development/app.dockerfile
volumes:
- ./storage:/var/www/storage
env_file: '.env.prod'
environment:
- "DB_HOST=database"
- "REDIS_HOST=cache"

The Web Server

web:
container_name: nginx_server
build:
context: ./
dockerfile: development/web.dockerfile
volumes:
- ./storage/logs/:/var/log/nginx
ports:
- 8990:80

The Database

database:
container_name: mysql_database
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=Baly"
- "MYSQL_USER=phpmyadmin"
- "MYSQL_PASSWORD=phpmyadmin"
- "MYSQL_ROOT_PASSWORD=finallyJDBC2017."
ports:
- 8991:3306

# redis

cache:
image: redis:3.0-alpine

volumes:
dbdata:

Second Step: Defining our Dockerfiles.

In this step, we define the dockerfiles for the containers we just defined in our docker-compose file. These dockerfiles will represent a series of commands that we'll want to run inside our docker containers.

Defining our 'app' dockerfile (laravel_app)

Create a folder in your laravel app's root directory and name it development. Inside the folder you just created, create a file and name it app.dockerfile(yes, without any extensions). Open this file and copy paste the following code into it:

FROM php:7.2-fpm

COPY composer.lock composer.json /var/www/

COPY database /var/www/database

WORKDIR /var/www

RUN apt-get update && apt-get -y install git && apt-get -y install zip

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
&& php composer-setup.php
&& php -r "unlink('composer-setup.php');"
&& php composer.phar install --no-dev --no-scripts
&& rm composer.phar

COPY . /var/www

RUN chown -R www-data:www-data
/var/www/storage
/var/www/bootstrap/cache

RUN php artisan cache:clear

RUN php artisan optimize

RUN apt-get install -y libmcrypt-dev
libmagickwand-dev --no-install-recommends
&& pecl install mcrypt-1.0.2
&& docker-php-ext-install pdo_mysql
&& docker-php-ext-enable mcrypt

RUN mv .env.prod .env

RUN php artisan optimize

Overview of the Above Code

  1. From php:7.2-fpm - This means will be building our container from an image, php:7.2-fpm. Also, you can change this version to meet your development environment needs.
  2. COPY - In the first copy command, we copy our composer.lock and composer.json from our root folder (in our host machine) to /var/www/ in the docker container. In the second copy command, we copy our database folder in the host machine to /var/www/databasefolder in the docker container. This is because, one, we'll want to make sure that the dependencies we use in our development environment (in composer.json) will be reflected inside the container when we download dependencies and two, that we can access our migrate files inside the docker container in cases we may need to run migrate command.
  3. WORKDIR - We set the working directory to /var/www which means we don't have to cd to this folder (move to this folder) in cases we'll need to run bash commands.
  4. RUN - Here, we install all the dependencies that will be needed by laravel, including composer and the dependencies needed by composer. Please note the if(hash_file('SHA384'... line. The hash value defined there will change with every update, and therefore if your installer fails with the message: installer corrupt, consider getting the correct hash value from: Get Hash Value.
  5. COPY . /var/www - At this point we copy all our folder contents into /var/www folder in the docker container.
  6. RUN - In the final run commands, we clear our application cache and other cache and install the mysql driver that laravel uses to make connections to the database. Afterwards, we rename our .env.prodfile to .env since this file will contain the correct environment variables specific to the docker container environment and therefore should be used by laravel. We run php artisan optimize to remove the cached version of the .env file.

Please note that it is unnecessary to copy everything from our root folder (like vendor folder) and docker provides a .dockerignore file which works pretty much like a .gitignore file. Our dockeringore file will look as follows:

.git
.idea
.env
node_modules
vendor
storage/framework/cache/**
storage/framework/sessions/**
storage/framework/views/**
development

Save this file in the same folder as your app.dockerfile (development folder).

For your .env.prod file, copy paste your .env file and rename it to .env.prod. In the database settings, change the DB_HOST to match the name of your mysql container, and the password to match what you defined in your docker-compose.yml file. If you followed all my steps without changing a thing, then your .env.prod file should resemble the following:

DB_CONNECTION=mysql
DB_HOST=mysql_database
DB_PORT=3306
DB_DATABASE=Baly
DB_USERNAME=phpmyadmin
DB_PASSWORD=phpmyadmin

Defining our 'web' dockerfile

In the same folder you just created (the development folder) create a web.dockerfile. Copy paste the following to the dockerfile:

FROM nginx:1.10-alpine

ADD development/vhost.conf /etc/nginx/conf.d/default.conf

COPY public /var/www/public

Overview of the Above Code

We build our dockerfile from the image: nginx:1.10-alpine. We then replace nginx's default.conf file with the vhost.conf we'll create in a sec.

We also copy our laravel app's public directory to the public directory of nginx, that will server all our public assets.

Create a vhost.conf file in this same directory (development) and copy paste this into it:

server {
listen 80;
index index.php index.html;
root /var/www/public;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
try_files $uri /index.php?$args;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass app:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

}

our php-fpm container will be listening on port 9000 and hence app:9000

Almost there...

So, to counter-check, you need to already have the following files:

  1. Root folder - docker-compose.yml and .env.prod
  2. development folder:
  • .dockerignore
  • app.dockerfile
  • web.dockerfile
  • vhost.conf

If so, then you're almost done, but first, some prerequisites:

If you are using Docker Toolbox on Windows and your laravel app folder is in a folder other than C:/users you will have trouble sharing volumes between your host machine and your docker containers. This is because any other folder that's not C:/users is not mounted by virtual box when your docker machine starts. Therefore, to fix this, first stop your running docker machine by running:

docker-machine stop

Then open virtualbox, right-click the machine named default and click on settings . Navigate to Shared Folders click on it, and add a new folder that defines the location of your laravel app folder. Remember to check against Auto mount. Afterwards, start your docker machine by running:

docker-machine start default
Drum Rolls...

Assuming you have done everything correctly, go ahead and run the following command:

docker-compose up --build

Make sure that you are running this command inside the root folder of your laravel app. This command builds your container images and finally starts them. If everything goes according to plan, you should be able to access your laravel app running inside your container at:

0.0.0.0:8990

Replace 8990 with the port you defined in your docker-compose.yml file if you used a different port.

Also, please note that for users using Docker Toolbox, docker creates a virtual network and assigns an IP address to it. You can find this IP address by searching for docker quickstart terminal and running it. The IP address assigned will be displayed in the terminal that pops up and you'll be able to access your laravel app by going to:

your-docker-machine-ip:8990

And there you have it folks! You have successfully deployed your laravel app on docker! Stay tuned for my next post where I'll be describing on how to deploy your Laravel app on a production environment.

Thanks for reading. If you liked this post, share it with all of your programming buddies!