-1

I'm using docker for my projects, mostly Laravel ones, which has the configurement of building a MySQL container and an app container.

The containers respectively is named "db" and "app".

enter image description here

The .env file is therefore configured after the hostname "db" in order to have the served webserver properly initialised

enter image description here

This enforces the developer to be inside the container in order to run appropiate commands that has anything to do with the database connection, otherwise giving error as the environment believes we are talking about a mysql instance at the computer. (right?)

Therefore, a script in the project root directory is made in order to ease this.

enter image description here

./cmd.sh artisan migrate (Works)

php artisan migrate (Does not) - unknown database

This concludes to the problem of mine, which I'm here for.

As you might see, this is oddly specific to MacOS environment and that would be true. Having the need of this script to work cross-os, meaning painlessly for Linux and Windows developers as well, is somewhat of a struggle of mine.

So, the issue in TLDR: How can I arrange this method of ease to work for all OS?

Using the current way has these issues:

Linux:

  • 1000 is the default user, not 33. Simply changing 33 to 1000 kind of fixes it
  • changing to 1000 makes it not working for mac though

Windows:

  • Needs powershell for bash, otherwise (cannot find the tty input device)
  • Powershell cant read the sh syntax

I kind of want to try staying away from using multiplie files, one for each os, as solution.

So, what am I missing here, is this even doable and where could I read about making this doable?

Thank you so much in advance and so sorry about the wall of text.


TLDR: For docker projects, how can i make a script that can run the container command for all Operative systems?

Community
  • 1
  • 1
PatricNox
  • 3,306
  • 1
  • 17
  • 25
  • 1
    use `#!/bin/bash` and test the OSTYPE environment variable. https://stackoverflow.com/questions/394230/how-to-detect-the-os-from-a-bash-script – Richard Barber May 31 '20 at 11:00
  • 1
    @RichardBarber thanks! that is definitively a step on the way. However, cygwin inteprets the EOL sign differently which ultimately ends to a file just for cygwin. Fixing the EOL character makes it invalid for Mac/Linux users. I couldn't find a way around this, any suggestions? – PatricNox Jun 01 '20 at 06:24
  • Are you using Ascii? – Richard Barber Jun 01 '20 at 07:04
  • 1
    @RichardBarber I managed to come around it using bash instead of cygwin, thanks alot! – PatricNox Jun 01 '20 at 07:21
  • Please don't post images of code / console output. As well as making it hard for people to copy & paste (to duplicate your problem), it renders screen-readers useless. – SiHa Jun 01 '20 at 07:24

1 Answers1

0

Using inspo from How to detect the OS from a Bash script?, (thanks Richard Barber for link), I managed to come around my problem using the OSTYPE variable to diff which OS that's in use.

For windows, simply use a bash terminal and the settings for either Linux or MacOS would work.

#!/bin/bash

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  USER="1000"
else
  USER="33"
fi

artisan() {
    docker-compose exec --user $USER app php artisan "$@"
}

"$@"
PatricNox
  • 3,306
  • 1
  • 17
  • 25