How to dockerize vue3 laravel9 application? - laravel

I am developping a vue3 laravel9 SPA.
The app is presently running in dev and in production on a web hosting.
I would like to distribute it without the need of a web hosting as a dockerized application but I do not know docker that much.
I base my approach on this example : https://github.com/AveryHowell/docker-laravel-vue
I just copied from the example to my app
laravel/nginx (folder)
laravel/docker-compose.yml (file)
laravel/laravel.dockerfile (file)
vue/nginx.conf (file)
vue/vue.dockerfile (file)
The changes I made to this example are very limited.
First I changed the version of mysql in laravel/docker-compose.yml to mariadb:10.3.37
#Mysql DB
mysql:
image: mariadb:10.3.37
container_name: db
restart: unless-stopped
tty: true
ports:
- "33061:3306"
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: laravel
volumes:
- ../mysql:/mysql
networks:
- app-network
This version is the one I presently use and avoid some trouble with default values in json columns.
Secondly I changed the database config and some urls in .env
APP_NAME='my application'
APP_ENV=local
APP_KEY=base64:Wh2GGnDZ6YLEjNghheIX4qL+6P9lHuS8sO03L/pCufA=
APP_DEBUG=true
APP_URL=http://localhost
APP_API_URL=http://localhost/api/
APP_API_DEV_URL=http://localhost:8080/
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=secret
DB_ROOT_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_DOMAIN=.localhost
SANCTUM_STATEFUL_DOMAINS=localhost:8080
SPA_URL=http://localhost:8080
After that I used these commands
docker-compose up -d
docker-compose exec laravel composer update
docker-compose exec laravel composer install
docker-compose exec laravel composer dump-autoload
docker-compose exec laravel php artisan key:generate
docker-compose exec laravel php artisan config:cache
docker-compose exec laravel php artisan migrate:fresh
I had the tables created in mariadb
but when I visited localhost:8080 as said in the example, I got this message
The connection was reset
The connection to the server was reset while the page was loading.
The site could be temporarily unavailable or too busy. Try again in a few moments.
If you are unable to load any pages, check your computer’s network connection.
If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the web.
What is wrong?
EDIT AFTER FIRST COMMENT
[jaaf#localhost laravel]$ docker container ls --filter label=com.docker.compose.project
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5719d4b36179 laravel-webserver "/docker-entrypoint.…" 44 minutes ago Up 44 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp webserver
9b88d442636c laravel-vue "docker-entrypoint.s…" 44 minutes ago Restarting (1) 51 seconds ago vue
37743ac9307f laravel "docker-php-entrypoi…" 44 minutes ago Up 44 minutes 9000/tcp laravel
1cf764c56ad0 mariadb:10.3.37 "docker-entrypoint.s…" 44 minutes ago Up 44 minutes 0.0.0.0:33061->3306/tcp, :::33061->3306/tcp db
[jaaf#localhost laravel]$

Related

Unable to connect to a running Laravel Sail Docker project with TablePlus (role does not exist)

I created a new Laravel project and installed Sail with composer require laravel/sail --dev followed by php artisan sail:install and sail up to get the project up and running in Docker.
By doing these actions my .env file changed from
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_docker
DB_USERNAME=root
DB_PASSWORD=
to
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=laravel_docker
DB_USERNAME=sail
DB_PASSWORD=password
And now I got two running Docker containers:
laravel-docker_laravel.test_1
laravel-docker_pgsql_1
I'm able to run the basic user migration with sail artisan migrate.
Next up I want to connect Tableplus (or Postico) with my Postgresql database running in Docker. Therefore I filled in the following information:
When trying to connect I got ERROR FATAL: role "sail" does not exist.
Can someone help me out?
Edit 1: Adding a screenshot from some terminal commands. I can connect to the database in the docker container, see a list of the tables and get a table with all the rows from the users table (inserted with a seeder using Laravel Sail)
Edit 2: docker-compose ps
Thanks to this post I found a working solution.
Changing to a different port in the docker-compose.yml file fixed the issue.
Before:
pgsql:
image: 'postgres:13'
ports:
- '${FORWARD_DB_PORT:-5432}:5432'
After:
pgsql:
image: 'postgres:13'
ports:
- '${FORWARD_DB_PORT:-5632}:5432'
After changing the port number to 5632 in TablePlus I'm able to connect to the database.
I have the same problem "role "sail" does not exist". In this case we need to use command ./vendor/bin/sail down -v to delete the existing Docker volume data. After that you can do ./vendor/bin/sail up command.

Laravel Sail SQL Connection timed out

I just started using Laravel Sail, but Laravel can't connect to the MySQL server. Even Artisan in the container gives Connection timed out. I can connect from MySQL Workbench on my host machine.
My .env file has been updated for use with sail:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Laravel sail does not come with phpMyAdmin to visualize your database. so, you need to go with one of these:
add phpMyAdmin to your docker-compose.yml file manually.
services:
...
phpmyadmin:
image: phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- 8080:80
environment:
PMA_HOST: mysql
MYSQL_USERNAME: "${DB_USERNAME}"
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
...
volumes:
...
phpmyadmin:
driver: local
restart your project again.
sail up -d
sail now will download phpMyAdmin image to your docker container where you
will able to view database on port:8080 [http://localhost:8080/].
use a graphical database management application like TablePlus as mentioned in sail documentation.
I recently ran across this issue too, when upgrading an old Laravel project from using homestead to sail. As per the docs you need to set DB_HOST=mysql in your .env file so that your application container can communicate with the MySQL container.
You must write in "DB_HOST" are domain or ip
DB_HOST=1.2.3.4
or
DB_HOST=example.com

Name or service not known [tcp://redis:6379] change REDIS_HOST value still does'nt work

Need a help! My code was run on Docker and get problem with Redis.
I've checked on server the Redis is running.
I also try change in .env file REDIS_HOST=redis --> REDIS_HOST=localhost and REDIS_HOST=127.0.0.1 but does'nt work anymore!
REDIS_HOST=redis
php_network_getaddresses: getaddrinfo failed: Name or service not known [tcp://redis:6379]
REDIS_HOST=127.0.0.1 or REDIS_HOST=localhost
Connection refused [tcp://127.0.0.1:6379]
.env file
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=redis
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
docker ps | grep redis
365ba9ede8e0 redis "docker-entrypoint.s…" 7 hours ago Up 7 hours 0.0.0.0:6379->6379/tcp redis
3e94135b4c83 redis:alpine "docker-entrypoint.s…" 8 hours ago Up 5 hours 6379/tcp ms-worker_redis_1
69d8708931e6 redis:alpine "docker-entrypoint.s…" 8 hours ago Up 8 hours 6379/tcp ma-worker-redis-service
810c182d403d redis:alpine "docker-entrypoint.s…" 8 hours ago Up 8 hours 6379/tcp ma-redis-service
had the same problem. It turned out that it was necessary to add radish to the application network
redis:
image: redis
restart: unless-stopped
ports:
- '6379:6379'
command: redis-server --save 20 1 --loglevel warning --requirepass ${REDIS_KEY}
volumes:
- redis:/data
networks:
- web

php artisan migrate on Azure (in BitBucket pipeline)

I have setup a pipeline in BitBucket to automatically deploy my master branch of my project to an Azure Web App instance.
The app deploys the files and runs composer update as expected (although it does warn that it's running as root), but php artisan migrate --force returns:
Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access
denied for user 'forge'#'127.0.0.1' (using password: NO) (SQL: select
* from information_schema.tables where table_schema = forge and table_name = migrations)
I have already created the .env file, and when I run php artisan migrate from within a shell it runs successfully and the tables are created.
Being that 'forge' is the default user in database.php I figure .env isn't being loaded when the command is fired from the deploy script.
Is there something obvious I've missed to cause this issue, or should I somehow set it up to not run as root?
I could replace the database details in database.php but I feel that's the wrong thing to do.
edit
.env contents (with certain data replaced with ********):
APP_NAME=Laravel
APP_ENV=local
APP_KEY=********
APP_DEBUG=true
APP_URL=********
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=********
DB_DATABASE=********
DB_USERNAME=********
DB_PASSWORD=********
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
edit 2
I realise I'm yet to publish my bitbucket-pipelines.yml file:
image: php:7.2-fpm
pipelines:
branches:
master:
- step:
script:
- apt-get update && apt-get install -qy git curl libmcrypt-dev mysql-client && apt-get install -qy unzip git
- yes | pecl install mcrypt-1.0.1
- docker-php-ext-install pdo_mysql
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer update
- php artisan migrate --force
- php artisan serve --port=80 &
- sleep 5
- curl -vk http://localhost:80
deployment: staging
services:
- mysql
definitions:
services:
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: '******'
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
MYSQL_USER: '******'
MYSQL_PASSWORD: '******'
MYSQL_PORT: '******'
I also have a .env.pipelines file:
APP_ENV=local
APP_KEY=******
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=******
DB_USERNAME=******
DB_PASSWORD=******
This error basically comes from the after changes in the .env file:
Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access
denied for user 'forge'#'127.0.0.1' (using password: NO) (SQL: select
* from information_schema.tables where table_schema = forge and table_name = migrations)
Whenever we change the DB_DATABASE, DB_USERNAME and DB_PASSWORD in .env file, we need to clear the cache.
After completion of .env edit, must be clear cache: php artisan config:cache
NOTE: If no password is set on the database, clear it DB_PASSWORD, empty space must also be removed(In the past I've also faceout this problem, It's consider blank space as a password)
Without seeing your deploy script and how you are connecting with your Azure server you would need to put
php artisan config:clear // This will reload the .env file to cache
after you have connected to your server but before you run
php artisan migrate
Please checkout the link:
https://laravel.com/docs/5.7/configuration#configuration-caching
php artisan config:cache
The above command will just regenerate the cache for you. (if added as a part of deployment script)
Else you can use php artisan config:clear just to clear the existing config and fetch values from .env/config files (add as a part of your deployment script)

SQLSTATE[HY000] [2002] Connection refused Laravel 5.6 and Laradock

I have a problem with my database mysql. I can not access it, I have a laravel project which I added Laradock to be able to use the containers with Docker. I launched two containers typing docker-compose up -d mysql nginx in my laradock directory. Once the containers are launched, I can access Laravel's home page, but when I want to communicate with my database I have the following error:
SQLSTATE [HY000] [2002] Connection refused.
Someone has an idea of ​​the source of my problem. Thank you .
i am on ubuntu 17.10
Docker version 17.12.0-ce, build c97c6d6
my .env file in project is here
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=db_authors
DB_USERNAME=root
DB_PASSWORD=root
.env file on laradock folder is
MYSQL_VERSION=8.0
MYSQL_DATABASE=db_authors
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d
and here is mysql container in my docker-compose.yml
mysql:
build:
context: ./mysql
args:
- MYSQL_VERSION=${MYSQL_VERSION}
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- TZ=${WORKSPACE_TIMEZONE}
volumes:
- ${DATA_SAVE_PATH}/mysql:/var/lib/mysql
- ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
ports:
- "${MYSQL_PORT}:3306"
networks:
- backend
my containers are well launched here is the result I get by doing
docker-compose up -d mysql nginx
Creating network "laradock_default" with the default driver
Creating laradock_mysql_1 ... done
Creating network "laradock_backend" with driver "bridge"
Creating laradock_workspace_1 ... done
Creating laradock_applications_1 ...
Creating laradock_php-fpm_1 ... done
Creating laradock_php-fpm_1 ...
Creating laradock_nginx_1 ... do
try the following:
1 - Enter the MySQL container: docker-compose exec mysql bash.
2 - Enter mysql: mysql -uroot -proot
3 - See all users: SELECT User FROM mysql.user;
4 - Run any commands show databases, show tables, select * from......
Does it work? Can you log on MySQL like that?

Resources