How to run Laravel application and connect other services (MySql) using Docker.

How to run Laravel application and connect other services (MySql) using Docker.

Now Imagine your colleagues may want to run your Laravel app on their local machine, they will be required to install all PHP/Laravel dependencies and extensions in their local. Now, this is where Docker comes in, using Docker, it’ll be much easier for the applications to run on different devices, no matter what operating systems or environments we’re working on.

What does Docker do?

Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. No matter what environment/OS you’re working on right now, everybody can run your app anywhere.

In this article, I’ll show you the easiest way to run your Laravel application using Docker and connect our app to the database (MySql). We will be creating a Dockerfile and a docker-compose.yml.

The Dockerfile is used to build images while the docker-compose.yml file is used to run images.

Step 1: Create a Dockerfile

I assume you have a Laravel project, let’s add a docker file to the Laravel project. So, in the root of the project add a new file called Dockerfile. Then we write our instructions.

Note: Notice that the name of the file starts with capital D without any extensions.

# Set the base image for the application
FROM php:8-fpm-alpine

# Install system dependencies
RUN apk update && apk add --no-cache \
    build-base \
    mysql-client \
    git \
    zip \
    unzip \
    libpng-dev \
    libzip-dev

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql gd zip

# Set the working directory
WORKDIR /var/www/html

# Copy application code
COPY . /var/www/html

# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

# Install application dependencies
RUN composer install --prefer-dist --no-scripts --no-dev --no-autoloader && \
    composer dump-autoload --optimize

# Copy the PHP configuration file
COPY ./docker/php.ini /usr/local/etc/php/conf.d/custom.ini

# Copy the Laravel configuration file
COPY .env.docker .env

# Generate the application key
RUN php artisan key:generate

# Set file permissions
RUN chown -R www-data:www-data storage && \
    chown -R www-data:www-data bootstrap/cache
# Expose port 8000 for the PHP-FPM server
EXPOSE 8000

Extra Notes

  • Create a folder in your root directory with the name docker, and create a file inside the folder with the name php.ini. docker/php.ini. Then add the code below to the file.
memory_limit = 512M
upload_max_filesize = 100M
post_max_size = 100M
  • Create a file in the root folder with the name .env.docker and copy your environment variable to it

Overall, the Dockerfile above sets up the necessary dependencies for our Laravel application, installs Composer and the application dependencies, copies the application code and configuration files to the image, generates an application key, and sets file permissions.

Step 2: Create a Docker compose file

We will connect our database with docker-compose.yml. And if we need some additional commands to run other services, we can do that also inside the docker-compose.

Let’s create a new file named docker-compose.yml inside our root project. Open the docker-compose.yml file in a text editor and define the services (MySql) you want to run.

    version: '3'

    services:
    db:
        image: mysql:latest
        ports:
        - "3306:3306"
        environment:
        MYSQL_DATABASE: your_database_name
        MYSQL_USER: your_username
        MYSQL_PASSWORD: your_password
        MYSQL_ROOT_PASSWORD: your_password
        networks:
        - mynetwork

    web:
        build:
          context: .
          dockerfile: Dockerfile
        ports:
        - "8000:8000"
        depends_on:
        - db
        environment:
        DB_CONNECTION: mysql
        DB_HOST: db
        DB_PORT: 3306
        DB_DATABASE: your_database_name
        DB_USERNAME: your_username
        DB_PASSWORD: your_password
        networks:
        - mynetwork
    command:
        sh -c "php artisan migrate && php artisan serve --host=0.0.0.0"

    networks:
    mynetwork:
        driver: bridge

This docker-compose.yml file defines two services - db and web. The db service uses the MySql image and sets environment variables for the root password and database name. The web service builds a Docker image using the Dockerfile in the current directory and maps the container's 8000 port to the host's 8000 port using the ports configuration. The depends_on parameter specifies that the web service depends on the db service. The command section specifies the command to be executed when the container starts. The php artisan migrate command will run the database migrations, and the php artisan serve --host=0.0.0.0 command will start the web server and bind it to the container's IP address, allowing it to be accessed from outside the container.

Save and close the docker-compose.yml file.

Open a terminal or command prompt and navigate to the project directory. Run the docker-compose up command to start the containers defined in the Docker Compose file:

docker-compose up

This command will start both containers and connect them. The web service will be accessible on port 8000, and it will be able to connect to the db service using the hostname db.

After the image is successfully running, we can now access the server at http://localhost:8000

Common Docker command

  • docker build -t my_app . – Build our docker Image

  • docker image ls - List docker images

  • docker run <imageID> - Run docker image as a container

  • docker exec -it <container ID> sh – Access the docker container

Reference https://www.docker.com