Tutorial

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

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

The author selected The FreeBSD Foundation to receive a donation as part of the Write for DOnations program.

Introduction

Over the past few years, Docker has become a frequently used solution for deploying applications thanks to how it simplifies running and deploying applications in ephemeral containers. When using a LEMP application stack, for example, with PHP, Nginx, MySQL and the Laravel framework, Docker can significantly streamline the setup process.

Docker Compose has further simplified the development process by allowing developers to define their infrastructure, including application services, networks, and volumes, in a single file. Docker Compose offers an efficient alternative to running multiple docker container create and docker container run commands.

In this tutorial, you will build a web application using the Laravel framework, with Nginx as the web server and MySQL as the database, all inside Docker containers. You will define the entire stack configuration in a docker-compose file, along with configuration files for PHP, MySQL, and Nginx.

Prerequisites

Before you start, you will need:

Step 1 — Downloading Laravel and Installing Dependencies

As a first step, we will get the latest version of Laravel and install the dependencies for the project, including Composer, the application-level package manager for PHP. We will install these dependencies with Docker to avoid having to install Composer globally.

First, check that you are in your home directory and clone the latest Laravel release to a directory called laravel-app:

  1. cd ~
  2. git clone https://github.com/laravel/laravel.git laravel-app

Move into the laravel-app directory:

  1. cd ~/laravel-app

Next, use Docker’s composer image to mount the directories that you will need for your Laravel project and avoid the overhead of installing Composer globally:

  1. docker run --rm -v $(pwd):/app composer install

Using the -v and --rm flags with docker run creates an ephemeral container that will be bind-mounted to your current directory before being removed. This will copy the contents of your ~/laravel-app directory to the container and also ensure that the vendor folder Composer creates inside the container is copied to your current directory.

As a final step, set permissions on the project directory so that it is owned by your non-root user:

  1. sudo chown -R $USER:$USER ~/laravel-app

This will be important when you write the Dockerfile for your application image in Step 4, as it will allow you to work with your application code and run processes in your container as a non-root user.

With your application code in place, you can move on to defining your services with Docker Compose.

Step 2 — Creating the Docker Compose File

Building your applications with Docker Compose simplifies the process of setting up and versioning your infrastructure. To set up our Laravel application, we will write a docker-compose file that defines our web server, database, and application services.

Open the file:

  1. nano ~/laravel-app/docker-compose.yml

In the docker-compose file, you will define three services: app, webserver, and db. Add the following code to the file, being sure to replace the root password for MYSQL_ROOT_PASSWORD, defined as an environment variable under the db service, with a strong password of your choice:

~/laravel-app/docker-compose.yml
version: '3'
services:
  
  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: your_mysql_root_password
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

The services defined here include:

  • app: This service definition contains the Laravel application and runs a custom Docker image, digitalocean.com/php, that you will define in Step 4. It also sets the working_dir in the container to /var/www.
  • webserver: This service definition pulls the nginx:alpine image from Docker and exposes ports 80 and 443.
  • db: This service definition pulls the mysql:5.7.22 image from Docker and defines a few environmental variables, including a database called laravel for your application and the root password for the database. You are free to name the database whatever you would like, and you should replace your_mysql_root_password with your own strong password. This service definition also maps port 3306 on the host to port 3306 on the container.

Each container_name property defines a name for the container, which corresponds to the name of the service. If you don’t define this property, Docker will assign a name to each container by combining a historically famous person’s name and a random word separated by an underscore.

To facilitate communication between containers, the services are connected to a bridge network called app-network. A bridge network uses a software bridge that allows containers connected to the same bridge network to communicate with each other. The bridge driver automatically installs rules in the host machine so that containers on different bridge networks cannot communicate directly with each other. This creates a greater level of security for applications, ensuring that only related services can communicate with one another. It also means that you can define multiple networks and services connecting to related functions: front-end application services can use a frontend network, for example, and back-end services can use a backend network.

Let’s look at how to add volumes and bind mounts to your service definitions to persist your application data.

Step 3 — Persisting Data

Docker has powerful and convenient features for persisting data. In our application, we will make use of volumes and bind mounts for persisting the database, and application and configuration files. Volumes offer flexibility for backups and persistence beyond a container’s lifecycle, while bind mounts facilitate code changes during development, making changes to your host files or directories immediately available in your containers. Our setup will make use of both.

Warning: By using bind mounts, you make it possible to change the host filesystem through processes running in a container, including creating, modifying, or deleting important system files or directories. This is a powerful ability with security implications, and could impact non-Docker processes on the host system. Use bind mounts with care.

In the docker-compose file, define a volume called dbdata under the db service definition to persist the MySQL database:

~/laravel-app/docker-compose.yml
...
#MySQL Service
db:
  ...
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - app-network
  ...

The named volume dbdata persists the contents of the /var/lib/mysql folder present inside the container. This allows you to stop and restart the db service without losing data.

At the bottom of the file, add the definition for the dbdata volume:

~/laravel-app/docker-compose.yml
...
#Volumes
volumes:
  dbdata:
    driver: local

With this definition in place, you will be able to use this volume across services.

Next, add a bind mount to the db service for the MySQL configuration files you will create in Step 7:

~/laravel-app/docker-compose.yml
...
#MySQL Service
db:
  ...
    volumes:
      - dbdata:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf
  ...

This bind mount binds ~/laravel-app/mysql/my.cnf to /etc/mysql/my.cnf in the container.

Next, add bind mounts to the webserver service. There will be two: one for your application code and another for the Nginx configuration definition that you will create in Step 6:

~/laravel-app/docker-compose.yml
#Nginx Service
webserver:
  ...
  volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
  networks:
      - app-network

The first bind mount binds the application code in the ~/laravel-app directory to the /var/www directory inside the container. The configuration file that you will add to ~/laravel-app/nginx/conf.d/ will also be mounted to /etc/nginx/conf.d/ in the container, allowing you to add or modify the configuration directory’s contents as needed.

Finally, add the following bind mounts to the app service for the application code and configuration files:

~/laravel-app/docker-compose.yml
#PHP Service
app:
  ...
  volumes:
       - ./:/var/www
       - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
  networks:
      - app-network

The app service is bind-mounting the ~/laravel-app folder, which contains the application code, to the /var/www folder in the container. This will speed up the development process, since any changes made to your local application directory will be instantly reflected inside the container. You are also binding your PHP configuration file, ~/laravel-app/php/local.ini, to /usr/local/etc/php/conf.d/local.ini inside the container. You will create the local PHP configuration file in Step 5.

Your docker-compose file will now look like this:

~/laravel-app/docker-compose.yml
version: '3'
services:
  
  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: your_mysql_root_password
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge
#Volumes
volumes:
  dbdata:
    driver: local

Save the file and exit your editor when you are finished making changes.

With your docker-compose file written, you can now build the custom image for your application.

Step 4 — Creating the Dockerfile

Docker allows you to specify the environment inside of individual containers with a Dockerfile. A Dockerfile enables you to create custom images that you can use to install the software required by your application and configure settings based on your requirements. You can push the custom images you create to Docker Hub or any private registry.

Our Dockerfile will be located in our ~/laravel-app directory. Create the file:

  1. nano ~/laravel-app/Dockerfile

This Dockerfile will set the base image and specify the necessary commands and instructions to build the Laravel application image. Add the following code to the file:

~/laravel-app/php/Dockerfile
FROM php:7.2-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

First, the Dockerfile creates an image on top of the php:7.2-fpm Docker image. This is a Debian-based image that has the PHP FastCGI implementation PHP-FPM installed. The file also installs the prerequisite packages for Laravel: mcrypt, pdo_mysql, mbstring, and imagick with composer.

The RUN directive specifies the commands to update, install, and configure settings inside the container, including creating a dedicated user and group called www. The WORKDIR instruction specifies the /var/www directory as the working directory for the application.

Creating a dedicated user and group with restricted permissions mitigates the inherent vulnerability when running Docker containers, which run by default as root. Instead of running this container as root, we’ve created the www user, who has read/write access to the /var/www folder thanks to the COPY instruction that we are using with the --chown flag to copy the application folder’s permissions.

Finally, the EXPOSE command exposes a port in the container, 9000, for the php-fpm server. CMD specifies the command that should run once the container is created. Here, CMD specifies "php-fpm", which will start the server.

Save the file and exit your editor when you are finished making changes.

You can now move on to defining your PHP configuration.

Step 5 — Configuring PHP

Now that you have defined your infrastructure in the docker-compose file, you can configure the PHP service to act as a PHP processor for incoming requests from Nginx.

To configure PHP, you will create the local.ini file inside the php folder. This is the file that you bind-mounted to /usr/local/etc/php/conf.d/local.ini inside the container in Step 2. Creating this file will allow you to override the default php.ini file that PHP reads when it starts.

Create the php directory:

  1. mkdir ~/laravel-app/php

Next, open the local.ini file:

  1. nano ~/laravel-app/php/local.ini

To demonstrate how to configure PHP, we’ll add the following code to set size limitations for uploaded files:

~/laravel-app/php/local.ini
upload_max_filesize=40M
post_max_size=40M

The upload_max_filesize and post_max_size directives set the maximum allowed size for uploaded files, and demonstrate how you can set php.ini configurations from your local.ini file. You can put any PHP-specific configuration that you want to override in the local.ini file.

Save the file and exit your editor.

With your PHP local.ini file in place, you can move on to configuring Nginx.

Step 6 — Configuring Nginx

With the PHP service configured, you can modify the Nginx service to use PHP-FPM as the FastCGI server to serve dynamic content. The FastCGI server is based on a binary protocol for interfacing interactive programs with a web server. For more information, please refer to this article on Understanding and Implementing FastCGI Proxying in Nginx.

To configure Nginx, you will create an app.conf file with the service configuration in the ~/laravel-app/nginx/conf.d/ folder.

First, create the nginx/conf.d/ directory:

  1. mkdir -p ~/laravel-app/nginx/conf.d

Next, create the app.conf configuration file:

  1. nano ~/laravel-app/nginx/conf.d/app.conf

Add the following code to the file to specify your Nginx configuration:

~/laravel-app/nginx/conf.d/app.conf
server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        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;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

The server block defines the configuration for the Nginx web server with the following directives:

  • listen: This directive defines the port on which the server will listen to incoming requests.
  • error_log and access_log: These directives define the files for writing logs.
  • root: This directive sets the root folder path, forming the complete path to any requested file on the local file system.

In the php location block, the fastcgi_pass directive specifies that the app service is listening on a TCP socket on port 9000. This makes the PHP-FPM server listen over the network rather than on a Unix socket. Though a Unix socket has a slight advantage in speed over a TCP socket, it does not have a network protocol and thus skips the network stack. For cases where hosts are located on one machine, a Unix socket may make sense, but in cases where you have services running on different hosts, a TCP socket offers the advantage of allowing you to connect to distributed services. Because our app container is running on a different host from our webserver container, a TCP socket makes the most sense for our configuration.

Save the file and exit your editor when you are finished making changes.

Thanks to the bind mount you created in Step 2, any changes you make inside the nginx/conf.d/ folder will be directly reflected inside the webserver container.

Next, let’s look at our MySQL settings.

Step 7 — Configuring MySQL

With PHP and Nginx configured, you can enable MySQL to act as the database for your application.

To configure MySQL, you will create the my.cnf file in the mysql folder. This is the file that you bind-mounted to /etc/mysql/my.cnf inside the container in Step 2. This bind mount allows you to override the my.cnf settings as and when required.

To demonstrate how this works, we’ll add settings to the my.cnf file that enable the general query log and specify the log file.

First, create the mysql directory:

  1. mkdir ~/laravel-app/mysql

Next, make the my.cnf file:

  1. nano ~/laravel-app/mysql/my.cnf

In the file, add the following code to enable the query log and set the log file location:

~/laravel-app/mysql/my.cnf
[mysqld]
general_log = 1
general_log_file = /var/lib/mysql/general.log

This my.cnf file enables logs, defining the general_log setting as 1 to allow general logs. The general_log_file setting specifies where the logs will be stored.

Save the file and exit your editor.

Our next step will be to start the containers.

Step 8 — Modifying Environment Settings and Running the Containers

Now that you have defined all of your services in your docker-compose file and created the configuration files for these services, you can start the containers. As a final step, though, we will make a copy of the .env.example file that Laravel includes by default and name the copy .env, which is the file Laravel expects to define its environment:

  1. cp .env.example .env

You can now modify the .env file on the app container to include specific details about your setup.

Open the file using nano or your text editor of choice:

  1. nano .env

Find the block that specifies DB_CONNECTION and update it to reflect the specifics of your setup. You will modify the following fields:

  • DB_HOST will be your db database container.
  • DB_DATABASE will be the laravel database.
  • DB_USERNAME will be the username you will use for your database. In this case, we will use laraveluser.
  • DB_PASSWORD will be the secure password you would like to use for this user account.
/var/www/.env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=your_laravel_db_password

Save your changes and exit your editor.

With all of your services defined in your docker-compose file, you just need to issue a single command to start all of the containers, create the volumes, and set up and connect the networks:

  1. docker-compose up -d

When you run docker-compose up for the first time, it will download all of the necessary Docker images, which might take a while. Once the images are downloaded and stored in your local machine, Compose will create your containers. The -d flag daemonizes the process, running your containers in the background.

Once the process is complete, use the following command to list all of the running containers:

  1. docker ps

You will see the following output with details about your app, webserver, and db containers:

Output
CONTAINER ID NAMES IMAGE STATUS PORTS c31b7b3251e0 db mysql:5.7.22 Up 2 seconds 0.0.0.0:3306->3306/tcp ed5a69704580 app digitalocean.com/php Up 2 seconds 9000/tcp 5ce4ee31d7c0 webserver nginx:alpine Up 2 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp

The CONTAINER ID in this output is a unique identifier for each container, while NAMES lists the service name associated with each. You can use both of these identifiers to access the containers. IMAGE defines the image name for each container, while STATUS provides information about the container’s state: whether it’s running, restarting, or stopped.

We’ll now use docker-compose exec to set the application key for the Laravel application. The docker-compose exec command allows you to run specific commands in containers.

The following command will generate a key and copy it to your .env file, ensuring that your user sessions and encrypted data remain secure:

  1. docker-compose exec app php artisan key:generate

You now have the environment settings required to run your application. To cache these settings into a file, which will boost your application’s load speed, run:

  1. docker-compose exec app php artisan config:cache

Your configuration settings will be loaded into /var/www/bootstrap/cache/config.php on the container.

As a final step, visit http://your_server_ip in the browser. You will see the following home page for your Laravel application:

Laravel Home Page

With your containers running and your configuration information in place, you can move on to configuring your user information for the laravel database on the db container.

Step 9 — Creating a User for MySQL

The default MySQL installation only creates the root administrative account, which has unlimited privileges on the database server. In general, it’s better to avoid using the root administrative account when interacting with the database. Instead, let’s create a dedicated database user for our application’s Laravel database.

To create a new user, execute an interactive bash shell on the db container with docker-compose exec:

  1. docker-compose exec db bash

Inside the container, log into the MySQL root administrative account:

  1. mysql -u root -p

You will be prompted for the password you set for the MySQL root account during installation in your docker-compose file.

Start by checking for the database called laravel, which you defined in your docker-compose file. Run the show databases command to check for existing databases:

  1. show databases;

You will see the laravel database listed in the output:

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

Next, create the user account that will be allowed to access this database. Our username will be laraveluser, though you can replace this with another name if you’d prefer. Just be sure that your username and password here match the details you set in your .env file in the previous step:

  1. GRANT ALL ON laravel.* TO 'laraveluser'@'%' IDENTIFIED BY 'your_laravel_db_password';

Flush the privileges to notify the MySQL server of the changes:

  1. FLUSH PRIVILEGES;

Exit MySQL:

  1. EXIT;

Finally, exit the container:

  1. exit

You have configured the user account for your Laravel application database and are ready to migrate your data and work with the Tinker console.

Step 10 — Migrating Data and Working with the Tinker Console

With your application running, you can migrate your data and experiment with the tinker command, which will initiate a PsySH console with Laravel preloaded. PsySH is a runtime developer console and interactive debugger for PHP, and Tinker is a REPL specifically for Laravel. Using the tinker command will allow you to interact with your Laravel application from the command line in an interactive shell.

First, test the connection to MySQL by running the Laravel artisan migrate command, which creates a migrations table in the database from inside the container:

  1. docker-compose exec app php artisan migrate

This command will migrate the default Laravel tables. The output confirming the migration will look like this:

Output
Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table

Once the migration is complete, you can run a query to check if you are properly connected to the database using the tinker command:

  1. docker-compose exec app php artisan tinker

Test the MySQL connection by getting the data you just migrated:

  1. \DB::table('migrations')->get();

You will see output that looks like this:

Output
=> Illuminate\Support\Collection {#2856 all: [ {#2862 +"id": 1, +"migration": "2014_10_12_000000_create_users_table", +"batch": 1, }, {#2865 +"id": 2, +"migration": "2014_10_12_100000_create_password_resets_table", +"batch": 1, }, ], }

You can use tinker to interact with your databases and to experiment with services and models.

With your Laravel application in place, you are ready for further development and experimentation.

Conclusion

You now have a LEMP stack application running on your server, which you’ve tested by accessing the Laravel welcome page and creating MySQL database migrations.

Key to the simplicity of this installation is Docker Compose, which allows you to create a group of Docker containers, defined in a single file, with a single command. If you would like to learn more about how to do CI with Docker Compose, take a look at How To Configure a Continuous Integration Testing Environment with Docker and Docker Compose on Ubuntu 16.04. If you want to streamline your Laravel application deployment process then How to Automatically Deploy Laravel Applications with Deployer on Ubuntu 16.04 will be a relevant resource.

Want to launch a high-availability MySQL cluster in a few clicks? DigitalOcean offers worry-free MySQL managed database hosting. We’ll handle maintenance and updates and even help you migrate your database from external servers, cloud providers, or self-hosted solutions. Leave the complexity to us, so you can focus on building a great application.

Learn more here

About the authors
Default avatar

Cloud Architect

Principal Engineer | Cloud Architect | Adventurer



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
76 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Step 9 — Creating a User for MySQL

Next, create the user account that will be allowed to access this database. Our username will be laraveluser, though you can replace this with another name if you’d prefer. Just be sure that your username and password here match the details you set in your .env file in the previous step:

GRANT ALL ON laravel.* TO 'laraveluser'@'%' IDENTIFIED BY 'your_laravel_db_password';

to be

GRANT ALL PRIVILEGES  ON laravel.* TO 'laraveluser'@'%' IDENTIFIED BY 'your_laravel_db_password';

and thanks for the great article.

Thanks for the feedback.

As seen in MySQL documentation, ‘ALL’ is a synonym for ‘ALL PRIVILEGES’.

This is a great tutorial, but can someone help me to understand how to implement Certbot and let’s encrypt with this set up? (Docker)

Thank for the tutorial but what about production stage? Should the data be persisted there as well and what should be changed in the container in general?

In a production environment, we would usually package the application code with the container as there is no need for bind mounts. The DB data will be persisted in the named volume as demonstrated in this tutorial.

can you tell why we do so, what are the benefits

This tutorial was great and I didn’t have any problems, so thank you! My one questions is how do I get the current Laravel project off of the server so that I can make my changes and not have all the settings over written when I push it to production.

If there is a tutorial for this please point me in that direction. Currently using Ubuntu 18.10

Hello! I have a question. I’m trying to create a different db container for different projects. But, docker doesn’t create the new db container with the database name. Show the .yml file

db_xxx:
    image: mysql:5.7.22
    container_name: db_xxx
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: xxx
      MYSQL_ROOT_PASSWORD: password
      SERVICE_TAGS: dev
      SERVICE_NAME: db_xxx
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

With docker ps I can actually see the correct names on containers, and with “exec” can access to the container name db_xxx, but there’s no database ‘xxx’, just ‘laravel’ db.

I hope to be clear.

Thanks in advance.

The problem here is that you are binding the dbdata:/var/lib/mysql named volume which contains the existing data from the db service, hence the ‘laravel’ db. You need to define a new volume and bind like so xxx:/var/lib/mysql.

Thanks a lot for your response, was really helpful.

Thanks for the tutorial. Why are we using the digitalocean.com/php instead of an official php image on the docker container. Also why do we need both definitions on the Dockerfile for php:7.2-fpm and the docker-compose file digitalocean.com/php?

Dockerfile takes the official php image (php:7.2-fpm), installs all the dependancies and does all the application setup.

docker-compose runs the above Dockerfile to create our custom image digitalocean.com/php

Why does the Nginx container need application code volume bind? Does it not proxy the request to the PHP container which then handles the request?

Would we then need to package application code into both the PHP and Nginx container or keep the application data in a shared volume

The Nginx configuration requires us to set a root path for the application when nginx is not acting as a reverse proxy webserver. In our case, Nginx is not acting as a reverse proxy server which would be the case if we were using NodeJs where we only required to forward the request to the node server.

Yes in our case we would need to share the application data using a volume. In a more complex setup, the application code can be shared using NFS.

This is not entirely right. The fastcgi_pass proxies the request to fpm, similarly as reverse proxy to node server. We do not need application code to be available to nginx, at all. It runs fine without any access to our php code. All requests are passed to fpm which runs php and has access to all files. However, nginx does need access to static files, which are not passed to fpm.

Agree with this, but how would it be done in a production environment? I’ve removed the volume mount from the Nginx container and results in a 404 error on all requests.

I also agree with @ruuter. A LEMP setup uses nginx as a reverse proxy and php-fpm to handle all requests for php generated content.

I don’t have production experience, but to include static files I think you just need to bind mount the /public folder for development and in production include the /public folder when building the webserver image.

How do I implement this? I am pretty new to docker and nginx, and this separation of app and webserver looks appealing to me.

Dear Admin Please help me. i’m do step on aricle to step run docker-composer.

  • docker-compose exec app php artisan key:generate then show error: OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused “exec: "php": executable file not found in $PATH”: unknown

I’m use ubuntu 18.04.

thank so much.

Hi have you a solution for this please? The quick solution is chmod 777 but I want to take the best way thanks!

Try the following command to generate the app key:

docker exec -it app php artisan key:generate

Please make sure your app container is running by checking that the container is “Up” in the status column of the output from docker ps command.

How would you run laravel’s queue:work with this solution? Could it be extended to include supervisor perhaps?

You could use supervisor for the job.

The simplest way to run a queue in this setup is by using the following example command:

docker exec -it app php artisan queue:work

The docker exec -it app followed by the artisan queue command will run the queue.

app          | [11-Mar-2019 01:07:06] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
app          | [11-Mar-2019 01:07:06] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
app          | [11-Mar-2019 01:07:06] NOTICE: fpm is running, pid 1
app          | [11-Mar-2019 01:07:06] NOTICE: ready to handle connections
db           | Initializing database

hi…thank you for this it really help me. do you have anything on how to use npm on docker? for compiling frontend assets?

Hi @besingamkb I made this changes to my Dockerfile and it worked :)

Install dependencies

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - &&
apt-get update && apt-get install -y
nodejs
build-essential
mysql-client
libpng-dev
libjpeg62-turbo-dev
libfreetype6-dev
locales
zip
jpegoptim optipng pngquant gifsicle
vim
unzip
git
curl &&
npm install -g npm

I have to use the db on the host, how would I access the host db with this docker-compose?

Is there a way to connect the bridge network with a host port?

Hi, Thanks for the great tutorial, When i did “docker-compose exec app php artisan migrate” It says “Connection refused (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations)” The .env file {DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306} I changed to port of mysql in docker-compose file to 3309:3306 instead of 3306:3309 Can you help me to solve this problem? Thanks

Try with DB_HOST=db instead DB_HOST=mysql.

change .env file and cache in ./bootstrap/cache/config.php change DBHOST=mysql to DBHOST=db

verything was very nice, I think it was an excellent tutorial, I configured it in windows and it did not print any error :) but in the end when I put in my browser http: // localhost did not work, change the port of my host pointing to nginx ( webserver) for another (8100), I did the same in the server block (listen 8100), then docker-compose restart and it does not work. Does someone help me?

I have the same error and I can not correct it

Hi, I am able to successfully execute the docker-compose exec app nano .env command, but when trying to make changes to the .env file it is a read-only file and am unable to save any changes. I got around this by running docker exec -it -u root laravel-app bash but I was wondering if there is a better way to execute this?

Another issue I ran into eventually was with getting a permission denied error from Laravel when I visited the machine’s IP address. I got around this by changing the directory permissions and group as explained (here). My question here would be; did I do things correctly or should have the directory permissions and ownership been set earlier?

While I was able to eventually get things working, I’m not sure if I did things in the most secure or efficient way and would like some guidance for future projects.

docker exec --user=“root” -it app /bin/bash apt-get update apt-get install nano nano .env

for me the Step 9 — Creating a User for MySQL is:

CREATE USER 'laraveluser'@'%' IDENTIFIED WITH mysql_native_password BY 'your_laravel_db_password';

then

GRANT ALL PRIVILEGES  ON laravel.* TO 'laraveluser'@'%'

and thanks for all

I figured out that there was a missing dependency when configuring GD, so I added libfreetype6-dev to the Dockerfile apt-get install step.

Anyway, I am feeling a little lost here, as I cannot seem to connect to the webserver locally. I am running Docker on Windows and it has been working fine for other docker-compose configurations, but this time around I’m getting connection refused no matter what IP I add to my hosts-file (my nginx configuration is listening for devdomain.test). Any tips on how to troubleshoot here? I’ve restarted the terminal, restarted Docker for Windows, checked my configs and docker inspect’ed to confirm even singular container IPs. I’m using pretty much the exact same configuration as posted here.

Okay, so I looked at a docker-compose configuration I had from an old project, and I noticed a difference between the nginx-images used. This tutorial proposes nginx:alpine but I had used nginx:latest in my previous project. When I changed from alpine to latest I could connect normally… I still don’t know why, because a nginx:alpine-container by itself worked just fine.

Hey there,

thank you at first for your tutorial. So far, it was very helpful.

As a next step, I watched some YouTube videos and in one specific video (https://www.youtube.com/watch?v=bSG2YMqJJys) he is setting up bootstrap with the command “npm install”. Unfortunately, trying to do so: docker-compose exec app npm install brings the message npm could not be found. Therefore, I extended the Dockerfile for nodejs but still it is not working. Can you give me some advice how to load bootstraps?

Thanks md

A short update. I used following nodejs image: nodejs: image: wiwatsrt/docker-laravel-nodejs volumes: - ./:/var/www

and ran the command “docker-compose run --rm nodejs npm install” It created the folder node_modules and I adjusted the app.scss file with: @import ‘node_modules/bootstrap/scss/bootstrap’;

But still my about.blade.php does not use the bootstrap style for the navbar:

<!DOCTYPE html> <html lang=“{{config(‘app.locale’) }}”> <head> <meta charset=“utf-8”> <meta http-equiv=“X-UA-Compatible” content=“IE=Edge”> <meta name=“viewport” content=“width=device-width, initial-scale=1”> <link rel=“stylesheet” href=“{{asset(‘css/app.css’)}}”> <title>{{config(‘app.name’, ‘Laravel’)}}</title> </head> <body> @include(‘inc.navbar’) About </body>

navbar.blade.php: <nav class=“navbar navbar-inverse”> <div class=“container”> <div class=“navbar-header”> <button type=“button” class=“navbar-toggle collapsed” data-toggle=“collapse” data-target=“#navbar” aria-expanded=“false” aria-controls=“navbar”> <span class=“sr-only”>Toggle navigation</span> <span class=“icon-bar”></span> <span class=“icon-bar”></span> <span class=“icon-bar”></span> </button> <a class=“navbar-brand” href=“/”>{{config(‘app.name’, ‘Laravel’)}}</a> </div> <div id=“navbar” class=“collapse navbar-collapse”> <ul class=“nav navbar-nav”> <li class=“active”><a href=“#”>Home</a></li> <li><a href=“/about”>About</a></li> <li><a href=“#”>Contact</a></li> </ul> </div> </div> </nav>

sorry to bump this thread… I just wanted to add that I was using the wrong bootstrap version:

<nav class=“navbar navbar-expand-lg navbar-light bg-light”> <a class=“navbar-brand” href=“/”>{{config(‘app.name’, ‘Laravel’)}}</a> <button class=“navbar-toggler” type=“button” data-toggle=“collapse” data-target=“#navbarNav” aria-controls=“navbarNav” aria-expanded=“false” aria-label=“Toggle navigation”> <span class=“navbar-toggler-icon”></span> </button> <div class=“collapse navbar-collapse” id=“navbarNav”> <ul class=“navbar-nav”> <li class=“nav-item active”> <a class=“nav-link” href=“#”>Home <span class=“sr-only”>(current)</span></a> </li> <li class=“nav-item”> <a class=“nav-link” href=“/about”>About</a> </li> <a class=“nav-link” href=“/contact”>Contact</a> </ul> </div> </nav>

Thank you fo this tutorial Please how can i have mysql database files in the host computer and managed them with a IDE like visual studio code ?

Thanks so much!!!

To avoid clustering my laravel root folder i reorganized the docker files as below

/.docker/mysql/my.conf /.docker/nginx/conf.d/app.conf /.docker/php/local.ini /.docker/Dockerfile

Hi Thanks for your amazing tutorial. In Step 8 when i try to create docker container, below error occur so I want to help me to solve it. I try to :

docker-compose up -d --force-recreate app

and the error is:

E: Package 'mysql-client' has no installation candidate
ERROR: Service 'app' failed to build: The command '/bin/sh -c apt-get update && apt-get install -y     build-essential     mysql-client     libpng-dev     libjpeg62-turbo-dev     libfreetype6-dev     locales     zip     jpegoptim optipng pngquant gifsicle     vim     unzip     git     curl' returned a non-zero code: 100

Thanks.

Same, did you figure this out?

I just had this problem also, seems to be an issue related to a new release of php7. It can be fixed by replacing mysql-client with mariadb-client and it starts working again 🎉

Hi, thanks for this complete tutorial… I did everything right until step 8(I mean I did not get any errors until step 8) but when i execute ($docker-compose up -d) i got error for installing free type exension (–with-freetype-dir) so I removed this from the file then it completed without errors so I could see all the three containers are up and running with ($docker ps) command and i continued to the end of step 8… but I got 500 internal server error when I go to my browser… dose my action (removing php extension from docker-compose) caused this error? can any one help me please?? I realy need to do this for my work thank you all.

try to change php:7.2 to php:7.3-fpm-stretch but you have to put again --with-freetype-dir

https://github.com/docker-library/php/issues/865

my DockerFile work fine:

FROM php:7.3-fpm-stretch

Copy composer.lock and composer.json

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

Set working directory

WORKDIR /var/www

Install dependencies

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - &&
apt-get update && apt-get install -y
nodejs
build-essential
mariadb-client
libssl-dev
zlib1g-dev
libjpeg-dev
libwebp-dev
libxpm-dev
pkg-config
libpng-dev
libjpeg62-turbo-dev
libfreetype6-dev
locales
zip
jpegoptim optipng pngquant gifsicle
vim
unzip
libzip-dev
git
curl &&
npm install -g npm

Clear cache

RUN apt-get clean && rm -rf /var/lib/apt/lists/*

Install extensions

RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-webp-dir --with-jpeg-dir RUN docker-php-ext-install gd

Install composer

RUN curl -sS https://getcomposer.org/installer | php – --install-dir=/usr/local/bin --filename=composer

Add user for laravel application

RUN groupadd -g 1000 www RUN useradd -u 1000 -ms /bin/bash -g www www

Copy existing application directory contents

COPY . /var/www

Copy existing application directory permissions

COPY --chown=www:www . /var/www

Change current user to www

USER www

Expose port 9000 and start php-fpm server

EXPOSE 9000 CMD [“php-fpm”]

thank you so muuuuuuuuuuchh

Great tutorial, thanks!

I had to change mysql-client to default-mysql-client in the apt-get install section of the Dockerfile.

Hello! Great tutorial but I have some problems. When I access the front page of my laravel application, automaticaly three proccess of php-fpm: pool www starts with 30%~40% of cpu usage each, causing the application to slow even accessing the front page which is a simple laravel page. When I run the app using the local php wihtout the docker enviorement it works fine and de cpu usage reaches maximum 10%. Can someone help me?

Are you running docker on your local machine, e.g. Docker Desktop? If yes, you may want to check if you have assigned sufficient resources to the docker machine. Perhaps lack of RAM is causing the containers to swap to disk.

Great tutorial, but I have a question, why in step 8 you did cp .env.example .env when you have a dockerfile and you could do COPY .env.example .env and composer install ?

I tried to do this but vendor folder is hidden and .env doesn’t appear

keep getting this error when i want to migrate i have given my user privellege but still have this error any help please ?

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = ishangodb and table_name = migrations and table_type = ‘BASE TABLE’)

at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664 660| // If an exception occurs when attempting to run a query, we’ll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database’s errors. 663| catch (Exception $e) {

664| throw new QueryException( 665| $query, $this->prepareBindings($bindings), $e 666| ); 667| } 668|

Exception trace:

1 PDOException::(“SQLSTATE[HY000] [2002] No such file or directory”) /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

2 PDO::__construct(“mysql:host=localhost;port=3306;dbname=ishangodb”, “ishangouser”, “ishangosecret”, []) /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

Please use the argument -v to see more details.

docker-compose exec app php artisan key:generate

UnexpectedValueException : The stream or file “/var/www/storage/logs/laravel-2019-09-19.log” could not be opened: failed to open stream: Permission denied

at /var/www/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:111 107| restore_error_handler(); 108| if (!is_resource($this->stream)) { 109| $this->stream = null; 110|

111| throw new \UnexpectedValueException(sprintf('The stream or file “%s” could not be opened: '.$this->errorMessage, $this->url)); 112| } 113| } 114| 115| if ($this->useLocking) {

Exception trace:

1 Monolog\Handler\StreamHandler::write() /var/www/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php:120

2 Monolog\Handler\RotatingFileHandler::write() /var/www/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php:42

Please use the argument -v to see more details.

Hi, thank you for great tutorial. I want to use php:7.4-fpm to build my app but I get this error:

ERROR: Service 'app' failed to build: The command '/bin/sh -c docker-php-ext-install pdo_mysql mbstring zip exif pcntl' returned a non-zero code: 1

How can is solve this problem?

This is a great tutorial,Faced two issues while setting up

1] once i ran docker-compose up -d facing error package ‘mysql-client’ has no installation candidate docker

Solution as php7.2-fpm doesn’t support mysql-client replace it with mariadb-client refer this article for more insights https://stackoverflow.com/questions/57048428/e-package-mysql-client-has-no-installation-candidate-in-php-fpm-image-build-u

2] while migrating data faced throwing error PDOException - SQLSTATE[HY000] [2002] Connect refused

Solution as we are using docker container we have to set the IP in .env file change db DB_HOST=IP,To get the IP from the mysql container this give command docker inspect containerid(Of Mysql)

For those getting a Package ‘mysql-client’ has no installation candidate error on docker-compose up -d:

Change line 12 in Dockerfile from :

mysql-client \

to :

default-mysql-client \

No matter what I do it always goes to the default nginx index file and not the laravel index file. Any ideas why?

Quick note, I had to change mysql-client to default-mysql-client for installing dependencies step

Did anyone else run into a memory issue? When I ran “docker run --rm -v $(pwd):/app composer install” it threw an error seen here.

Warning: proc_open(): fork failed - Out of memory in phar:///usr/bin/composer/vendor/symfony/console/Application.php on line 952
                                            
  [ErrorException]                          
  proc_open(): fork failed - Out of memory

I think it is similar to this issue. https://github.com/composer/composer/issues/945.

They suggest creating swap memory as an alternative to increasing the size of the droplet. Is there a way to do this using docker or should I go to a larger droplet temporarily? My current droplet is 1GB.

First of all, thank you for the tutorial!

When I run commands like docker-compose exec app php artisan make:controller… etc, the owner of the newly created files is root. So I need to run chmod to have a possibility to edit this files. Is it possible to make these new files be created under the correct user?

Why my browser shows home page as nginx welcome instead of laravel app home page?

Is it one droplet per container? Or we can use one droplet for many container?

quick question: you’re configuring nginx root to /var/www/public but the volume you mount in the nginx service is /var/www. is this a mistake?

I’ve been unable to install laravel using composer. I used the command given in the guide. My output is the below:

user@DESKTOP-3SA8N6S:~/laravel-app$ docker run --rm -v $(pwd):/app composer install
Composer could not find a composer.json file in /app
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section

The laravel-app directory contains a composer.json file. I don’t know why it’s not working.

Hello! It is a really good explained HowTo. I learned a lot. Thanks!

However, I am having problems with the permissions for the user I setup for MySQL in the Laravel application.

  • After executing the Step 9. I am receiving the following:
$ docker-compose exec app php artisan migrate

Illuminate\Database\QueryException

SQLSTATE[HY000] [1045] Access denied for user 'mariomenjr'@'app.rastreo-academico_app-network' (using password: YES) (SQL: select * from information_schema.tables where table_schema = rastreo_academico and table_name = migrations and table_type = 'BASE TABLE')

# Omitted Laravel's nice code snapshot for simplicity
  • Then, I tried many approaches. Reflected in the MySQL users table:
mysql> select user, host from mysql.user;
+---------------+-----------------------------------+
| user          | host                              |
+---------------+-----------------------------------+
| mariomenjr    | %                                 |
| root          | %                                 |
| mariomenjr    | app.rastreo-academico_app-network |
| mysql.session | localhost                         |
| mysql.sys     | localhost                         |
| root          | localhost                         |
+---------------+-----------------------------------+

# As you can see, I first tried with the regular 'mariomenjr'@'%' setting. Did not work.
# Then, I tried with the 'mariomenjr'@'app.rastreo-academico_app-network' as Laravel was presenting that option in the error message.
# Did not work either.
  • Then I though, probably the config cache is not being updated. But it is:
# /var/www/bootstrap/cache/config.php
...
array (
        'driver' => 'mysql',
        'url' => NULL,
        'host' => 'db',
        'port' => '3306',
        'database' => 'rastreo_academico',
        'username' => 'mariomenjr',
        'password' => '$HzxXyo^i',
        'unix_socket' => '',
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => NULL,
        'options' => 
        array (
        ),
      ),
...

Is there something else I can try? Thanks in advance!

# docker-compose MySQL section
...
  db:
    image: mysql:5.7.22
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: "${DB_DATABASE}"
      MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network
...

# .env DB section
...
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=rastreo_academico
DB_USERNAME=mariomenjr
DB_PASSWORD=blahblah
...

I had the same problem for 4 days. Finally was able to fix it by changing the database password,

environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: voucher_app
      MYSQL_USER: app_user
      MYSQL_PASSWORD: ******
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql

these were environment variables in docker-compose file, I am assuming that may be my MYSQL_PASSWORD was too long or had invalid character.

delete your database volume. run $ docker-compose up -d run the grant commands in step 9.

and follow on. let me know if it works for you too. I wanna know :)

Very nice tutorial! Just two small problems:

  1. You should add a .dockerignore containing vendor to prevent it from being copied to the app service and install the dependencies in the Dockerfile with composer install instead of relying on the user having to do it manually every time he clones the project from a remote source
  2. You can remove image: digitalocean.com/php from the app service in the docker-compose.yml file, as you’re using a Dockerfile to generate the service

If you are doing this on macOs and getting the “SQLSTATE[HY000] [2002] No such file or directory…” error, just add 'unix_socket' => env('DB_SOCKET', ''), to the mysql section in the ./config/database.php file, and DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock to your .env file.

Hi! I got an error and need help, please:

Status: Downloaded newer image for mysql:5.7.22 Creating app … Creating db … Creating webserver … error Creating app … done Creating db … done 3): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use

ERROR: for webserver Cannot start service webserver: driver failed programming external connectivity on endpoint webserver (9c4f43736ef3d6b6f3159d7ce240e0cdf8ddf7c5655cf1438b6e3d4489e4da33): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use

What should I do? Thanks

Hello, fantastic article. Good explanations on each step.

However, I have 2 questions i mind related to this setup.

(1) I could not understand what the first step is doing.

docker run --rm -v $(pwd):/app composer install

What is the reason for “/app” after the colon? The container will be destroyed anyway after composer finishes installing Laravel. This “/app” directory can’t be used by any other container. Is it just a temp directory to get composer install Laravel inside the container and get it reflected in my local directory laravel_app? If so, will composer not update all config files inside the container assuming the directory name is “app”, but those config files will not work anymore as the local directory is “laravel_app”?

(2) Since laravel got installed in local directory but not inside a container volume, how would I replicate this entire setup to another unix machine without following all these steps in that machine too?

I am currently creating this setup in a VM that I have access temporarily. After the setup works, I want to replicate it to another Ubuntu machine that I will have more control over. Is there any way to bundle the entire setup, including persistent data and just reproduce in another Ubuntu machine?

Will really appreciate if you can address the above (2) questions.

Thank You so much it works for me. I have one question now I want to change my PHP version, I have added in Dockerfile and use these commands docker-compose down docker-compose up -d but still, PHP version is not updated someone please help me

This is a great tutorial, I want to implement let’s encrypt SSL with this one can someone help me.

Please help me here

This error occurs. I did it on Ubuntu 18 and followed all the procedures. I can’t find a reason why the composer.lock file is missing.

youngjin_cheon@instance-1:~/laravel-app$ docker-compose up -d
Creating network "laravel-app_app-network" with driver "bridge"
Creating volume "laravel-app_dbdata" with local driver
Building app
Step 1/16 : FROM php:7.2-fpm
7.2-fpm: Pulling from library/php
bf5952930446: Pull complete
a409b57eb464: Pull complete
3192e6c84ad0: Pull complete
43553740162b: Pull complete
f225612c7ad9: Pull complete
0342ae2b9139: Pull complete
bee69a16bd40: Pull complete
3bb983246d5f: Pull complete
3d974ad783a4: Pull complete
dd6f02f36656: Pull complete
1619d78ddff6: Pull complete
Digest: sha256:7be1ff8d64f73141bca4ca44acd19fd83f2b26be024515befa725392d99f2565
Status: Downloaded newer image for php:7.2-fpm
 ---> 02438bb242a1
Step 2/16 : COPY composer.lock composer.json /var/www/
ERROR: Service 'app' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder610168676/composer.lock: no such file or directory

Thanks for the well written article. However, I have troubles in step 1. I can’t run composer like this because I am missing depenedecies like php-zip. I also got this error when I tried docker-compose up:

ERROR: for webserver Cannot start service webserver: OCI runtime create failed: container_linux.go:349: starting container process caused “process_linux.go:449: container init caused "rootfs_linux.go:58: mounting \"/home/adam/www/docku/realworld/backoffice/docker-compose/nginx/conf.d\" to rootfs \"/var/lib/docker/overlay2/41076b397b8c0f5b8b9109fe67ab8927ae92d42a7eefbc5c9081a6ff1e55bdc3/merged\" at \"/var/lib/docker/overlay2/41076b397b8c0f5b8b9109fe67ab8927ae92d42a7eefbc5c9081a6ff1e55bdc3/merged/etc/nginx/conf.d\" caused \"not a directory\""”: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

i keep getting this, trying to login as root in mysql:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

@blait455 One possible cause for this is if you ran docker-compose up -d with a different root password specified. The dbdata volume sticks around even after the container and images are destroyed. You could try removing/pruning the containers and volumes.

when trying to login to mysql i keep getting this

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

This comment has been deleted

    If you are doing this on macOs and getting the “SQLSTATE[HY000] [2002] No such file or directory…” error, just add 'unix_socket' => env('DB_SOCKET', ''), to the mysql section in the ./config/database.php file, and DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock to your .env file.

    Hi! I got an error and need help, please:

    Status: Downloaded newer image for mysql:5.7.22 Creating app … Creating db … Creating webserver … error Creating app … done Creating db … done 3): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use

    ERROR: for webserver Cannot start service webserver: driver failed programming external connectivity on endpoint webserver (9c4f43736ef3d6b6f3159d7ce240e0cdf8ddf7c5655cf1438b6e3d4489e4da33): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use

    What should I do? Thanks

    Hello, fantastic article. Good explanations on each step.

    However, I have 2 questions i mind related to this setup.

    (1) I could not understand what the first step is doing.

    docker run --rm -v $(pwd):/app composer install

    What is the reason for “/app” after the colon? The container will be destroyed anyway after composer finishes installing Laravel. This “/app” directory can’t be used by any other container. Is it just a temp directory to get composer install Laravel inside the container and get it reflected in my local directory laravel_app? If so, will composer not update all config files inside the container assuming the directory name is “app”, but those config files will not work anymore as the local directory is “laravel_app”?

    (2) Since laravel got installed in local directory but not inside a container volume, how would I replicate this entire setup to another unix machine without following all these steps in that machine too?

    I am currently creating this setup in a VM that I have access temporarily. After the setup works, I want to replicate it to another Ubuntu machine that I will have more control over. Is there any way to bundle the entire setup, including persistent data and just reproduce in another Ubuntu machine?

    Will really appreciate if you can address the above (2) questions.

    Thank You so much it works for me. I have one question now I want to change my PHP version, I have added in Dockerfile and use these commands docker-compose down docker-compose up -d but still, PHP version is not updated someone please help me

    This is a great tutorial, I want to implement let’s encrypt SSL with this one can someone help me.

    Please help me here

    This error occurs. I did it on Ubuntu 18 and followed all the procedures. I can’t find a reason why the composer.lock file is missing.

    youngjin_cheon@instance-1:~/laravel-app$ docker-compose up -d
    Creating network "laravel-app_app-network" with driver "bridge"
    Creating volume "laravel-app_dbdata" with local driver
    Building app
    Step 1/16 : FROM php:7.2-fpm
    7.2-fpm: Pulling from library/php
    bf5952930446: Pull complete
    a409b57eb464: Pull complete
    3192e6c84ad0: Pull complete
    43553740162b: Pull complete
    f225612c7ad9: Pull complete
    0342ae2b9139: Pull complete
    bee69a16bd40: Pull complete
    3bb983246d5f: Pull complete
    3d974ad783a4: Pull complete
    dd6f02f36656: Pull complete
    1619d78ddff6: Pull complete
    Digest: sha256:7be1ff8d64f73141bca4ca44acd19fd83f2b26be024515befa725392d99f2565
    Status: Downloaded newer image for php:7.2-fpm
     ---> 02438bb242a1
    Step 2/16 : COPY composer.lock composer.json /var/www/
    ERROR: Service 'app' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder610168676/composer.lock: no such file or directory
    

    Thanks for the well written article. However, I have troubles in step 1. I can’t run composer like this because I am missing depenedecies like php-zip. I also got this error when I tried docker-compose up:

    ERROR: for webserver Cannot start service webserver: OCI runtime create failed: container_linux.go:349: starting container process caused “process_linux.go:449: container init caused "rootfs_linux.go:58: mounting \"/home/adam/www/docku/realworld/backoffice/docker-compose/nginx/conf.d\" to rootfs \"/var/lib/docker/overlay2/41076b397b8c0f5b8b9109fe67ab8927ae92d42a7eefbc5c9081a6ff1e55bdc3/merged\" at \"/var/lib/docker/overlay2/41076b397b8c0f5b8b9109fe67ab8927ae92d42a7eefbc5c9081a6ff1e55bdc3/merged/etc/nginx/conf.d\" caused \"not a directory\""”: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

    i keep getting this, trying to login as root in mysql:

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    
    

    @blait455 One possible cause for this is if you ran docker-compose up -d with a different root password specified. The dbdata volume sticks around even after the container and images are destroyed. You could try removing/pruning the containers and volumes.

    when trying to login to mysql i keep getting this

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    
    

    This comment has been deleted

      This comment has been deleted

        saya memiliki masalah pada bagian ini

        docker run –rm -v $(pwd):/app composer install
        
        Unable to find image ‘composer:latest’ locally
        latest: Pulling from library/composer
        df20fa9351a1: Pull complete
        b358d6dbbdff: Pull complete
        0232d962484c: Pull complete
        0c1d3ac04d2a: Pull complete
        6345b6a18b86: Pull complete
        938a92b586ef: Pull complete
        c0208c70cd8d: Pull complete
        c87c00ebf3da: Pull complete
        dfc8cf90500c: Pull complete
        4aefd0aa5ed3: Pull complete
        0fd3037f2b84: Pull complete
        4e5f1df9275f: Pull complete
        e044c287ec8a: Pull complete
        5a7665f561f7: Pull complete
        02264f19e479: Pull complete
        Digest: sha256:4f7e017201f7128fa6752ae0d868cb94270cb2f216b87192c2718d99dfb78704
        Status: Downloaded newer image for composer:latest
        Loading composer repositories with package information
        Updating dependencies (including require-dev)
        

        prosesnya berhenti pada bagian updating dependencies, untuk solusinya bagaimana ya?

        I have done with all steps but I am getting this error when I hit my IP address in the browser, Anyone please help me on same.

        https://prnt.sc/ugkfqy

        ========================== /var/www/ Illuminate\Encryption\MissingAppKeyException No application encryption key has been specified. Hide solutions Your app key is missing Generate your application encryption key using php artisan key:generate.

        READ MORE Laravel installation

        Hi there,

        It looks like that your app key was not generated. To do that you need to run the following command:

        1. docker-compose exec app php artisan key:generate

        Regards, Bobby

        Thanks for the reply, I have already run above command and again i have run I have got this error msg

        https://prnt.sc/ugwqv7

        Here is my DB connection string https://prnt.sc/ugwr7y

        I get nearly the same error: https://media.discordapp.net/attachments/755749265158176870/756532187825832047/unknown.png

        I think I also have a DB connection issue, I set location as db, not localhost or 127.0.0.1

        I think It’s working now I am able to connect with MySQL DB Now I want to configure SSL on it so I need to generate CSR file inside the docker container, or in my server.

        On Mac, if you have a symlink, “docker run … composer install” command will give “file exists” error. Use the actual directory path.

        For app container with some customizations:

        After copying the directories I am trying to install dependencies using composer

        RUN composer install
        

        It does show successful when building

        > @php artisan package:discover
        Discovered Package: aws/aws-sdk-php-laravel
        Discovered Package: barryvdh/laravel-cors
        Discovered Package: barryvdh/laravel-dompdf
        Discovered Package: fideloper/proxy
        Discovered Package: laravel/horizon
        Discovered Package: laravel/tinker
        Discovered Package: lesstif/php-jira-rest-client
        Discovered Package: nunomaduro/collision
        Discovered Package: pragmarx/google2fa-laravel
        Discovered Package: prettus/l5-repository
        Package manifest generated successfully.
        
        

        but libraries are not actually installed. No vendor folder is created. These are the permissions by the way:

        RUN chown -R www:www /var/www/project-folder
        RUN chmod -R 774 /var/www/project-folder
        

        Everything seems fine but it doesn’t create vendor folder. Any idea what I might be doing wrong?

        Hello, I am facing an error

        After running

        docker-compose exec app php artisan key:generate
        

        it shows

        Could not open input file: artisan
        

        I tried to solve issue, but i faced further problems like after running ``` composer install

        it shows:
        

        Loading composer repositories with package information Installing dependencies (including require-dev) from lock file Your requirements could not be resolved to an installable set of packages.

        Problem 1 - This package requires php ^7.3 but your PHP version (7.2.34) does not satisfy that requirement. Problem 2 - laravel/framework v8.9.0 requires php ^7.3 -> your PHP version (7.2.34) does not satisfy that requirement. - laravel/framework v8.9.0 requires php ^7.3 -> your PHP version (7.2.34) does not satisfy that requirement. - Installation request for laravel/framework v8.9.0 -> satisfiable by laravel/framework[v8.9.0].

        
        and running
        

        composer update --ignore-platform-reqs

        shows:
        

        Illuminate\Foundation\ComposerScripts::postAutoloadDump @php artisan package:discover --ansi

        In BusServiceProvider.php line 51:

        syntax error, unexpected ‘)’

        Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

        
        
        Please help and thank you so much for this great tutorial

        Running

        $ laravel-app]$ docker-compose up -d
        

        I get these errors …

        Failure resolving Reading package lists...
        W: Failed to fetch http://deb.debian.org/debian/dists/buster/InRelease  Temporary failure resolving 'deb.debian.org'
        W: Failed to fetch http://security.debian.org/debian-security/dists/buster/updates/InRelease  Temporary failure resolving 'security.debian.org'
        W: Failed to fetch http://deb.debian.org/debian/dists/buster-updates/InRelease  Temporary failure resolving 'deb.debian.org'
        W: Some index files failed to download. They have been ignored, or old ones used instead.
        Reading package lists...
        Building dependency tree...
        Reading state information...
        Package build-essential is not available, but is referred to by another package.
        This may mean that the package is missing, has been obsoleted, or
        is only available from another source
        
        Package locales is not available, but is referred to by another package.
        This may mean that the package is missing, has been obsoleted, or
        is only available from another source
        
        Package git is not available, but is referred to by another package.
        This may mean that the package is missing, has been obsoleted, or
        is only available from another source
        
        E: Package 'build-essential' has no installation candidate
        E: Unable to locate package libpng-dev
        E: Unable to locate package libjpeg62-turbo-dev
        E: Unable to locate package libfreetype6-dev
        E: Package 'locales' has no installation candidate
        E: Unable to locate package zip
        E: Unable to locate package jpegoptim
        E: Unable to locate package optipng
        E: Unable to locate package pngquant
        E: Unable to locate package gifsicle
        E: Unable to locate package vim
        E: Unable to locate package unzip
        E: Package 'git' has no installation candidate
        ERROR: Service 'app' failed to build: The command '/bin/sh -c apt-get update && apt-get install -y     build-essential     libpng-dev     libjpeg62-turbo-dev     libfreetype6-dev     locales     zip     jpegoptim optipng pngquant gifsicle     vim     unzip     git     curl' returned a non-zero code: 100
        [jaaf@fixe laravel-app]$
        

        This comment has been deleted

          When I execute this command:

          sudo docker-compose exec app php artisan migrate
          

          I GET THIS ERROR:

          SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')
          

          PS: I created laravel user and gave him all privileges on laravel.* @ ‘%’

          and my .env file contains this code:

          DB_CONNECTION=mysql
          DB_HOST=db
          DB_PORT=3306
          DB_DATABASE=laravel
          DB_USERNAME=laraveluser
          DB_PASSWORD=karboulx941A!
          

          Good article but it ultimately failed for me.

          • first, I had to change the Dockerfile to pull from php:7.2-fpm because:

          Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version “>= 7.3.0”. You are running 7.2.34. in /var/www/vendor/composer/platform_check.php on line 24

          Then, it failed for the same reason nvc11190 September 19, 2019 encountered:

          docker-compose exec app php artisan key:generate

          UnexpectedValueException : The stream or file “/var/www/storage/logs/laravel-2019-09-19.log” could not be opened: failed to open stream: Permission denied

          at /var/www/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:111

          I can’t fix this. :(

          Hello, I encounter a problem after following this tutorial, once my application is launched I have no access to any public folder file of laravel except the index.php file. So my application has no style file or Javascript file that works, would you have an idea? And if I try to access the file by changing the link in my navigation bar Laravel returns a 404 error.

          For Laravel 8 and php version 8 you can use given below Docker file code

          FROM php:8-fpm
          
          # Copy composer.lock and composer.json
          COPY composer.lock composer.json /var/www/
          
          # Set working directory
          WORKDIR /var/www
          
          ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
          
          RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
              install-php-extensions mbstring pdo_mysql zip exif pcntl gd
              
          
          #previous code
          # Install dependencies
          RUN apt-get update && apt-get install -y \
              build-essential \
              libpng-dev \
              libjpeg62-turbo-dev \
              libfreetype6-dev \
              locales \
              zip \
              jpegoptim optipng pngquant gifsicle \
              vim \
              unzip \
              git \
              curl
          
          
          # Clear cache
          RUN apt-get clean && rm -rf /var/lib/apt/lists/*
          
          # Install extensions
          #RUN docker-php-ext-install 
          #RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
          
          # Install composer
          RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          
          # Add user for laravel application
          RUN groupadd -g 1000 www
          RUN useradd -u 1000 -ms /bin/bash -g www www
          
          RUN php artisan key:generate
          
          # Copy existing application directory contents
          COPY . /var/www
          
          # Copy existing application directory permissions
          COPY --chown=www:www . /var/www
          
          # Change current user to www
          USER www
          
          # Expose port 9000 and start php-fpm server
          EXPOSE 9000
          CMD ["php-fpm"]
          

          how do i delete the container i just create

          I think the Dockerfile needs to be updated to use php >=8.0 Thank you for the tutorial!

          I did the tutorial until this line:

          docker-compose exec app php artisan key:generate

          First it brought me that Laravel needs version 8.0.2 of php, so I adjusted the files to use that. After that I just got this message: “Could not open input file: artisan”

          I use Docker Desktop under Windows. Might be something with permissions? Do you have any idea how I can fix that to go on with the tutorial?

          FROM php:8-fpm
          

          I am unable to enable pdo_mysql extension. With phpinfos() there is always and only pdo_sqlite.

          RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
              install-php-extensions mbstring pdo_mysql zip exif pcntl gd
          

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Become a contributor for community

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          DigitalOcean Documentation

          Full documentation for every DigitalOcean product.

          Resources for startups and SMBs

          The Wave has everything you need to know about building a business, from raising funding to marketing your product.

          Get our newsletter

          Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

          New accounts only. By submitting your email you agree to our Privacy Policy

          The developer cloud

          Scale up as you grow — whether you're running one virtual machine or ten thousand.

          Get started for free

          Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

          *This promotional offer applies to new accounts only.