The following environment was created on a virtual machine (VM). You can do the same with Docker Desktop for Windows.
It is assumed that you have acquired a Docker Hub account and completed docker login
.
This time we will use Alpine Linux as the base Docker image.
The line starting with #
is a comment, so it is not actually necessary.
#Use php Docker image with alpine
FROM php:7-alpine3.12
#Port open to the outside
EXPOSE 8000
#Update package repository list with package manager apk
RUN apk update --no-cache && apk upgrade --no-cache
#Install composer
WORKDIR /tmp
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
#Installation of packages required for development
RUN apk add --no-cache bash vim jq
#Install any extensions required by PHP
#Package List: https://gist.github.com/chronon/95911d21928cff786e306c23e7d1d3f3
RUN apk add --no-cache oniguruma-dev && \
docker-php-ext-install -j$(nproc) mbstring pdo_mysql
#Working directory
WORKDIR /work
CMD ["tail", "-f", "/dev/null"]
sudo docker build -t my-php:v0.1 .
Change <username>
to your Docker Hub username.
sudo docker tag my-php:v0.1 <username>/my-php:v0.1
sudo docker push <username>/my-php:v0.1
Add -p
to open the port. $ PWD:/work
maps the current directory to/work
in the container.
sudo docker run -it --rm -v $PWD:/work -p 3000:8000 my-php:v0.1 /bin/bash
** First time only ** Set up composer with the following command. This step is not necessary if composer.json exists.
bash-5.0# composer init
Package name (<vendor>/<name>) [root/work]: tomoyk/php-docker-example
Author [, n to skip]: Tomoyuki KOYAMA <[email protected]>
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []:
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
{
"name": "tomoyk/php-docker-example",
"authors": [
{
"name": "Tomoyuki KOYAMA",
"email": "[email protected]"
}
],
"require": {}
}
Do you confirm generation [yes]? yes
Would you like the vendor directory added to your .gitignore [yes]? yes
Since composer.json exists from the second time onward, install the packages together with the following command.
bash-5.0# composer install
If you want to add a new package, use the following command. This time, overtrue/phplint is installed.
bash-5.0# composer require overtrue/phplint
If the installation is successful, overtrue/phplint will be added to require
in composer.json.
Also, a vendor directory is created.
bash-5.0# ls
Dockerfile README.md composer.json composer.lock vendor
Try running the installed phplint.
bash-5.0# ./vendor/bin/phplint
phplint 2.0.2 by overtrue and contributors.
No config file loaded.
....................................................... 56 / 339 (16%)
........................................................ 112 / 339 (33%)
........................................................ 168 / 339 (49%)
........................................................ 224 / 339 (66%)
........................................................ 280 / 339 (82%)
........................................................ 336 / 339 (99%)
....
Time: < 1 sec Memory: 4.0 MiB Cache: No
OK! (Files: 339, Success: 339)
Some packages may require extension.
In that case, add the package with docker-php-ext-install
of Dockerfile and rebuild.
Here we use vim. You may use an editor such as VS Code or an integrated development environment from the host machine side.
bash-5.0# ls
Dockerfile README.md composer.json composer.lock vendor
bash-5.0# vim index.php
###Write PHP
bash-5.0# cat index.php
<?php
phpinfo();
Start the development server with the following command.
bash-5.0# php -S 0.0.0.0:8000
[Mon Dec 14 02:45:24 2020] PHP 7.4.13 Development Server (http://0.0.0.0:8000) started
[Mon Dec 14 03:03:26 2020] 172.17.0.1:57906 Accepted
[Mon Dec 14 03:03:26 2020] 172.17.0.1:57906 [200]: GET /
[Mon Dec 14 03:03:26 2020] 172.17.0.1:57906 Closing
[Mon Dec 14 03:03:27 2020] 172.17.0.1:57918 Accepted
[Mon Dec 14 03:03:27 2020] 172.17.0.1:57918 [404]: GET /favicon.ico - No such file or directory
[Mon Dec 14 03:03:27 2020] 172.17.0.1:57918 Closing
[Mon Dec 14 03:03:39 2020] 172.17.0.1:58018 Accepted
[Mon Dec 14 03:03:39 2020] 172.17.0.1:58018 [200]: GET /
[Mon Dec 14 03:03:39 2020] 172.17.0.1:58018 Closing
[Mon Dec 14 03:03:40 2020] 172.17.0.1:58030 Accepted
[Mon Dec 14 03:03:40 2020] 172.17.0.1:58030 [404]: GET /favicon.ico - No such file or directory
[Mon Dec 14 03:03:40 2020] 172.17.0.1:58030 Closing
When accessing from outside the container, add the -p
option of the docker command like docker run -p 3000: 8000
.
Since -p 3000: 8000
is given this time, access http: // phpinfo ();
described in index.php is executed.
The following article will be helpful for how to use Composer.
Recommended Posts