DEV Community

docker-compose up your entire Laravel + Apache + MySQL development environment.

V on April 04, 2019

Contents Contents Intro The set-up Webserver image for Laravel docker-compose.yml Up and Running Helper scripts (optional) container...
Collapse
 
memel06 profile image
Memel06 • Edited

Awesome article, thanks for it!

I thought that integrating also phpmyadmin would be very useful, so I added to my docker-compose.yaml this little snippet

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mysql-db
    environment:
      PMA_HOST: mysql-db
      PMA_PORT: 3306
    ports:
      - '8080:80'
    networks:
      backend:
        aliases:
          - phpmyadmin
Enter fullscreen mode Exit fullscreen mode

That's it, now the useful phpmyadmin buddy joins the squad :)

This can be improved adding PMA_HOST and PMA_PORT to the .env file for complete customization

Collapse
 
samuraipetrus profile image
Petrus Nogueira

In order to help other users, i’ll share here some troubles i was facing while running docker-compose build.

  1. No package oniguruma found

How to solve: Add libonig-dev package to apt-get's installation

  1. No package libzip found

How to solve: Add libzip-dev package to apt-get's installation

Hope it was helpful to someone! 😄

Collapse
 
manishsharmait52 profile image
manishsharmait52 • Edited

can you please provide me docker file and docker compose related full source code download link ? because i tried last 2 days but not getting success with docker file and docker compose to setup any php project.

Collapse
 
veevidify profile image
V

Are you setting up a new Laravel project or trying to create docker environment for existing one?

If you are setting up new Laravel project, you can refer to my repo:
github.com/veevidify/laravel-apach...

and then do:

docker-compose build
docker-compose up -d
docker-compose logs -f
Collapse
 
tenerecodes profile image
tenerecodes

HI there, i am setting an new project with the latest laravel repo + your dockerfile and docker-compose.yml i get this error:

Step 13/14 : RUN useradd -G www-data,root -u $uid -d /home/devuser devuser
---> Running in e24b11b14717
useradd: invalid user ID '-d'
ERROR: Service 'laravel-app' failed to build: The command '/bin/sh -c useradd -G www-data,root -u $uid -d /home/devuser devuser' returned a non-zero code: 3

any solution ?

Thread Thread
 
veevidify profile image
V • Edited

If you refer back to docker-compose.yml, this part:

...
      args:
        uid: ${UID}

docker-compose requires UID environment variable to be set. Check your .env file, make sure

UID=1000 # or whatever your host's user id is

If you're unsure about host user id, type id in your terminal to double check.

Thread Thread
 
tenerecodes profile image
tenerecodes

That worked fine thank you. but i have another question, if you permit, regarding your Dockerfile. how to copy npm from nodejs docker image like you did with composer.

the reason i ask this is because when i run "php artisan ui vue --auth" i get this message:

Vue scaffolding installed successfully.
Please run "npm install && npm run dev" to compile your fresh scaffolding.
Authentication scaffolding generated successfully.

Thread Thread
 
veevidify profile image
V • Edited

There is a neat little trick to run a docker container as if it's a binary in your host. This is very useful for things like composer or npm which only modifies files statically. Example:

$ cd /your/project/folder
$ docker run -it -u 1000:1000 -v "$PWD":/usr/src/app -w /usr/src/app --rm node:10 npm install

Make sure all the parameters are what you need, e.g. uid, node version, etc.

Even though I copy composer binary into the my own app image for "encapsulation", if we intend to only ever use such binary for static files modification on the host, that wouldn't be necessary. Instead this trick makes it more clean once you start having 5 6 container runtime for these sorts of purposes.

I conveniently "missed out" this part due to how varied people's use cases with node are.

Collapse
 
manishsharmait52 profile image
manishsharmait52

Thank you so much to reply me , i will check it and let you know if i am facing another issue .

Thank you again ..... !

Thread Thread
 
manishsharmait52 profile image
manishsharmait52

Hello Sir, I facing 500 server error issue , can you please give me some tips. what happens over there ?

Collapse
 
marcuswii profile image
marcuswii • Edited

Trying to learn laravel and docker.
I cloned your repo and ran
docker-compose build
docker-compose up -d
docker-compose logs -f

docker ps gives me two containers. If I type localhost:8000 I see that the webserver is running but I get a Forbidden
You don't have permission to access this resource.
Apache/2.4.38 (Debian) Server at localhost Port 8000
error.
The log file gives : [authz_core:error] [pid 16] [client 172.19.0.1:43254] AH01630: client denied by server configuration: /var/www/html/public
and also
AH00112: Warning: DocumentRoot [/var/www/html/public] does not exist

I.m running on Windows10, under WSL.

Can you help this one?

Marcus

Collapse
 
firecentaur profile image
Paul Preibisch • Edited

By the way, if doing this on your local machine, make sure you
change docker-composer.yml to use bind - otherwise when you run the command .container, no files will be listed inside!

volumes:
#- ./:/var/www/html

- type: bind
source: ./
target: /var/www/html

Collapse
 
taviroquai profile image
Marco Afonso

Thanks for this writting :)

I'm setting up a complete automated deployment of a laravel app using docker and I found that Laravel uses .env files instead of docker-compose.yml enviroment section.

Is there a way to make laravel to use docker-compose.yml environment section?

Thanks!

Collapse
 
veevidify profile image
V • Edited

Right, so what .env essentially does is that it emulates Laravel reading the actual runtime environment variables. In other words Laravel treats .env variables as if they're your actual host or build context's environment variables. Conveniently for us, docker-compose is also able to read from .env.

So to put it clearly, both Laravel and docker read these variables in a build context, which are mocked by .env file for development purposes.

In your deployment / build context, you would want to export these variables too. To name a few, Gitlab CI allows configuring them under Settings > CI/CD. Circle CI does also, under Build Settings > Environment Variables. If you only deploy from local (instead of remote CI), simply run deployment from within a container built with .env variables exported.

Collapse
 
dhodyrahmad profile image
Dhody Rahmad Hidayat

Hello sir, there's any different configuration between setting up a new Laravel project or trying to create docker environment for existing one?

Collapse
 
veevidify profile image
V • Edited

The difference lies in post containers startup.

So for existing project, we will create all the necessary docker files, such as Dockerfile, docker-compose, place all the configs in place.

Since we're volume mounting the app, depends on what your current setup is missing. If you havent run composer, and there is no vendor folder, you can go ahead and use the container to run composer install. If there is no app key, or passport keys in your setup, you will run respective artisan command to setup.

Similarly, since this setup aims at a new database container, you will start with an empty database (we mount it under var/ inside the project folder). If you have any migrations and seeders, you will need to run those to get a database setup. Alternatively if you have developed a dev database without those migrations and seeders, you can initiate and fill the database with a sql file mounted under run/dump/init.sql as I explained in the post.

Collapse
 
dhodyrahmad profile image
Dhody Rahmad Hidayat

Thank you for your response, I'm newbie in this scope. I have a Laravel application also have migrations and seeders for this application. I've done create necessary docker files and place the configs in place. But there's some problem at migration:

Access denied for user 'root'@'172.19.0.2' (using password: YES)

did I missed something?

Thread Thread
 
veevidify profile image
V

that probably mean your database username and password are not set to what the docker container is built with.

Make sure your environment variables for the mysql service (specified in docker-compose.yml) are consistent with the process you are using to access the database (whatever you are trying that yields that error).

Collapse
 
akonibrahim profile image
Akonibrahim

Thank you for this amazing post.
I'm trying to dockerize an existing laravel application using your post. But my static files are returning 404.
stackoverflow.com/questions/626231...

Collapse
 
kevintimothyift profile image
kevintimothyift

can you solve this error on composer install?

devuser@b37b652e508b:/var/www/html$ composer install
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Package operations: 101 installs, 0 updates, 0 removals

[RuntimeException]

/var/www/html/vendor does not exist and could not be created.

Collapse
 
nickstock profile image
nickstock

Great post, I found this very easy to set up and work's like a charm, much quicker than setting up on a vm.

Collapse
 
edwinblack profile image
edwinblack

when execute the code php artisan migrate print a error

Database name seems incorrect: You're using the default database name laravel. This database does not exist.

Collapse
 
manishsharmait52 profile image
manishsharmait52

Hello Sir, I facing 500 server error issue , can you please give me some tips. what happens over there ?

Collapse
 
veevidify profile image
V

The fact that you get a response from the webserver means the docker environment works.

If you get require_once ... vendor/autoload error, then you'd probably missing composer dependencies.

docker exec -it laravel-app bash -c "sudo -u devuser /bin/bash"

gets you inside the container.

devuser@0e382dff6a23:/var/www/html$

then

composer install
php artisan key:generate
php artisan migrate
php artisan config:cache
Collapse
 
manishsharmait52 profile image
manishsharmait52

okay i will check again .

Thank you .

Collapse
 
mrigesh901 profile image
Mrigesh patni

RUN composer install:

0 0.554 Composer plugins have been disabled for safety in this non-interactive session. Set COMPOSER_ALLOW_SUPERUSER=1 if you want to allow plugins to run as root/super user.

0 0.554 Do not run Composer as root/super user! See getcomposer.org/root for details

0 0.559 Composer could not find a composer.json file in /var/www/html

0 0.559 To initialize a project, please create a composer.json file. See getcomposer.org/basic-usage


failed to solve: executor failed running [/bin/sh -c composer install]: exit code: 1
PS C:\xampp\htdocs\portal>

Collapse
 
yaby88 profile image
yaby

git clone git@git.repo.url/laravel-project

fatal: repository 'git@git.repo.url/laravel-project' does not exist

why repository is not exist ????

Collapse
 
vicf profile image
Victor

This is just an example of how you set up YOUR OWN project, so "laravel-project" is just a fake example name. You can read it as :

git clone git@git.repo.url/MY-OWN-PROJECT-CREATED-AFTER-READING-THIS-ARTICLE

Collapse
 
firecentaur profile image
Paul Preibisch

Hi there, the containers are running, but it seems that the volume mapping never worked. When I look in the container, no files are listed except the run folder.

Collapse
 
rant989 profile image
rant989

Awesome, thanks for it!
It helped me a lot

If I want to add SSL what is the way to do this?

Collapse
 
davidramos010 profile image
David Ramos

I have this error when I login: GET localhost:8000/js/app.js net::ERR_ABORTED 404 (Not Found).

I need help, I could not solve it.

Collapse
 
atnaize profile image
Atnaize • Edited

Hello,

Is it possible to run the composer update and migrations directly from the Dockerfile?

I tried to

WORKDIR /var/www/html
RUN composer install

But this is not working

Collapse
 
veevidify profile image
V • Edited

Hi,
The setup I had on the repo use volume mount for the source, so that any changes, including composer install (vendor folder) persist on the host, for ease of development.

If you simply want a running app with vendor packages inside the container, you need to add composer.json into the container during build stage. Refer to this part of the article:

...you'll need to add the source files into the container pre-build...

COPY . /var/www/html
RUN cd /var/www/html && composer install && php artisan key:generate

Note that by doing this, your host pc won't have the vendor packages persisted.

Collapse
 
tracydavis profile image
Tracy • Edited

My laravel application is working on the localhost. But while running through localhost:port , I get error of 403.
This is the error:

Forbidden
You don't have permission to access this resource.

Collapse
 
ahmedsliman profile image
Ahmed sliman

Hello,

Amazing Tut. thanks,

I didn't get the last point regarding Helpers scripts, Where can I write the bash script like this

docker exec -it laravel-app bash -c "sudo -u devuser /bin/bash"

Collapse
 
veevidify profile image
V • Edited

Those are quite literally bash scripts (if your development is linux based) which wraps around your host's docker binary, to run commands inside a running container.

You can have these scripts anywhere. I included them within the project folder itself to share with others working on the repo. Checkout the "TL;DR" section, and my repo on github: github.com/veevidify/laravel-apach...