Devs.tw 是讓工程師寫筆記、網誌的平台。歡迎您隨手紀錄、寫作,方便日後搜尋!
會用 Linux 架網站,就一定能學會 docker。
因為 docker 只是把架站過程 拆解
與 抽象化
而已。
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
want to know what's the OS? find it on docker hub description
want to observe the image layers? find it on docker hub tags
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
-v
就是 --volume
就是 VOLUME
. /usr/src
有在 Dockerfile 裡面產生過. 反正就找個路徑放 source code 而已.-w
就是 --workdir
就是 WORKDIR
. 就跟 cd 進去準備工作差不多意思.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
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/
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
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
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
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";
}
docker-compose.yml
should be finein 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
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
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
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