Local PHP/Apache Setup 101

Created by josh
May 02, 2018 12:18:25 PM PDT (Revised September 08, 2020 9:09:11 AM PDT )


Learn the basics of containerizing your dev environment

Heyyall,

Docker can be an extremely useful development platform for building applications that scale anywhere from a local test environment to a collaborative cloud solution between teams. The best way to get familiar with how Docker works is to start small, such as building and automating a local test environment. If you have a chance, I recommend reading about Docker and understanding some fundamental principles of containerization platforms and how Docker is different than yet another virtual environment here. Once you familiarize yourself with the general overview and are ready to start developing, I would also recommend reading through some of the documentation, starting with this.

 

Getting started:

This tutorial/blog post of mine will show you a basic example of how to start up a project locally on your own PC.

 

1) Install Docker (instructions here)

# Bash installation (Debian Jessie/Stretch)


$ sudo apt-get update 


$ sudo apt-get install \
     apt-transport-https \
     ca-certificates \
     curl \
     gnupg2 \
     software-properties-common


$ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -


$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"


$ sudo apt-get update


$ sudo apt-get install docker-ce


# Verify...
$ which docker

 

/usr/bin/docker

 

Write your first Dockerfile. This will create a new image, based on the official PHP/Apache Dockerfile:

FROM php:7.2-stretch
COPY php-site/ /var/www/html

 

Note: the directory "php-site," per the example above, will need to be in the same directory as your Dockerfile. There are ways to adjust the "context," but for this example, keep these together.

Now, let's build the image then run it...

$ docker build --tag sample-web-image .

You just built the image and set the context to be the current working directory (hence the last parameter being a period). This is the directory where the Dockerfile and the rest of the build reside.

 

Let's see the image:

$ docker images

 

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sample-web-image    latest              1cadc7378907        28 hours ago        377MB

 

Great! Now let's run it...

$ docker run --detach --interactive --tty --publish 80:80 --name sample-web-container sample-web-image



$ docker ps

 

CONTAINER ID        IMAGE               ...        PORTS               NAMES
2bb45eee388e        sample-web-image    ...        0.0.0.0:80->80/tcp  sample-web-container

 

You should be set. Hit up localhost (port 80): (http://localhost)


 
Make sure you have content in your php-site directory mentioned above, or you may see a forbidden error!