1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ShellScriptに入門する(4・5・6日目)

1
Last updated at Posted at 2019-08-14

今日もやっていきましょう

ShellScriptに入門する(1日目)
ShellScriptに入門する(2日目)
ShellScriptに入門する(3日目)
ShellScriptに入門する(4・5・6日目)

はじめに

Laravelのdocker環境を対話形式で構築できるようなスクリプトをShellScriptでつくりました

指定できるもの

  • DBの種類(MySQL/PostgreSQL)
  • DBのバージョン
  • DB名・user・password・DBのポート番号
  • webサーバ(Apache/Nginx)
  • webサーバのポート番号
  • PHPのバージョン
  • Laravelのバージョン

流れ

  1. 対話形式で上記設定をdocker-compose.yml, Dockerfileや.envに反映させていく
  2. docker-composeコマンドでコンテナを立ち上げる
  3. コンテナ内でLaravelのプロジェクトを作成する
  4. プロジェクト内の.envを書き換える(主にDB関係)

事前準備

こちらのスクリプトを動かすには以下のコマンドをインストールして使えるようにしておく必要があります

  • docker
  • docker-compose
  • jq

作成したファイル

こちらに置いています
gen_laravel_on_docker

デモンストレーション

% chmod 755 ./gen_laravel_on_docker.sh; . ./gen_laravel_on_docker.sh
Please select MySQL or PostgreSQL mysql/postgres [ mysql ]
postgres
Please input PostgreSQL version [ 10 ]
10
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1153    0  1153    0     0    752      0 --:--:--  0:00:01 --:--:--   752
Please input DB name you want to set
psql-test
Please input DB user you want to set
postgres
Please input DB password you want to set
password
Please input DB port you want to set
5432
Please select Apache or Nginx apache/nginx [ apache ]
nginx
Please input PHP version [ 7.3 ]

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1032    0  1032    0     0    707      0 --:--:--  0:00:01 --:--:--   707
Please input WebServer port you want to set
8080

Creating network "gen_laravel_on_docker_default" with the default driver
Building db
Step 1/3 : FROM postgres:10
 ---> 092b8d6e3163
略...
Creating gen_laravel_on_docker_app_1 ... done
Creating gen_laravel_on_docker_db_1  ... done
Creating gen_laravel_on_docker_web_1 ... done


Please input Laravel version you want
5.8.*
Installing laravel/laravel (v5.8.17)
  - Installing laravel/laravel (v5.8.17): Downloading (100%)         
Created project in /work
略...
Package manifest generated successfully.
Application key set successfully.
Complete deploy Laravel on Docker!
Access to http://localhost:8080

使用シェルがbashの場合は下記のように最初にfrom_zsh_to_bash.shを実行すればOK

$ chmod 755 from_zsh_to_bash.sh; . ./from_zsh_to_bash.sh
$ chmod 755 ./gen_laravel_on_docker.sh; . ./gen_laravel_on_docker.sh
以下同様...

改善できそうなこと

  • 一旦環境構築が終われば*.shや*.libは必要なくなるので、処理の最後で削除したほうがいい?
  • 便宜上DB(MySQL/PostgreSQL)とPHP(Apache用/Nginx用)のDockerfileはすべて用意しているため、使用しないDockerfileがでてくる
    使用しないDockerfileは削除したほうがよさそう

さいごに

会社の開発環境構築自動化のための練習としてLaravel環境構築対話スクリプトを作成してみた
思ったより難しく時間がかかってしまったが、とりあえず最後まで終わらせられてかなり勉強になった

今日はここまで
明日はもしかしたら別のことやってるかも...

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?