This article describes how to pull out the database service of phpenv to an independent environment.
PHPENV can define database services in SERVICE through environment variables.
If necessary in the development environment, we can easily restart all services through commands such as ./start and ./restart. nginx, fpm and db.
But if it is in online mode, I don't want to restart the website, and even the entire DB will restart. For example, when the php version is updated, the DB does not need to be restarted.
Because phpenv is only a simple bash container control environment, basically the database of phpenv can be built in another independent environment.
The following is how I do it,
I use a Synalogy Nas stand, so I put it in the /volume1/docker directory.
In the Linux environment, if you can't start the database container smoothly, don't be alarmed, just follow the instructions to create the directory, and adjust the permission owner to 999.
1. I changed the name of phpenv to database.
git clone https://github.com/DevinY/phpenv.git database
2. Directly add the environment configuration file of the database to the envs directory, the content is as follows:
DEFAULT=mariadb_ssh
SERVICES="ssh_db"
FOLDER=/volume1/docker/ccc/storage/app/backup
WORKSPACE=db
PROJECT=db
SSH_PORT=2260
DB_PORT=3360
USER_ID=1026
GROUP_ID=100
3. In the above settings, I specified DEFAULT to use the yml file, which is in the services directory, so I copied it to the database directory through the following command.
cp services/mariadb_ssh.yml .
4. We can cat maraidb_ssh.yml to view, please adjust according to your own needs, this file can be logged in as root without a password root account password.
version: '3.6'
services:
db:
image: mariadb:10.5.5
#ports:
# - ${DB_PORT-1250}:3306
volumes:
#- ./etc/my.cnf:/etc/mysql/conf.d/my.cnf
- ./data/${PROJECT-default}:/var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD= "yes"
- MYSQL_ROOT_HOST=127.0.0.1
- TZ=Asia/Taipei
restart: unless-stopped
network_mode: service:ssh
Here you can see that the directory of the database is the PROJECT name we set in the second step above.
In addition, network_mode: is used here as service, which represents the container where I want to hang the DB service on the SSH In this way, I can better access the container to manage the database through the SSH connection.
So in the second step of the setting, you should have found another setting, SERVICES="ssh_db"
This is to inform that when we use the ./start command, we will also start the services/ ssh_db.yml file.
We can look at the content of ssh_db.yml as follows:
version: '3.6'
services:
ssh:
build:
context: ./dockerfiles
dockerfile: Dockerfile-ssh-${CPU-x86_64}
args:
USER_ID: ${USER_ID-1000}
GROUP_ID: ${GROUP_ID-1000}
image: ${PROJECT}_ssh
ports:
- ${SSH_PORT-2222}:22
- ${DB_PORT-127.0.0.1:3306}:3306
volumes:
- ./etc/php:/usr/local/etc/php/conf.d
- ./etc/code-server:/home/dlaravel/.vscode-server
- ./authorized_keys:/home/dlaravel/.ssh/authorized_keys
- ${FOLDER-./project}:/var/www/html
networks:
- dlaravel_net
networks:
dlaravel_net:
In this file, SSH_PORT and DB_PORT will be used. In this example, I have opened 2260 and 3360 for external access.
The FOLDER here is an interesting setting. I directly mounted the Projecte of this website to the /var/www/html directory of the container.
In this way, if necessary, I can drop the .sql file into the storage folder of the Project, and then use the source to insert the data.
5. In the database directory, update your openssh public key to the authorized_keys file, and then you can start the container.
In addition to viewing at startup, it is also possible to view through ./console ps, as shown in the following screen:
6. As you can see, the SSH I use is port 2260, so add the following configuration to my ~/.ssh/config
Host db
HostName 192.168.99.130
User dlaravel
Port 2260
IdentitiesOnly yes
IdentityFile=~/.ssh/id_ed25519
In the above settings, the name of the Host is customized. My simple setting is called db. We can use the command line to perform a simple connection test:
Sixth, I personally like Sequel Pro, so my settings are probably like this
Successfully passed ssh encrypted connection, connect to DB
Supplement, in the latest version of PHPENV, 2023-01-21 added the WORKSPACE setting.
The old version defaulted to enter the php service when opening ./console, but now we can define the default service we want to enter through the worksapce.
Here I define WORKSPACE preset to enter the container service of db, we can also adjust the preset to enter the container service of ssh.
No Comment
Post your comment