Something that I'd like to share with you!

Sunday, April 19, 2020

Minimal PHP/Apache Docker setup

No comments :

Why Docker?

The portability of the containers, can be deployed anywhere
Efficient system resource usage compare to VM

How to Dockerize your php application?

Assuming your OS already have a good and working Docker installation
Let us use single page PHP applications below, index.php inside a webfolder directory
index.php content;

<?php
phpinfo();
?>

[root@localhost php_docker]# tree .
.
└── webfolder
└── index.php

Create a Dockerfile

Dockerfile in a list of instruction/command call to assemble the image

[root@localhost php_docker]# tree .
.
├── Dockerfile
└── webfolder
└── index.php

Content of the Dockerfile;

FROM php:7.4-apache
COPY webfolder/ /var/www/html/
EXPOSE 80

Explanation for each line

FROM php:7.4-apache
  • php:7.4-apache based image selection
COPY webfolder/ /var/www/html/
  • copy webfolder content into container /var/www/html/
  • Apache2 default web directory
EXPOSE 80
  • expose port 80 to the host
Next lets build the docker image

[root@localhost php_docker]# docker build --rm -t myphpdocker .

docker build [OPTIONS] PATH | URL | -
--rm = Remove intermediate containers after a successful build
-t = Name and optionally a tag in the 'name:tag' format
myphpdocker = docker container name
. (single dot) = build path

Check the built image

[root@localhost php_docker]# docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myphpdocker         latest              0c1fe5b1aa9e        47 minutes ago      414 MB

Next is to run the docker

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
-t = Allocate a pseudo-TTY
-i = Keep STDIN open even if not attached
-it = For interactive processes (like a shell), you must use -i -t together
-p = Publish a container's port(s) to the host
-d = Run container in background and print container ID

Lets try the interactive mode first

[root@localhost php_docker]# docker run -p 80:80 -it myphpdocker

Apache log appear on the terminal.
Open your browser and head to "http:/<host ip>/"


Now try the detach mode

[root@localhost php_docker]# docker run -p 80:80 -d myphpdocker

Check the running container

[root@localhost php_docker]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
4619304831d4        myphpdocker         "docker-php-entryp..."   6 seconds ago       Up 5 seconds        0.0.0.0:80->80/tcp   frosty_tesla

No comments :