Attention
You can now run different PHP versions per project:
Release v3.0.0-beta-0.1
8. Work inside the PHP container¶
The Devilbox allows you to completely work inside the PHP container (no matter what version), instead of your host operating system.
This brings a lot of advantages, such as that you don’t have to install any development tool on your OS or if you are on Windows, you get a full blown Linux environment.
Additionally, special port-bindings and forwards are in place that allows you to even interchangably work locally or inside the container without having to alter any php config for database and other connections.
See also
Table of Contents
8.1. Enter the container¶
Entering the computer is fairly simple. The Devilbox ships with two scripts to do that. One for
Linux and MacOS (shell.sh
) and another one for Windows (shell.bat
).
8.1.1. Entering from Linux or MacOS: shell.sh
¶
# Navigate to the Devilbox directory
host> cd /path/to/devilbox
# Run provided script
host> ./shell.sh
# Now you are inside the PHP Linux container
devilbox@php-7.0.19 in /shared/httpd $
8.1.2. Entering from Windows: shell.bat
¶
# Navigate to the Devilbox directory
C:/> cd C:/Users/user1/devilbox
# Run provided script
C:/Users/user1/devilbox> shell.bat
# Now you are inside the PHP Linux container
devilbox@php-7.0.19 in /shared/httpd $
8.2. Inside the container¶
8.2.1. devilbox
user¶
By using the provided scripts to enter the container you will become the user devilbox
.
This user will have the same uid and gid as the user from your host operating system.
So no matter what files or directories you create inside the container, they will have the same permissions and uid/gid set your host operating system. This of course also works the other way round.
The uid and gid mappings are controlled via two .env
variables called NEW_UID and
NEW_GID
See also
If you want to find out more about synronized container permissions read up here: Syncronize container permissions
8.2.2. root
user¶
Sometimes however it is also necessary to do some actions that require super user privileges.
You can always become root inside the container by either impersonating it or by using sudo
to issue commands.
By default sudo
is configured to be used without passwords, so you can simply do the following:
# As user devilbox inside the container
devilbox@php-7.0.19 in /shared/httpd $ sudo su -
# You are now the root user
root@php-7.0.19 in /shared/httpd $
You can also use sudo
to run commands with root privileges without having to become root first.
# As user devilbox inside the container
devilbox@php-7.0.19 in /shared/httpd $ sudo apt update
devilbox@php-7.0.19 in /shared/httpd $ sudo apt install nmap
8.3. Leave the container¶
When you are inside the container and want to return to your host operating, just type exit
and you are out.
# As user devilbox inside the container
devilbox@php-7.0.19 in /shared/httpd $ exit
# You are now back on your host operating system
host>
8.4. Host to Container mappings¶
This section will give you an idea that there is actually no difference from inside the container and on your host operating system. Directory permissions, IP addresses, ports and DNS entries are fully syncronized allowing you to switch between container and host without having to change any settings.
8.4.1. File and directory Permissions¶
The username inside the container (devilbox
) might be different from your local host operating
system username, however its actual uid and gid will match. This is to ensure file and directory
permissions are synronized inside and outside the container and no matter from which side you
create files and directories, it will always look as if they are owned by your system user.
The uid and gid mappings are controlled via two .env
variables called NEW_UID and
NEW_GID
8.4.2. Directory mappings¶
One thing you should understand is the relation between the directories on your host operating system and the corresponding directory inside the PHP container.
The location of the data directory (HOST_PATH_HTTPD_DATADIR) on your host computer is controlled
via the HOST_PATH_HTTPD_DATADIR
variable inside the .env
file. No matter what location you
set it to, inside the container it will always be mapped to /shared/httpd
.
See the following table for a few examples:
Host operating system | Inside PHP container | |
---|---|---|
Data dir | ./www/data |
/shared/httpd |
Data dir | /home/user1/www |
/shared/httpd |
Data dir | /var/www |
/shared/httpd |
8.4.3. IP address mappings¶
The following table shows a mapping of IP addresses and hostnames. In other words, when you are inside the PHP container, you can reach the services via the below defined IP addresses or hostnames:
Container | Name | Hostname | IP Address |
---|---|---|---|
DNS | bind | bind | 172.16.238.100 |
PHP | php | php | 172.16.238.10 |
Apache, Nginx | httpd | httpd | 172.16.238.11 |
MySQL, MariaDB, PerconaDB | mysql | mysql | 172.16.238.12 |
PostgreSQL | pgsql | pgsql | 172.16.238.13 |
Redis | redis | redis | 172.16.238.14 |
Memcached | memcd | memcd | 172.16.238.15 |
MongoDB | mongo | mongo | 172.16.238.16 |
Note
It is recommended to use hostnames as they can be remembered much easiert.
An example to access the MySQL database from within the PHP container:
# Access MySQL from your host operating system
host> mysql -h 127.0.0.1 -u root -p
# Access MySQL from within the PHP container
devilbox@php-7.0.19 in /shared/httpd $ mysql -h mysql -u root -p
So when setting up a configuration file from your PHP project you would for example use
mysql
as the host for your MySQL database connection:
<?php
// MySQL server connection
mysql_host = 'mysql';
mysql_port = '3306';
mysql_user = 'someusername';
mysql_pass = 'somepassword';
?>
8.4.4. Port mappings¶
By default, ports are also synronized between host operating system (the ports that are exposed)
and the ports within the PHP container. This is however also configurable inside the .env
file.
Service | Port from host os | Port from within PHP container |
---|---|---|
PHP | NA | 9000 |
Apache/Nginx | 80 |
80 |
MySQL | 3306 |
3306 |
PostgreSQL | 5432 |
5432 |
Redis | 6379 |
6379 |
Memcached | 11211 |
11211 |
MongoDB | 27017 |
27017 |
8.4.5. DNS mappings¶
All project DNS records are also available from inside the PHP container independent of the value of TLD_SUFFIX.
The PHP container is hooked up by default to the bundled DNS server and makes use Setup Auto DNS.
See also
You can achieve the same on your host operating system by explicitly enabling auto-dns. See also: Setup Auto DNS.
8.5. Checklist¶
- You know how to enter the PHP container
- You know how to become root inside the PHP container
- You know how to leave the container
- You know that file and directory permissions are synronized
- You know by what hostnames you can reach a specific service
- You know that project urls are available inside the container and on your host