Lightweight PHP 7.4 development environment created with Docker

environment

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.

procedure

1. Create Dockerfile

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"]

2. Build the Docker image

sudo docker build -t my-php:v0.1 .

3. Push to Docker Hub

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

4. How to use

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] Composer setup

** 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

[From the second time onwards]

Since composer.json exists from the second time onward, install the packages together with the following command.

bash-5.0# composer install

Package installation (addition)

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.

Create/edit PHP file

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 development server

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: // : 3000/from a Web browser. You can see that phpinfo (); described in index.php is executed.

image.png

The following article will be helpful for how to use Composer.

How to use Composer --Qiita

Recommended Posts

Lightweight PHP 7.4 development environment created with Docker
[First team development ③] Share the development environment created with Docker
Debug the VSCode + Docker + PHP development environment with XDebug.
Laravel development environment construction with Docker (Mac)
Build a PureScript development environment with Docker
Create Spring Boot-gradle-mysql development environment with Docker
Build a Wordpress development environment with Docker
Php settings with Docker
Disposable PHP with Docker
Build a WordPress development environment quickly with Docker
Rails development environment created with VSCode and devcontainer
[First team development ②] Build an environment with Docker
Wordpress local environment construction & development procedure with Docker
Create a Spring Boot development environment with docker
WSL2 + VSCode + Docker development environment
Build Docker + Laravel PHP + Vue.js development environment in 5 minutes
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
Build Java development environment with WSL2 Docker VS Code
React environment construction with Docker
Comfortable Docker environment created with WSL2 CentOS7 and Docker Desktop
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
How to build Rails, Postgres, ElasticSearch development environment with Docker
[Vagrant] Prepare LAMP development environment with Vagrant (centos + apache + MySQL + PHP)
I tried to create a padrino development environment with Docker
Rails + MySQL environment construction with Docker
Node.js environment construction with Docker Compose
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Environment construction with Docker for beginners
[PDO → DB connection with docker, PHP]
Prepare Java development environment with Atom
Build PlantUML environment with VSCode + Docker
Build Rails environment with Docker Compose
Create SolrCloud verification environment with Docker
Create Laravel environment with Docker (docker-compose)
Build jooby development environment with Eclipse
[Environment construction with Docker] Rails 6 & MySQL 8
Build Unity development environment on docker
Build docker + laravel environment with laradock
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to install Pry after building Rails development environment with Docker
Prepare a transcendentally simple PHP & Apache environment on Mac with Docker
PostgreSQL environment construction with Docker (from setup to just before development)
Build a development environment for Django + MySQL + nginx with Docker Compose
Alert slack with alert manager in Docker environment
Prepare Java development environment with VS Code
GPU environment construction with Docker [October 2020 version]
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Sapper × Go (echo) × Docker development environment construction
Create Rails 6 + MySQL environment with Docker compose
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Edit Mysql with commands in Docker environment
Create a MySQL environment with Docker from 0-> 1
[Docker] Create Node.js + express + webpack environment with Docker
Spring Boot + Docker Java development environment construction