尤川豪   ·  2年前
445 貼文  ·  275 留言

Docker Practice

會用 Linux 架網站,就一定能學會 docker。

因為 docker 只是把架站過程 拆解抽象化 而已。

Run a php script file

Dockerfile

FROM php:7.4-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]

index.php

<?php

echo "Hello from the docker container";

cmd

$ docker build -t my-php-app .
$ docker run -it --rm --name my-running-app my-php-app

cmd for running script without Dockerfile

docker run -it --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:7.4-cli php index.php

Run a php file with the built-in server to show web page

cmd

docker run -it -v "$PWD":/usr/src/myapp -w /usr/src/myapp -p 8080 php:7.4-cli php -S 0.0.0.0:8080

cmd to see what is the working port

docker ps
  • will see this 0.0.0.0:32770->8080/tcp

open browser with all of these, all works

http://0.0.0.0:32770/
http://localhost:32770/
http://127.0.0.1:32770/
  • 32770 is just a random valid port found by docker. assign the port manually is better
docker run -it -v "$PWD":/usr/src/myapp -w /usr/src/myapp -p 8080:8080 php:7.4-cli php -S 0.0.0.0:8080

Run a php + apache site to show web page

Dockerfile

FROM php:7.4-apache
COPY . /var/www/html/

index.php

<?php

echo "<h1>This web page is from docker.</h1>";

cmd

docker build -t my-php-app .
docker run -p 8080:80 my-php-app

cmd to run without Dockerfile

docker run -p 8080:80 -v "$PWD":/var/www/html php:7.4-apache

Run a mysql client app

docker-compose.yml

# Use root/example as user/password credentials
version: '3.1'

services:

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

cmd

docker-compose up

Run a php + apache + mysql site to show web page

Dockerfile

FROM php:7.4-apache
RUN docker-php-ext-install mysqli

docker-compose.yml

# Use user/test as user/password credentials
version: "3.1"

services:

  www:
    build: .
    ports:
      - "8080:80"
    volumes:
      - .:/var/www/html/
    links:
      - db

  db:
    image: mysql:8.0
    ports:
      - "3306:3306"
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      # remember to create the `mysql-data` folder in the current path
      - ./mysql-data:/var/lib/mysql
    environment:
      MYSQL_DATABASE: myDb
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test

index.php

<?php

echo "<h1>This web page is from docker LAMP.</h1>";

echo "<hr>";

$dbname = 'myDb';
$dbuser = 'user';
$dbpass = 'test';
$dbhost = 'db';

$connect = mysqli_connect($dbhost, $dbuser, $dbpass)
    or die("Unable to Connect to '$dbhost'");

mysqli_select_db($connect, $dbname) or die("Could not open the db '$dbname'");

mysqli_query($connect, "CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255));");

$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($connect, $test_query);

$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
    $tblCnt++;
    echo $tbl[0]."<br />\n";
}

if (!$tblCnt) {
    echo "There are no tables<br />\n";
} else {
    echo "There are $tblCnt tables<br />\n";
}

Run a Laravel + apache + ubuntu + mysql site


Run a phpmyadmin service to administrate the above site


Understand Laradock

in the app repo

git submodule add https://github.com/Laradock/laradock.git

so the file structure would be

* project-a
*   laradock-a
* project-b
*   laradock-b

launch the server & database

docker-compose up -d nginx mysql

enter the terminal in the container

docker-compose exec workspace bash

Understand Vessel

  • vessel 是用 composer 安裝的,所以需要在電腦上準備好可以跑 composer 的環境
composer require shipping-docker/vessel
php artisan vendor:publish --provider="Vessel\VesselServiceProvider"

# Run this once to initialize project
# Must run with "bash" until initialized
bash vessel init

./vessel start

Understand Sail

  • legacy laravel 裝不起來. php 版本那些有衝突
  • https://github.com/laravel/sail/blob/1.x/composer.json
  • 用在最新的 larvel 上吧
  • 作業系統倒是不用很新. 也不用新到 php8. 使用 php7.3 夠跑第一次的 sail 即可.
  • 還有 php7.3 相關的 php extension 那些要裝. 不難.
  • 之後都會透過 sail 在 docker 裡面跑. composer package 環境版本就完全不用管了.

install laravel 8.0

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

launch the docker

cd example-app

./vendor/bin/sail up

more

sail up -d
sail down
sail artisan queue:work
sail php --version
sail php script.php
sail composer require laravel/sanctum
sail node --version
sail npm run prod
sail test
sail artisan test
  • 應該是方便開發而已. 正式環境先別使用吧.
  • 之後 laravel8 的快速安裝與啟用本機開發環境,真的是神速了.

to make sure the database is ready

./vendor/bin/sail artisan migrate

play more, enable the auth feature

./vendor/bin/sail composer require laravel/breeze --dev
./vendor/bin/sail artisan breeze:install
./vendor/bin/sail npm install
./vendor/bin/sail npm run dev
  分享   共 1,369 次點閱
按了喜歡:
共有 0 則留言
還沒有人留言。歡迎分享您的觀點、或是疑問。
您的留言
尤川豪
445 貼文  ·  275 留言

Devs.tw 是讓工程師寫筆記、網誌的平台。隨手紀錄、寫作,方便日後搜尋!

歡迎您一起加入寫作與分享的行列!

查看所有文章