Using docker / docker compose to deploy software applications

Time:2020-2-23

Swoft

The first high-performance PHP full stack framework based on swoole native orchestration in the new era, built-in orchestration network server and commonly used orchestration client, resident in memory, independent of traditional php-fpm, fully asynchronous non blocking IO implementation, similar to synchronous client writing to achieve asynchronous client use, no complex asynchronous callback, no tedious yield, It can be used to build high-performance web systems, APIs, middleware, basic services and so on.

Docker image of swoft

Sudden vernacular

It sounds strange to use docker to install swoft. Swoft is a PHP framework that relies on the swoole extension. To say that docker installs swoft is actually the component dependency and environment that docker needs to install swoft.

It will take some time to install and build the dependencies and environments required by the swoft framework running environment, such as PHP version > = 7.1, swoole version > = 2.1, and to install hiredis to help open the asynchronous redis client of swoole, and to require swoole to open the cooperation mode.

Therefore, in order to save our time, the official provides a docker image package, which contains the components required by the swoft running environment:

php 7.1+
swoole 2.1+ –enable-async-redis-client –enable-coroutine
composer
pecl

We only need to download the image and create a new container, which provides all the dependencies and environments required by the swoft framework. After attaching the sowft project on the host computer to the working directory of swoft image / var / www / swoft, we can continue our development or production work. Let you free yourself from the slightly cumbersome dependency and environment construction of swoft and directly enter the business development work.

At the beginning, I didn’t understand the swoft image well. The frame in the image is actually used for experience. I always mistakenly thought that I wanted to edit the swoft frame source code of the image for development

It should be noted that the entrypoint command of the sowft image (running the initialization command) is
ENTRYPOINT ["php", "/var/www/swoft/bin/swoft", "start"]

That is to say, the swoft service will start with the container, which requires that if we choose to mount the swoft project developed on the host to the working directory of the container, it must be completely installed (use composer to install the dependencies). At the same time, the container uses the swoft service as the pre process. If we want to stop / start to reload the swoft service, the container will follow the exit, which is a little inconvenient. Therefore, in order to facilitate the subsequent development, we should separate the swoft service as the front-end process of the container, so that the restart / stop of the swoft service in the container does not affect the operation of the container itself. Of course, if you just experience swoft, you can create and start the container directly. There is a set of completely installed swoft framework in the image.

Later, we will give a way to deploy the development or production environment with the help of the swoft image only by installing the docker / docker compose / git required for operation and maintenance on the host computer (modify the image entry point to bash mode, then enter the image and use the composer to install the dependency, start swoft and make full use of the image resources)

Docker deployment swoft

The host still needs to install the basic PHP / Composer (or you can CP your locally developed projects, but this may cause some component versions to be inconsistent. You need to submit the business code + composer.json + composer.lock file, exclude the vendor directory, and the online server will be the most standardized for composer install again)

1. Create a swoft project on the host (the host needs to install the basic PHP environment to use composer)

composer create-project --prefer-dist swoft/swoft swoft [--dev] && cd swoft
perhaps
git clone [email protected]ub.com:swoft-cloud/swoft.git && cd swoft && composer install && cd swoft

2. Pull the swoft image to create the swoft container and mount the installed swoft project on the host to the working directory of the swoft container

//Pull swoft image
//Associate the local swoft project directory with the mirrored project directory (/ var / www / swoft)
//Mapping host 8081 port to container 80 port
//The container is named mysoft
//Guard mode start
docker run -v $(pwd):/var/www/swoft -p 8081:80 --name mySwoft -d swoft/swoft
//See if the container is running
docker ps
//View container log
docker logs mySwoft

3. Enter the swoft container shell

//Executing bash of mysoft container in interactive mode
docker exec -it mySwoft bash
//Stop will stop the container, so you can exit the shell and start it with docker start mysoft
[email protected]:/var/www/swoft# php bin/swoft start|stop|reload
//Because we have attached the swoft project on the host computer to the project directory of the swoft container / var / www / swoft, later development and modification of the project on the host computer is enough
//You can use PS's FTP synchronization tool

You can view the corresponding component version through the command in the container shell of swoft

[email protected]:/var/www/swoft# php -v         
[email protected]:/var/www/swoft# php --ri swoole
[email protected]:/var/www/swoft# composer -V    
[email protected]:/var/www/swoft# pecl -V

Docker composer deployment of soft

The host still needs to install the basic PHP / Composer (or you can CP your locally developed projects, but this may cause some component versions to be inconsistent. You need to submit the business code + composer.json + composer.lock file, exclude the vendor directory, and the online server will be the most standardized for composer install again)
There is a docker-compose.yml file in the swoft project

version: '3'

services:
    swoft:
       image: swoft/swoft:latest
#      build: ./
       ports:
         -"80:80"? Port mapping
       volumes:
         -. /: / var / www / swoft ා mount the local swoft project to the image project path under the current path
       Stdin? Open: true? Open standard output
       TTY: true ා open TTY session
       Privileged: true ා give permissions such as creating folders
       Entrypoint: ["PHP", "/ var / www / swoft / bin / swoft", "start"]. The entry start command starts the swoft service

Naturally, it is easier to use docker, but it is still necessary to create a swoft project on the host

1. Create a swoft project on the host (the host needs to install the basic PHP environment to use composer)

composer create-project --prefer-dist swoft/swoft swoft [--dev] && cd swoft
perhaps
git clone [email protected]:swoft-cloud/swoft.git && cd swoft && composer install && cd swoft

2. Using docker compose to choreograph the startup container

Edit the docker-compose.yaml file to give the container a custom name

version: '3'

services:
    swoft:
       image: swoft/swoft:latest
       Container? Name: mysoft? Customize a name for the container to facilitate management
#      build: ./
       ports:
         -"80:80"? Port mapping
       volumes:
         -. /: / var / www / swoft ා mount the local swoft project to the image project path under the current path
       Stdin? Open: true? Open standard output
       TTY: true ා open TTY session
       Privileged: true ා give permissions such as creating folders
       Entrypoint: ["PHP", "/ var / www / swoft / bin / swoft", "start"]. The entry start command starts the swoft service
#Start container
docker-compose up -d swoft
#See if the container runs successfully
docker ps
#Enter the container shell
docker exec -it mySwoft bash

Deploy swoft on the host without PHP environment installed

In the first two ways to deploy swoft, you need to install the PHP basic environment on the host computer to use composer to install the dependent components of the local swoft project, so as to mount and start the container with the working directory of the swoft image (because the entry command of the container is to directly start swoft, for example, if we mount the local swoft that does not have the dependent swoft installed Project to the mirror working directory, and the container will fail to start and exit.).

1. Pull swoft (just pull, no installation dependency)

git clone [email protected]:swoft-cloud/swoft.git && cd swoft

Using docker image directly

#- it opens standard input and terminal
#-- entrypoint overrides the default setting of starting swoft service in the image
#- D guard mode
#This allows the container to create a bash as a pre process at startup without starting the swoft service
docker run -it -d -p 80:80 \
-v $(pwd):/var/www/swoft \
--name mySwoft \
--entrypoint="" \
swoft/swoft bash

#Enter container after startup
docker exec -it mySwoft bash
#Using the composer installation in the container depends on that the working directory is already associated with the swoft project of the host
compose intall [--no-dev]
#Start swoft
php bin/swoft start
#Stopping swoft at this time will not cause the container to exit
php bin/swoft stop

Using docker compose

Edit the docker-compose.yml file, open stdin_open (equivalent to – I of docker), TTY (equivalent to – t of docker), and change the entry point to bash.

version: '3'

services:
    swoft:
       container_name: mySwoft
       image: swoft/swoft:latest
#      build: ./
       ports:
         -"8082:80" ා mapping host 8082 port to container 80
       volumes:
         -. /: / var / www / swoft ා mount the current project directory of the host to the working directory of the container / var / www / swoft
       Stdin ﹐ open: true ﹐ be sure to open this item or the container will exit after bash execution
       TTY: true ා open session terminal
       privileged: true
#      entrypoint: ["php", "/var/www/swoft/bin/swoft", "start"]
       Entrypoint: ["bash"] (swoft will not be started by default when the container is started after changing to this command, so even if the framework dependency is not installed, container startup will not be affected

Start the container after saving docker-compose.yml

docker-compose up -d swoft

Enter the container shell and use the container’s composer installation framework dependency

#Enter the container shell
docker exec -it mySwoft bash
#The default working directory of the swoft image / var / www / swoft is mapped with the swoft project directory of the host computer
#Using composer in container to install dependency
composer install [--no-dev]
#Start swoft
php bin/swoft start|stop|restart

In this way, the host computer completely saves the work of installing PHP / composer in advance and makes full use of the off the shelf environment provided by the image~

Recommended Today

External part – Visual Studio code and vue.js

Fan waipian Seaconch is so tired that he can’t help writing this article I can’t help but recommend new editors. I can’t help but use them everywherevs codeThis editor To whom? I don’t know, when the same person is like meFrom vsThen maybe you will feel the same as me~ Start learning in Mayvue.jsUp to […]