2

Before going any further, I've been looking everywhere on how to run laravel sail's project (including MySQL, Redis, etc) properly after cloning a repository without any local environments. I have read some another questions and explanations, but still didn't have any completed/proper answers.

I have tried to create a new fresh laravel project by using sail, then upload to git, and clone it again to my local machine with using different folder, then tried all of above links.

  • e.g For MySQL, I have tried to add php artisan migrate or run sail artisan migrate and it showed connection refused.
  • I have tried to build first before run sail up
  • I have tried to copy env example file

Until now, I only can run the sail (I can access the webpage) but not the databases and so on.

Thank you.

torek
  • 448,244
  • 59
  • 642
  • 775
zaidysf
  • 492
  • 2
  • 14
  • Have you checked to make sure you are creating the .env file? These are often left out of git repositories. – Second2None Mar 01 '22 at 06:40
  • Yes, I am sorry to not included in my question. I have tried to copy the .env file. – zaidysf Mar 01 '22 at 06:49
  • Make sure you are running `php artisan optimize:clear` then try running your migrations. – Second2None Mar 01 '22 at 06:51
  • I just tried it but not works, i think the problems are on docker containers, maybe i am missing some steps to make it works as expected. – zaidysf Mar 01 '22 at 06:53
  • Sounds like your .env has the wrong credentials for your database. DB_HOST should not be localhost – online Thomas Mar 01 '22 at 09:35
  • Yes, i have tried to copy the previous working .env file, and still not works – zaidysf Mar 01 '22 at 12:22
  • To keep it simple, laravel forge is the best option. Much easier to maintain and deploy. – Ersin Demirtas Mar 10 '22 at 13:17
  • Do you mean for local development? If yes, what did you try to do after cloning the project? Does the project already has sail installed? Are there any errors in the output? Add more details please... – DGF Mar 10 '22 at 13:20
  • Yes, for local development. I have posted my tries and errors (for MySQL), you can try to reproduce that, and for the errors – zaidysf Mar 10 '22 at 14:56
  • @ErsinDemirtas first, forge is not free. second, this is for local development (for other team members). – zaidysf Mar 10 '22 at 14:57
  • If this is for your local you have the devilbox (http://devilbox.org/) option. This is what I use to keep it consistent with other developer environments. – Ersin Demirtas Mar 10 '22 at 17:00
  • Yes sir, just like i posted above, `Tell me how you make that project (with all environments) run as expected without any local environments installed outside docker?`, because that's one the purpose of using docker, right? but thank you for your suggestions, pretty much appreciated. – zaidysf Mar 10 '22 at 19:01
  • You are not giving enough information, edit the question and add more stuff... What *exacly* did you do after cloning the project? Did you change your `.env` file? What does you `docker-compose.yml` file look like? Without some info from `.env` and `docker-compose.yml` it's hard to guess. Most importantly the `docker-compose.yml`. I can't reproduce something without info... – DGF Mar 10 '22 at 20:08
  • Yes, basically i just want to run the sail easily like how i created at the first. And it's easy to reproduce because i was not edit any files of that laravel default fresh installation. Just download sail's project, push it, clone it again to different directory and i have no idea how to run the other stacks (like MySQL). – zaidysf Mar 11 '22 at 06:37

2 Answers2

2

I have tried to create a new fresh laravel project by using sail, then upload to git, and clone it again to my local machine with using different folder, then tried all of above links.

Reproducing the steps based on the information you provided this is what I did:

Note: I will assume you already have docker installed.


curl -s https://laravel.build/test-app | bash


  • Go to project and sail up (wait until the output stops) just to test it out, you can sail down after:
cd test-app
 
./vendor/bin/sail up

./vendor/bin/sail down

  • Create a git repo in the project and push it to your host (I will assume you have git configured):
git init

git add .

git commit -m "Test app"

git remote add origin https://github.com/<your-username>/teste-app.git

git push --set-upstream origin master

  • Clone the repo to different folder:
git clone https://github.com/<your-username>/teste-app.git download_test-app

docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v $(pwd):/var/www/html \
    -w /var/www/html \
    laravelsail/php81-composer:latest \
    composer install --ignore-platform-reqs

  • Check with docker ps if there is any container running (running container can cause issues if you don't change Ports). If any container is running stop them with docker container stop <name of container OR ID of container>

  • Copy .env.example to .env. I changed where it has 127.0.0.1 to localhost. In the DB_HOST change the value to mysql

  • Run ./vendor/bin/sail/up and the app will start. Open the app on the browser (localhost:80 if you didn't change ports).

  • Generate the app key either by clicking in the "Generate Key" button when you open your app in the browser or by running ./vendor/bin/sail artisan key:generate

  • Run migrations to test if database works ./vendor/bin/sail artisan migrate

And you are good to go. Hope that it helps!

DGF
  • 330
  • 1
  • 4
  • 11
  • Hi, thank you so much for your answer, did you able to access the mysql via mysql client? if yes, how's the credentials? – zaidysf Mar 11 '22 at 15:34
  • I didn't try, but if the migration works it's because the database works. The credentials are set in the `.env` file in the `DB` section, you just have to use the same credentials to use the mysql client – DGF Mar 11 '22 at 16:34
0

Sometimes I have issues with existing volumes, those volumes exist but already have everything build. So even if i add a new project it still uses the incorrect volume.

please read below it explains it a lot better. Laravel Sail rebuild default database

So what I normally do is use a proxy server on my local dev. so I can use different projects and can let them communicate together.

Edit:: the solution i used for local development https://blog.devgenius.io/multi-laravel-sail-sites-with-custom-domain-e13c07d9dd0c

Marcel Santing
  • 103
  • 1
  • 5
  • Thank you for your answer in advance, i also have experiencing that existing volume for databases issues. but in this case, my laravel sail's project should be not works at the first time i create a new project (with another volumes of sail dbs are exist). – zaidysf Mar 01 '22 at 12:31
  • "my laravel sail's project should be not works at the first time i create a new project (with another volumes of sail dbs are exist)" <- i have troubles reading this sentence. I am not quite sure what you are trying to ask. – Marcel Santing Mar 01 '22 at 15:24
  • Yes sir, my laravel sail project is worked at the first time i install it. but when i push it to git and clone it again to different folder, i cannot run all of environment required properly – zaidysf Mar 01 '22 at 17:22