Prepare a transcendentally simple PHP & Apache environment on Mac with Docker

Introduction

Since I received a PHP project, I thought I would do it with MAMP, but suddenly it became "Do I need MAMP ...?: Thinking:".

Because I didn't use DB this time, and I thought that Mac has PHP and Apache.

Terminal


$ php -v
PHP 7.3.11 (cli) (built: Jun  5 2020 23:50:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies

$ httpd -v
Server version: Apache/2.4.41 (Unix)
Server built:   Jun  5 2020 23:42:06

Yeah yeah it's in.

However, since the PHP version of the production environment where the source works was 5 series, it was necessary to switch the PHP version.

I came up with three ways to prepare the development environment.

  1. Develop by switching PHP versions with Homebrew
  2. Tweak MAMP a little and switch PHP version to develop
  3. Build Docker

There aren't that many PHP projects, but I don't like to mess around with the local environment, and I'm used to being able to start Apache with a command poppy like Linux, so I decided to adopt the third Docker this time. did.

Premise

environment Version etc.
MacBook Pro 2019 model
OS macOS Catalina
Docker Engine v19.03.13
PHP 5.4

1. Install Docker Desktop

Select ** Download for Mac ** on the Official Site (https://www.docker.com/get-started) to download Docker Desktop.

スクリーンショット 2020-12-10 17.40.49.png

Run Docker.dmg and install it.

Run it when the installation is complete. After execution, you may be asked for a password, but enter it as appropriate.

When executed, a whale will appear on the right side of the menu bar above. (Sorry for being tiny) ↓ スクリーンショット 2020-12-10 17.47.05.png

Try typing a command in the terminal to check.

$ docker version
Client: Docker Engine - Community
 Cloud integration: 1.0.2
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 16:58:31 2020
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:07:04 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

2. Create and build a Dockerfile

2.1. Creating a Dockerfile

Write a Dockerfile to create an image.

Create an appropriate directory and create a Dockerfile.

$ mkdir php5_apache

$ cd php5_apache

$ vim Dockerfile

Let's borrow the Official Docker Image. You can build a container that also includes Apache by writing php: <version> -apache. The version used this time is * 5.4 *, so describe it as it is.

Dockerfile


FROM php:5.4-apache

2.2. Build

docker build <Dockerfile path> -t <image name>: <tag name> Build with.

Here, specify * php5_apache * as the image name and * 1.0 * as the version of the tag.

-t: Option to specify name and tag

$ docker build ./ -t php5_apache:1.0

Let's check if it was built.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php5_apache         1.0                 7246b9f23253        5 years ago         470MB

By the way, you can also check it from the dashboard of Docker Desktop. スクリーンショット 2020-12-10 18.31.52.png

3. Create and launch a container

Let's start it immediately.

I already have the source I want to move, so I will mount it.

The command is as follows. docker run -d -p <host side port>: <container side port> -v <host side path>: <container side path> php5_apache: 1.0

The explanation of the option used this time is as follows.

---d: Start in detached mode (background execution) ---p: Expose the container port to the host side ---v: Mount the host directory on the container

$ docker run -d -p 80:80 -v <Working directory>:/var/www/html php5_apache:1.0

Let's check if it started successfully.

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS              PORTS                NAMES
469f65bd6a4f        php5_apache:1.0     "apache2-foreground"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   recursing_hodgkin

4. Check with a browser

Since it opened on port 80, enter localhost in the address bar of the browser without specifying the port to open it. It is OK if PHP etc. are output safely.

5. Start and stop the container

Once you have created a container, you can start it with docker start <container ID or container name> and stop it with docker stop <container ID or container name>.

This time I will try using the container name. It was named * recursing_hodgkin *, so let's stop and start it.

$ docker stop recursing_hodgkin
$ docker start recursing_hodgkin

6. Log in to the container

You can log in as root below. By the way, the OS is Debian.

$ docker exec -it recursing_hodgkin /bin/bash

When it comes out, it's the familiar exit.

# exit

7. Bonus

7.1. Apache errors

When I looked at the log, I found an error-like log.

$ docker logs recursing_hodgkin
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Dec 10 09:58:54.402679 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.4.45 configured -- resuming normal operations
[Thu Dec 10 09:58:54.402792 2020] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

The content is "The domain name has not been set. I'm in trouble: disappointed_relieved:". You kindly wrote the solution, "Set the ServerName directive."

Maybe it's okay if you don't set it, but it's unpleasant that the error remains, so I'll set it.

(I was just playing around with CentOS, so I ended up with Debian's Apache config file or something like that.)

First, log in to docker.

$ docker exec -it recursing_hodgkin /bin/bash

I'm used to vim, so I installed it. I couldn't install vim without apt update, so I have to do it. I thought I should have written it in the Dockerfile from the beginning ...

In the container


# apt update
# apt -y upgrade
# apt install -y vim
# vim /etc/apache2/conf-enabled/httpd.conf

It seems that /etc/apache2/conf-enabled/*.conf is read as a config file, so I will describe it in this.

httpd.conf


ServerName localhost:80

Restart Apache service.

# /etc/init.d/apache2 reload
[ ok ] Reloading web server: apache2.

When I play with CentOS, it restarts with systemctl restart httpd, so I want to restart with service apache2 restart, but when I execute this, the container also died, so hit the above command ..

7.2. I can't use multibyte characters

** I didn't put the module. ** **

I heard such a voice from somewhere.

Put the module,

# docker-php-ext-install mbstring

Add to ini and enable it.

php.ini


extension=mbstring.so

The customary apache restart.

# /etc/init.d/apache2 reload

At the end

I'm not very familiar with Docker, so I left it as a memorandum.

There was a stumbling block when actually starting development, such as issuing an error log of php and writing the part written as a bonus in the Dockerfile, so I would like to write an article about it later.

Recommended Posts

Prepare a transcendentally simple PHP & Apache environment on Mac with Docker
Prepare a scraping environment with Docker and Java
Beginners install docker for mac and prepare php7.0 operating environment
Build a CentOS 8 virtual environment on your Mac with VirtualBox
[Vagrant] Prepare LAMP development environment with Vagrant (centos + apache + MySQL + PHP)
Create a Vue3 environment with Docker!
Build a Node.js environment with Docker
Building a haskell environment with Docker + VS Code on Windows 10 Home
Laravel development environment construction with Docker (Mac)
Build a PureScript development environment with Docker
Build a Java development environment on Mac
[docker] [nginx] Make a simple ALB with nginx
Build a Wordpress development environment with Docker
Build an environment with Docker on AWS
Build a JMeter environment on your Mac
Run Ubuntu + ROS with Docker on Mac
Lightweight PHP 7.4 development environment created with Docker
Build a simple Docker + Django development environment
Creating a java web application development environment with docker for mac part1
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Create a java web application development environment with docker for mac part2
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]
[Memo] Create a CentOS 8 environment easily with Docker
Just install Laravel8 on docker in PHP8 environment
Build a Laravel / Docker environment with VSCode devcontainer
Environment construction command memo with Docker on AWS
Building a Ruby environment for classes on Mac
Build a WordPress development environment quickly with Docker
Build a simple Docker Compose + Django development environment
Create a Spring Boot development environment with docker
Build a development environment to create Ruby on Jets + React apps with Docker
Create a Java development environment using jenv on Mac
Build Java development environment with VS Code on Mac
A simple CRUD app made with Nuxt / Laravel (Docker)
Easily build a Vue.js environment with Docker + Vue CLI
[Note] Build a Python3 environment with Docker in EC2
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
[Note] Create a java environment from scratch with docker
Php settings with Docker
[Docker] Build a Wordpress environment locally (Win, Mac) quickly
Debug the VSCode + Docker + PHP development environment with XDebug.
Disposable PHP with Docker
Creating a lightweight Java environment that runs on Docker
[Oracle Cloud] Build a 4-Node RAC environment of Oracle Database 19c with Docker on OCI Compute
Docker × Java Building a development environment that is too simple
I made a development environment with rails6 + docker + postgreSQL + Materialize.
A story about speeding up unittest on Docker for Mac
Build a Node-RED environment with Docker to move and understand
I tried running WordPress with docker preview on M1 Mac.
I tried to create a padrino development environment with Docker
Pytorch execution environment with Docker
Prepare Python3 environment with CentOS7
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
React environment construction with Docker
Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
Install metrics-server with Helm on Kubernetes that comes with Docker for Mac
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
I tried to create a Spring MVC development environment on Mac