This file is part of: PHP Development Template Stack
Copyright (c) 2024 Nico Jan Eelhart
This source code is licensed under the MIT License found in the ‘LICENSE.md’ file in the root directory of this source tree.
1. Create and start a PHP developer container
This section describes how to create and start the Docker PHP container. If you want to experiment with a swarm (create, initialize, and run different containers), refer to the document: How to Create a Swarm
Side note: Docker call syntax
Docker call syntax
(Skip this if you known docker basics)
Take note: Docker calling context Because we use Docker files (Dockerfile and compose) with descriptive names, for example, Dockerfile_Nodejs_React_Cont instead of plain Dockerfile, this has an impact on the way Docker commands are run and called. For example, with a plain Dockerfile, we would use this command to call the Docker file in the Docker Compose file:context: . dockerfile: Dockefile
In our case, we cannot use the default name but have to specify the name we gave, thus:
build: context: . dockerfile: Dockerfile_Nodejs_React_Cont
The same applies for using the build command. With the default Dockerfile, you can use this:
docker build # This will assume a file: Dockerfile is available
With the named file, we have to use
docker build -f MyDockerFileNameHere
The same applies for running the Compose file (use -f option)
1.1 The basic PHP container setup
This section creates and start the PHP container in docker Desktop. To Setup creat and start the PHP container in docker Desktop execute this command from the ApachePHPWebService directory:
docker compose -f compose_apache_php_cont.yml up -d # To rebuild an existing container avoid caching issues, you can use: docker-compose -f compose_apache_php_cont.yml up -d --build --force-recreate
Results & running the sample app
Test the container by executing the following tasks
Available default websites, after container creation:
♦ http://localhost:8071/phpinfo.php
♦ http://localhost:8071/index.php
Location of files (current ‘Document Root’) in container:
♦ /usr/local/apache2/htdocs/public
Update Document Root in:
° /usr/local/apache2/conf/extra/httpd-vhosts.conf
° Tip: For a second site enter the setting in httpd-vhosts.conf and add a alias Alias /test2 /usr/local/apache2/htdocs/test2/public
Set www-data owner andfile permissions in:
° chown -R www-data ./htdocs
° chmod -R 755 ./htdocs
mount bind location in Windows host:
♦ ..\ApachePHPWebService\app
Symfony
Symfony is included, start project:
- symfony new project-name
- Update the Document root to the public directory of the new created project directory.
- Copy the
/usr/local/apache2/htdocs/.htaccess
file to the new public root directory and make sure the www-data owner and permissions are set (restart) - For more information about Symfony, see here
1.2 Add PHPUnit to the image (sub-container)
Optional you can add the PHPunit test framework to the image, after executing the commands in the previous paragraph execute this command: ( again in the [name]Service directory)
docker compose -f compose_UnitTest_Addon.yml up -d --remove-orphans --build --force-recreate
Results & running the sample app
When this is done the following commands(in the container) should return the phpunit and composer versions
Expected commands results
phpunit --version # returns version phpunit composer -V # returns vcersio composer # In case the phpunit --version command returns not found, do this RUN echo 'export PATH="/root/.config/composer/vendor/bin:$PATH"' >> ~/.bashrc \ && . ~/.bashrc
Expected running Website
♦ http://localhost:8071/phpinfo.php
♦ http://localhost:8071/index.php
1.3 Create sub-container: Add Python front-end using pySide6 (Qt)
Optional you can add the Python Qt based front-end pySides6 to the image, after executing the commands in the previous paragraph execute this command: ( again in the [name]Service directory)
docker compose -f compose_python_frontend_addon.yml up -d --remove-orphans --build --force-recreate
Expected results
python3 --version && pip3 show PySide6 # Should display Python and pyside information
</small>
2. Develop and debug in Visual Studio Code
- Open VSC and press the docker Icon(left sidebar)
- Right Click on your container and choose “Attache Visual Studio Code” a new VSC Window opens that is mapped the container
- Choose: Open folder and select the folder /usr/local/apache2/htdocs
- For debug installation/configuration see the howto file: howto_steps_for_debugging
Appendix 1 Create other template from this template.
Side note: Create Project from Template
When You want to customize this template for your own template you can use the following rough procedure
Create Project from Template
Skipp this if you known how to deal with copy\customize docker files
To adapt the template directory for your project, follow these steps. This guide assumes you’re using the React stack; if you’re working with a different stack (e.g., PHP, Rust), simply replace “React” with the stack name your are using.s
- Copy the whole directory to your project name:
copy "React Development Template Stack" MyReactStack
- within your MyReactStack open the [name]Service directory
Warning When using multiple containers, it’s a good idea to rename the directory (for example, by adding a number) before proceeding. Otherwise, the containers will be grouped together, which is generally helpful, but this can lead to caching issues in certain container stacks, such as React. These issues may manifest as the same directories appearing in the container from a previous instance after running the compose_nodejs_react_cont.yml command. Caching problems can be quite troublesome in some Docker stack configurations- Customize the Dockerfiles: Since most Docker Compose setups involve a parent-child relationship (i.e., chaining), a change in one Dockerfile may require updates to all related files. Follow these steps:
3.1 In the first compose_* file change the services name to an appropriate name:
services: webserver-nodejs-react: # Change this
*Always use lowercase!
3.2 The above service name may appear more than once in the same file, update these service names as well!
3.3 Changes the service name from step 3.1 in the other compose_* files
3.4 Check the compose_* files when it contain a image name than update this to your own image name:
build:
context: .
dockerfile: Dockerfile_Nodejs_React_Cont
image: eelhart/react-base:latest
# Update above. i.e: [yourname/react-prjx]
3.5 This image name may appear in other compose_* files and other Dockerfile_* files, updates these image names as well.4 Lastly, update the ports to ensure that each host port is unique across all running containers. In your Docker Compose file, you might see this configuration:
ports:
target: 3001 # Container port.
published: 3002 # Host port, Make SURE it is unique
Alternatively, the syntax might look like this (achieving the same result):
ports:
- "3002:3001" # host:container
Make sure that Host port: 3002 is not used by any other docker container or other services on your host!