How to setup laravel with npm using docker-compose? - laravel

I have the next docker-compose.yml file
version: '2'
services:
# The Application
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www/html
volumes:
- ./giftmeabetterplanet:/var/www/html
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
# The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www/html
links:
- database:mysql
volumes_from:
- app
ports:
- 81:80
environment:
- "WEB_DOCUMENT_ROOT=/var/www/html/public"
# The Database
database:
image: mysql:5.6
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=homestead"
- "MYSQL_USER=homestead"
- "MYSQL_PASSWORD=secret"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "3306"
volumes:
dbdata:
I need to make NPM (node package manager) accessible somehow to build my JS and CSS files in 'web' container.
app.dockerfile
FROM php:7.0.4
RUN apt-get update && apt-get install -y libmcrypt-dev \
mysql-client libmagickwand-dev --no-install-recommends \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install mcrypt pdo_mysql
web.dockerfile
FROM webdevops/php-apache-dev:ubuntu-16.04
I've tried the next way by extending web.dockerfile
FROM orlandohohmeier/local-npm
FROM webdevops/php-apache-dev:ubuntu-16.04
But npm is not accessible from the command line in 'web' container. Maybe i don't understand some concepts but i just want to compile my stiles, javascript files and copy fonts from node-modules.
Best regards. Ivan

For PHP7.2 this is what I have in my php.dockerfile
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs

Add to the docker-compose file
npm:
image: node:14
working_dir: /var/www/my_shiny_project
entrypoint: ["npm"]
volumes:
- "./www/:/var/www/my_shiny_project"
Usage:
docker-compose run --rm npm install
Don't forget to check the paths in both working_dir and volumes

Try adding this to your web.dockerfile.
RUN apt-get install -y nodejs npm
And then build your containers and try running node or npm within the web container
docker-compose run web bash

Follow this tutorial: https://medium.com/#wiwatSrt/laravel-development-environment-in-docker-containers-7b173f62f372
When docker-compose is set up, run command:
docker-compose run --rm nodejs npm install

Related

What are the best practices for using cronjob along with docker and laravel?

I ran into a problem that I have to manually write a cronjob in a production environment inside Ubuntu, I would like to have a completely standalone docker project. There is a solution with creating an ubuntu container, but it looks weird, ubuntu inside ubuntu. So far, I've had to set up a cronjob outside of a container on Ubuntu itself.
Dockerfile
FROM php:8.1-fpm
RUN apt-get update && apt-get install -y \
apt-utils \
libpq-dev \
libpng-dev \
libzip-dev \
zip unzip \
git && \
docker-php-ext-install pdo_mysql && \
docker-php-ext-install bcmath && \
docker-php-ext-install gd && \
docker-php-ext-install zip && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
COPY ./_docker/app/php.ini /usr/local/etc/php/conf.d/php.ini
# Install composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN curl -sS https://getcomposer.org/installer | php -- \
--filename=composer \
--install-dir=/usr/local/bin
WORKDIR /var/www
docker-compose.yml
version: '3'
services:
nginx:
image: nginx:latest
volumes:
- ./:/var/www/
- ./_docker/nginx/conf.d/:/etc/nginx/conf.d/
ports:
- 8876:80
container_name: flowers_nginx
depends_on:
- flowers
flowers:
build:
context: .
dockerfile: _docker/app/Dockerfile
volumes:
- ./:/var/www/
depends_on:
- database
container_name: flowers_php
database:
image: mysql:8.0
restart: always
volumes:
- ./temp/db:/var/lib/mysql
environment:
MYSQL_DATABASE: ****
MYSQL_ROOT_PASSWORD: *****
MYSQL_USER: ****
MYSQL_PASSWORD: *****
ports:
- 8101:3306
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
container_name: flowers_mysql
Due to lack of experience, I need a hint with the best solution, maybe use kubernetes-cronjob or redis, but I have not yet learned these technologies, I will be grateful if you help me choose the right solution and development path.
I tried to do the following inside Ubuntu itself which has docker installed and it works, but because of this I can't make a completely standalone docker project without connecting to the ubuntu job which has docker installed
crontab -e
* * * * * cd /var/www/public/flowers_shop && php artisan schedule:run >> /dev/null 2>&1

Commands in Dockerfile has no effect in container (Docker)

I am deploying dockerized application to digitalocean server. (Ubuntu 22.04)
But I don't want to pass vendor directory there. To create it in remote server I am trying to use composer update method in my application's container.
docker-compose.yml:
version: "3.9"
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
Dockerfile:
FROM php:8.1-fpm as php
USER root
WORKDIR /var/www
# Install PHP packages
RUN apt-get update && apt-get install -y \
git \
curl \
libreoffice \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
libonig-dev \
libzip-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install mbstring zip exif pcntl
RUN docker-php-ext-configure gd --enable-gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
# Copy existing application directory contents
COPY . /var/www
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer update
RUN composer dump-autoload
RUN chmod -R 755 storage/
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
When I run docker build and up commands everything working fine without any error. But my application throws an error because it can't find vendor directory in application.
As you see in Dockerfile I am running composer update command. It is working while building and gives me some logs:
But when I log in my container with docker exec -it app /bin/bash command, I can't see any vendor directory created in /var/www. I am able to run composer update in container manually and it is not giving any error.
Main problem is why composer update is not creating vendor directory while givings logs. Even RUN chmod -R 755 storage/ command is not giving defined permissions to folder. I feel like Dockerfile has no effect on my container except installing packages and extensions.

Laravel projet from git inside docker container

I've been trying to create a docker compose and docker file for my laravel project. The closes one I get is
Could not open input file: artisan
This is my docker-compose.yml:
version: "3"
services:
webserver:
hostname: webserver
container_name: webserver
image: php:8
restart: always
build:
context: ./webserver
volumes:
- /home/dockerized/laravel-exp/:/app
ports:
- 21280:8000
networks:
- dev_networks
depends_on:
- phpmyadmin
db:
#other service base in mariadb.
phpmyadmin:
#could use adminer.
networks:
dev_networks:
name: dev_networks
With dockerfile as such:
FROM php:8
RUN apt-get update -y && apt-get install -y openssl zip unzip git apt-utils libonig-dev
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo mbstring
WORKDIR /app
COPY . /app
RUN rm -rf /app/*
RUN git clone https://github.com/.../myFproject.git /app && chmod +x /app/artisan
WORKDIR /app
RUN composer install
WORKDIR /app
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
I've using bitnami image but still no avail.

How to run laravel job queue in php-fpm image on docker

I have a container with a nginx, mailhog, redis and PHP image. All these images are on the same network.
I run Laravel on the PHP image.
I want to make use of the Job queue that laravel has, but I am struggling to run the queue in the PHP image.
I've looked at all the examples but it seems my lack of understanding of docker is causing me to not ask the right question
Below is my docker-compose.yml
version: '3'
networks:
devnet:
external: true
services:
# lightweight web-server:
nginx:
image: nginx:stable-alpine
container_name: lar-nginx
ports:
- 8080:80
- 4040:443
volumes:
- ./:/var/www
- ./run/nginx:/etc/nginx/conf.d
- ./local/certs:/etc/nginx/certs
depends_on:
- php
networks:
- devnet
# server-side scripting engine
php:
build:
context: .
dockerfile: Dockerfile
container_name: lar-php
volumes:
- ./:/var/www
ports:
- "9000:9000"
networks:
- devnet
# caching server:
redis:
image: redis:latest
container_name: lar-redis
ports:
- "6379:6379"
networks:
- devnet
# development email catch-all server & client:
mailhog:
image: mailhog/mailhog:latest
container_name: lar-mailhog
ports:
# imap port for send mail
- "1025:1025"
# www mailhog ui
- "8025:8025"
networks:
- devnet
Dockerfile
FROM php:7.4-fpm
RUN apt-get update
RUN apt-get -y install curl gnupg cron
# RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
# RUN apt-get -y install nodejs
# RUN npm install
# Install other required PHP extensions and unix utils:
RUN apt-get update && apt-get install -y libmcrypt-dev \
mariadb-client libmagickwand-dev libonig-dev \
libzip-dev libcurl4-openssl-dev redis-server \
zlib1g-dev wget git \
--no-install-recommends \
# && pecl install imagick
# && docker-php-ext-enable imagick
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install mbstring \
&& docker-php-ext-install zip \
&& docker-php-ext-install xml \
&& docker-php-ext-install curl \
&& docker-php-ext-install gd \
&& docker-php-ext-install soap
# Configure PHP internal vars:
ENV PHP_MEMORY_LIMIT=256M
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install php apcu pecl package:
RUN pecl install apcu && docker-php-ext-enable apcu
# Install php redis pecl package:
RUN pecl install redis && docker-php-ext-enable redis
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
# Permissions for Laravel
RUN chown -R www-data:www-data /var/www
RUN chmod -R 777 /var/www
COPY entrypoint.bash /usr/sbin
RUN chmod a+x /usr/sbin/entrypoint.bash
ENTRYPOINT /usr/sbin/entrypoint.bash
entrypoint.bash
#!/bin/bash
# turn on bash's job control
set -m
# Start the "main" PHP process and put it in the background
php-fpm &
# Start the helper crond process
crond
# now we bring the primary process back into the foreground
fg %1
In normal server(lamp) environment its pretty simple to work with cronjobs and queue but I dont know how to start up the queue.
php artisan queue:work in the php image returs There are no commands defined in the "queue:" namespace. Did you mean this? queue
Running it in tinker
\Queue::pushON('new', new App\Jobs\PublishingClass(array('foo'=>1,'foobar'=>783,'foobarfoo'=>33)));
show the job gets processed but I need to do it with a process running in the background
The most simple way is to call with the use of Tinker
It's Laravel command using for debugging, use it by running below command from from project root
php artisan tinker
To dispatch job on a specific queue from tinker
\Queue::pushON('rms', new App\Jobs\UpdateRMS());
first parameter - Queue name
second parameter - job name
Dispatch multiple jobs at once to a specific queue
\Queue::bulk([new App\Jobs\UpdateRMS(), new App\Jobs\UpdateRMS()], null, 'rms');
You can use this docker image, you don't need to configure the schedule, it's already implemented, with differents php expansions like redis, Rdkafka.
Follow this link:
https://hub.docker.com/r/jkaninda/laravel-php-fpm
https://github.com/jkaninda/laravel-php-fpm

Docker compose work on linux environment but not windows environment

I am using 2 environment for development one is a linux VM at home while another is the windows laptop at office. The dockerfile of angular environment work fine until a few days ago, it show the following error when I tried to start the docker container with docker compose on the laptop:
ng | /bin/sh: 1: sudo: not found
ng exited with code 127
However, the same issue does not occurs on my linux VM.
Dockerfile:
FROM node:12
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable
RUN mkdir /app
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json package-lock.json /app/
RUN npm install
#RUN npm install -g #angular/cli
COPY . /app
EXPOSE 4200
CMD ng serve --host 0.0.0.0
docker-compose.yaml:
version: "3"
services:
dj:
container_name: dj
build: Backend
command: python manage.py runserver 0.0.0.0:80
volumes:
- ./Backend:/code
ports:
- "80:80"
ng:
container_name: ng
build: Frontend/SPort
volumes:
- ./Frontend/SPort:/app
ports:
- "4200:4200"
I think you want to fix the sh script in your Dockerfile
add this:
RUN apt-get update && apt-get install -y dos2unix && dos2unix /path/to/the/script
hope that will help since the error comes from CRLF characters in windows

Resources