by Devin Yang
(This article was automatically translated.)

Published - 1 year ago ( Updated - 1 year ago )

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.

 

Tags: phpenv

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


fastapi,phpenv,docker

How do I create a fastapi runtime environment with phpenv

phpenv php laravel runs HAProxy, and automatically applies for and updates free certification, now let him run Python's fastapi. There is no limit to phpenv, the limit is your idea.😆

docker,phpenv

Unable to ping host.docker.internal on Linux

You can check with docker version, confirm that the version is Docker v20.10+, you can add extra_hosts as follows:

docker,phpenv

Ran out of Docker IPv4 address pool problem solving

Maybe in the old version of the docker environment, when using Docker compose to start the container, you will encounter the following error: Error response from daemon: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network This is actually this It is a symptom of running out of default-address-pools. In some environments, docker-compose will use the private network of Class B by default. The private IP of segment 172 has a range, from 172.16.0.0 to 172.31.255.255. That is to say, when we start a docker-compose Project, it will eat a private section of Class B, which is very heroic.