Laravel Swoole Docker "There are no commands defined in the "swoole" namespace" - php

So, I want to configure the swoole laravel project. I run a Dockerfile and it successfully run. Then I want to run compose file this give me error
There are no commands defined in the "swoole" namespace.
This is my first experience with swoole. And I don't understand what is the problem.
How can solve this problem?
This is a Dockerfile
FROM php:8.1-fpm-alpine
# Install laravel requirement PHP package
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS libzip-dev sqlite-dev \
libpng-dev libxml2-dev oniguruma-dev libmcrypt-dev curl curl-dev libcurl postgresql-dev
RUN docker-php-ext-install -j$(nproc) gd bcmath zip pdo_mysql pdo_pgsql
RUN pecl install xdebug swoole && docker-php-ext-enable swoole
# Install composer
ENV COMPOSER_HOME /composer
ENV PATH ./vendor/bin:/composer/vendor/bin:$PATH
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer
# Install PHP_CodeSniffer
WORKDIR /app
COPY ./ ./
USER root
RUN chown -R www-data /app/storage
RUN chmod -R ug+w /app/storage
RUN chmod 777 -R /app/storage
RUN chmod 777 -R /app/public
RUN composer install
RUN php artisan optimize
CMD php artisan swoole:http start
EXPOSE 1215
And this is a docker-compose.yaml file
version: "3.7"
services:
app:
build:
args:
user: www-data
uid: 1000
context: ./
dockerfile: Dockerfile
image: topspot-swoole-image
container_name: topspot-swoole-container
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- topspot-network
nginx:
image: nginx:alpine
container_name: topspot-nginx
restart: unless-stopped
ports:
- 80:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- topspot-network
networks:
topspot-network:
driver: bridge

Solved
I solved it. Firstly install the swoole and publish to a local project. Then run the container and the composer saw the Swoole packages.

Related

How do I make my container sync the files I make? (docker)

I'm making a web application using Laravel 7 but I want to develop it in docker, but when I am using my container, the container files are not synchronized with the ones that are shown in the text editor, how can I solve that?
The files that are not synchronized are, for example, the migrations or the composer.json, that is, when I install a package through composer, those changes are not reflected in the text editor
I've tried to put ./:/var/www/html in docker-compose.yml but I get an error when I run it. Warning: require(/var/www/html/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/artisan on line 18 Fatal error: require(): Failed opening required '/var/www/html/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /var/www/html /artisan online 18
Next, I provide my dockerfile and my docker compose.
Dockerfile
FROM composer:1.10.22 as build_stage
LABEL maintainer="Me"
COPY . /src
ADD .env.example /src/.env
WORKDIR /src
RUN composer install --no-ansi --no-interaction --no-progress --no-scripts --optimize-autoloader --ignore-platform-reqs
RUN php artisan key:generate
FROM php:7-fpm-alpine
ENV \
BUILD_PACKAGES="build-base curl-dev linux-headers" \
SQLITE_PACKAGES="zlib-dev libxml2-dev libxslt-dev postgresql postgresql-dev sqlite sqlite-libs sqlite-dev nano php7-gd php7-mysqli php7-zlib php7-curl"
RUN apk update && apk add $BUILD_PACKAGES && apk add $SQLITE_PACKAGES
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN set -ex \
&& apk --no-cache add \
postgresql-dev
RUN docker-php-ext-install pdo pdo_pgsql
COPY --from=build_stage /src /var/www/html
RUN chmod -R 777 /var/www/html
EXPOSE 5000
COPY ./run.sh /tmp
RUN chmod +x /tmp/run.sh
ENTRYPOINT ["/tmp/run.sh"]
docker-compose.yml
version: "3.4"
networks:
project-net:
volumes:
project-datastore:
services:
postgres-db:
image: postgres:11-alpine
volumes:
- project-datastore:/var/lib/postgresql/data
networks:
- project-net
ports:
- "25432:5432"
environment:
POSTGRES_DB: db
POSTGRES_USER: user
POSTGRES_PASSWORD: password
project:
build:
context: ./
image: project:latest
volumes:
- ./app:/var/www/html/app
- ./public:/var/www/html/public
- ./routes:/var/www/html/routes
- ./resources:/var/www/html/resources
depends_on:
- postgres-db
networks:
- project-net
ports:
- "5000:5000"
environment:
DATABASE_URL: postgres://user:password#postgres-db:5432/db
run.sh
cp /usr/share/zoneinfo/America/Mexico_City /etc/localtime
php artisan serve --host=0.0.0.0 --port=5000

docker entrypoint sh file restarting

I am testing docker with my php project. Everything is ok in testing but if I add ENTRYPOINT, docker is restarting.
Here is my docker compose file
version: "3.7"
services:
#Laravel App
app:
build:
args:
user: maruan
uid: 1000
context: ./docker/7.4
dockerfile: Dockerfile
# command: sh -c "start-container.sh"
image: laravel-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- app-network
#Nginx Service
nginx:
image: nginx:alpine
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker/7.4/nginx/conf.d:/etc/nginx/conf.d/default.conf
networks:
- app-network
#Mysl Service
db:
image: mysql:8
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
networks:
- app-network
networks:
app-network:
driver: bridge
Dockerfile
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
WORKDIR /var/www
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Install system dependencies
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential mariadb-client libfreetype6-dev libjpeg-dev libpng-dev libwebp-dev zlib1g-dev libzip-dev gcc g++ make vim unzip git jpegoptim optipng pngquant gifsicle locales libonig-dev \
&& docker-php-ext-configure gd \
&& docker-php-ext-install gd \
&& apt-get install -y --no-install-recommends libgmp-dev \
&& docker-php-ext-install gmp \
&& docker-php-ext-install mysqli pdo_mysql zip \
&& docker-php-ext-enable opcache \
&& apt-get autoclean -y \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /tmp/pear/
COPY . /var/www
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
COPY start-container.sh /usr/local/bin/start-container.sh
RUN chmod +x /usr/local/bin/start-container.sh
ENTRYPOINT ["start-container.sh"]
start-container.sh file
#!/usr/bin/env bash
set -e
cd /var/www
php artisan optimize
php artisan view:cache
#composer install && composer dump-autoload
exec "$#"
I also print log for that docker image.
Configuration cached successfully!
Route cache cleared!
Routes cached successfully!
Files cached successfully!
Compiled views cleared!
Blade templates cached successfully!
I think my error is docker container is restarting after running start-container.sh file. When I google, some people use PHP artisan script with ENTRYPOINT sh file.
What should I do not to restart again and again with ENTRYPOINT sh file?
Your entrypoint script ends with the line exec "$#". This runs the image's CMD, and is generally a best practice. However, your image doesn't have a CMD, so that command just expands to a bare exec, which causes the main container process to exit.
An image built FROM php:fpm often won't have a CMD line since the base image's Dockerfile specifies CMD ["php-fpm"]; it is enough to COPY your application code into a derived image, and the base image's CMD knows how to run it. However, setting ENTRYPOINT in a derived image resets the CMD from the base image (see the note in the Dockerfile documentation discussing CMD and ENTRYPOINT together). This means you need to repeat the base image's CMD:
ENTRYPOINT ["start-container.sh"]
CMD ["php-fpm"] # duplicated from base image, because you reset ENTRYPOINT

PHP container build in docker fails with error

I am writing a small pet project and using docker
I am using PHP-CLI, PHP-FPM and PGSQL - apline.
Below is the configuration
php-cli Dockerfile
FROM php:8.0-cli-alpine
RUN apk add --no-cache autoconf g++ make
#install PG
RUN apk add --no-cache postgresql-dev bash coreutils \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
RUN apk add --no-cache unzip
RUN docker-php-ext-install pdo_pgsql pcntl opcache
RUN docker-php-ext-install bcmath fileinfo
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/bin --filename=composer --quiet
WORKDIR /app
PHP-fpm dockerfile
FROM php:8.0-fpm-alpine
#install PG
RUN apk add --no-cache postgresql-dev bash coreutils \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
#enable extensions
RUN docker-php-ext-install pdo_pgsql opcache bcmath fileinfo
WORKDIR /app
docker-compose.yaml file
version: '3.8'
services:
nginx:
container_name: seln_nginx_dev-container
image: nginx
volumes:
- ./docker/nginx/:/etc/nginx/conf.d
- ./:/app
links:
- php-fpm
ports:
- '8000:80'
php-fpm:
container_name: seln_php-fpm-container
build:
context: docker
dockerfile: php-fpm/Dockerfile
environment:
DB_HOST: api-postgres
DB_USER: app
DB_PASSWORD: secret
DB_NAME: app
ports:
- 8080:8080
volumes:
- ./:/app
working_dir: /app
php-cli:
container_name: seln_php-cli-container
build:
context: docker
dockerfile: php-cli/Dockerfile
environment:
DB_HOST: api-postgres
DB_USER: app
DB_PASSWORD: secret
DB_NAME: app
volumes:
- ./:/app
api-postgres:
image: postgres:13.1-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
POSTGRES_DB: app
ports:
- "127.0.0.1:5432:5432"
And Makefile
init: down build up composer-install init-db
up:
docker-compose -f docker-compose.yaml up -d
down:
docker-compose -f docker-compose.yaml down -v --remove-orphans
composer-install:
docker-compose -f docker-compose.yaml run --rm php-cli composer install
composer-update:
docker-compose -f docker-compose.yaml run --rm php-cli composer update
build:
docker-compose -f docker-compose.yaml build
init-db:
docker-compose -f docker-compose.yaml run --rm php-cli vendor/bin/doctrine orm:schema-tool:drop --force && \
docker-compose -f docker-compose.yaml run --rm php-cli vendor/bin/doctrine orm:schema-tool:create
clear-cache:
docker-compose -f docker-compose.yaml run --rm php-cli vendor/bin/doctrine orm:clear-cache:metadata && \
docker-compose -f docker-compose.yaml run --rm php-cli vendor/bin/doctrine orm:clear-cache:query
What I am running into, when trying to build docker to work, I get an error use make init command
make: /bin/sh: Operation not permitted
make: *** [Makefile:209: pdo_pgsql.lo] Error 127
The command '/bin/sh -c docker-php-ext-install pdo_pgsql opcache bcmath fileinfo' returned a non-zero code: 2
ERROR: Service 'php-fpm' failed to build : Build failed
what is the reason for this error?
P>S - I managed to fix the error(use FROM php:8.0-cli-alpine3.13), but this solution does not suit me, I want to understand the reason, before the Ubuntu update, I could collect images without crutches, what could have changed in two weeks?

'mysql: not found', trying to connect a laravel, mysql and nginx in docker container

Ubuntu 20.04
Docker version 19.03.14
docker-compose version 1.25.0
I'm trying to figure out why i'm getting an error related to mysql. The steps i did:
Clone my laravel project
docker-compose build
docker-compose up -d
docker-compose exec app php artisan key:generate
docker-compose exec app php artisan config:cache
docker-compose exec app php artisan migrate
in the last one i get the following error:
Migration table created successfully.
Loading stored database schema: /var/www/database/schema/mysql-schema.dump
Symfony\Component\Process\Exception\ProcessFailedException
The command "mysql --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"" failed.
Exit Code: 127(Command not found)
Working directory: /var/www
Output:
================
Error Output:
================
sh: 1: mysql: not found
at vendor/symfony/process/Process.php:257
253▕ */
254▕ public function mustRun(callable $callback = null, array $env = []): self
255▕ {
256▕ if (0 !== $this->run($callback, $env)) {
➜ 257▕ throw new ProcessFailedException($this);
258▕ }
259▕
260▕ return $this;
261▕ }
+19 vendor frames
20 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
My files:
docker-compose.yml
version: '3.7'
services:
# The Application
app:
build:
context: .
dockerfile: docker-files/app.dockerfile
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
volumes:
- ./:/var/www
- ./docker-files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
env_file: '.env'
networks:
- app-network
# The Web Server
nginx:
container_name: nginx
restart: unless-stopped
tty: true
build:
context: .
dockerfile: docker-files/web.dockerfile
volumes:
- ./:/var/www
- ./docker-files/nginx/conf.d/:/etc/nginx/conf.d/
ports:
- "8990:80"
- "443:443"
networks:
- app-network
# The Database
db:
container_name: db
restart: unless-stopped
tty: true
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql/
- ./docker-files/mysql/my.cnf:/etc/mysql/my.cnf
environment:
MYSQL_DATABASE: maindbs
MYSQL_USER: root
MYSQL_PASSWORD: 123456
MYSQL_ROOT_PASSWORD: 123456
ports:
- "3306:3306"
networks:
- app-network
#Volumes
volumes:
dbdata:
driver: local
#Docker Networks
networks:
app-network:
driver: bridge
app.dockerfile
FROM php:7.4-fpm
COPY composer.lock composer.json /var/www/
COPY database /var/www/database
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libzip-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy application folder
COPY . /var/www
RUN chown -R www-data:www-data \
/var/www/storage \
/var/www/bootstrap/cache
RUN php artisan optimize
EXPOSE 9000
CMD ["php-fpm"]
web.dockerfile
FROM nginx:alpine
ADD docker-files/nginx/conf.d/app.conf /etc/nginx/conf.d/default.conf
COPY public /var/www/public
.env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=maindbs
DB_USERNAME=root
DB_PASSWORD=123456
I can connect to laravel app using 127.0.0.1:8990
You don't have mysql client in the container that tries to execute mysql command.
Add mysql via the Dockerfile.
RUN apt-get update && apt-get install -y mysql-client && rm -rf /var/lib/apt

Docker with Laravel

I am setting up a development environment with the docker. In it I need
NGINX (webservice)
PHP 7.2 (app)
MARIADB 10.3 (database)
In the app container I must install composer, I will work using laravel.
Using RUN composer install andCOPY .env.example .env in dockerfile to install vendor and configure.env, I did not get any error messages but no files were created either.
docker-compose.yml
version: '3'
services:
app:
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
build:
context: .
dockerfile: Dockerfile
image: php:7.2
container_name: app
restart: unless-stopped
tty: true
working_dir: /var/www
networks:
- app-network
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
networks:
- app-network
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
db:
image: mariadb:10.3
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: db
MYSQL_ROOT_PASSWORD: pass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
volumes:
- dbdata:/var/lib/mysql
networks:
app-network:
driver: bridge
volumes:
dbdata:
driver: local
dockerfile
FROM php:7.2-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
mariadb-client-10.3 \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Run composer install
RUN composer install
# Create the .env from the .env.example
COPY .env.example .env
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
I have created a Setup to deploy a Laravel Application with the help of NGINX Web Server and MYSQL Database at Github.
It should be pretty easy to set up. If you encounter any issues, please feel free to raise an issue at Github, or ask me here.

Categories