Docker and PHP: Difference between revisions
No edit summary |
No edit summary |
||
(20 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Contents of a Docker directory == | |||
The following files must be present to use <code>docker-compose</code>: | |||
docker-compose.yml | |||
Dockerfile | |||
<code>docker-compose build</code> will build the image. | |||
== Run a basic PHP Docker image == | |||
docker run --rm php:7.4-apache-buster php --version | |||
The <code>--rm</code> argument removes the container after it has finished. | |||
== Accessing a running container == | |||
docker-compose exec [container] bash | |||
Note that the container name is as provided in <code>docker-composer.yml</code>, it is not the container name printed to the command line when you run <code>docker-compose up</code> (however you must pass that name to <code>docker exec</code>). | |||
== Enabling Apache modules == | |||
Checking which modules are enabled: | |||
apache2ctl -M | |||
Enabling modules (SSL and Rewrite are two common requirements): | |||
RUN a2enmod rewrite ssl | |||
== Basic PHP Dockerfile == | == Basic PHP Dockerfile == | ||
Build an image and copy in a PHP file: | Build an image and copy in a PHP file: | ||
FROM php:7. | FROM php:7.4-apache-buster | ||
COPY index.php /var/www/html/ | COPY index.php /var/www/html/ | ||
To build the image: | |||
docker build -t phpinfo:latest . | |||
To run the image: | |||
docker run --rm -p 8080:80 phpinfo | |||
== Basic PHP Docker Compose == | |||
version: "3" | |||
services: | |||
app: | |||
image: phpinfo | |||
container_name: phpinfo | |||
build: | |||
context: . | |||
dockerfile: docker/Dockerfile | |||
ports: | |||
- 8080:80 | |||
volumes: | |||
- .:/var/www | |||
This can be built and run with: | |||
docker-compose up --build -d | |||
<code>-d</code> runs the container as a daemon, and returns the command line immediately. | |||
Dockerfile doesn't have to be in a different directory, but this seems to be something of a convention and perhaps keeps things tidy. By default docker-compose will look for a Dockerfile in the current directory. | |||
== Database == | |||
Assuming you want to use MariaDB: | |||
version: "3" | |||
services: | |||
app: | |||
build: . | |||
ports: | |||
- 8080:80 | |||
database: | |||
image: mariadb | |||
restart: always | |||
environment: | |||
MARIADB_RANDOM_ROOT_PASSWORD: "yes" | |||
MARIADB_DATABASE: "app" | |||
MARIADB_USER: "app" | |||
MARIADB_PASSWORD: "secret" | |||
ports: | |||
- 3307:3306 | |||
The advantage of adding a port mapping here is that you can connect to the database container from the host, which is handy if you have a graphical MariaDB client such as DBeaver. You can of course also login to the container and run the mysql client on the command line. | |||
Logging into the container using the host: | |||
mysql -u app -D app -h 127.0.0.1 -P 3307 -p | |||
Note: 127.0.0.1 must be used as the host, as MySQL will usually try a socket connection by default if localhost is used (also it may attempt to use ::1, the IPv6 address, which may not work on your setup). | |||
For port mapping from the host, choose something other than the default port (3306), otherwise you will get a clash if MariaDB is running on the host (which it probably will be on a local development machine). | |||
== Extensions == | |||
PHP extensions can be installed via <code>docker-php-ext-install</code>, e.g. to install the MySQL extension: | |||
RUN docker-php-ext-install pdo_mysql | |||
[[Category:Docker]] |
Latest revision as of 15:04, 22 June 2021
Contents of a Docker directory
The following files must be present to use docker-compose
:
docker-compose.yml Dockerfile
docker-compose build
will build the image.
Run a basic PHP Docker image
docker run --rm php:7.4-apache-buster php --version
The --rm
argument removes the container after it has finished.
Accessing a running container
docker-compose exec [container] bash
Note that the container name is as provided in docker-composer.yml
, it is not the container name printed to the command line when you run docker-compose up
(however you must pass that name to docker exec
).
Enabling Apache modules
Checking which modules are enabled:
apache2ctl -M
Enabling modules (SSL and Rewrite are two common requirements):
RUN a2enmod rewrite ssl
Basic PHP Dockerfile
Build an image and copy in a PHP file:
FROM php:7.4-apache-buster COPY index.php /var/www/html/
To build the image:
docker build -t phpinfo:latest .
To run the image:
docker run --rm -p 8080:80 phpinfo
Basic PHP Docker Compose
version: "3" services: app: image: phpinfo container_name: phpinfo build: context: . dockerfile: docker/Dockerfile ports: - 8080:80 volumes: - .:/var/www
This can be built and run with:
docker-compose up --build -d
-d
runs the container as a daemon, and returns the command line immediately.
Dockerfile doesn't have to be in a different directory, but this seems to be something of a convention and perhaps keeps things tidy. By default docker-compose will look for a Dockerfile in the current directory.
Database
Assuming you want to use MariaDB:
version: "3" services: app: build: . ports: - 8080:80 database: image: mariadb restart: always environment: MARIADB_RANDOM_ROOT_PASSWORD: "yes" MARIADB_DATABASE: "app" MARIADB_USER: "app" MARIADB_PASSWORD: "secret" ports: - 3307:3306
The advantage of adding a port mapping here is that you can connect to the database container from the host, which is handy if you have a graphical MariaDB client such as DBeaver. You can of course also login to the container and run the mysql client on the command line.
Logging into the container using the host:
mysql -u app -D app -h 127.0.0.1 -P 3307 -p
Note: 127.0.0.1 must be used as the host, as MySQL will usually try a socket connection by default if localhost is used (also it may attempt to use ::1, the IPv6 address, which may not work on your setup).
For port mapping from the host, choose something other than the default port (3306), otherwise you will get a clash if MariaDB is running on the host (which it probably will be on a local development machine).
Extensions
PHP extensions can be installed via docker-php-ext-install
, e.g. to install the MySQL extension:
RUN docker-php-ext-install pdo_mysql