how to run command in docker-compose.yml file? - php

i have laravel project and i want to create docker for it
and i want to automation it
i want to run this commends:
{
php artisan key:generate
php artisan migrate
php artisan queue:work --daemon
}
and its my docker-compose.yml file
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: xxxx/lumen:Library
container_name: Library
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: LibraryWebserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: Librarydb
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: library
MYSQL_ROOT_PASSWORD: Library!23
MYSQL_USER: root
MYSQL_PASSWORD: Library!23
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
volumes:
- dbdata:/var/lib/mysql
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
i try to use command: php artisan migrate and its do nothing
and also use
command: bash -c "php artisan migrate"
command: 'php artisan migrate'

You can use a command like the below example. It will run the command once the container is up and running.
version: "3.0"
services:
app:
image: busybox
command: top
networks:
app_net:
link_local_ips:
- 57.123.22.11

Related

environment problem in yml file for docker

i make an error on the point youmas:
environment:
XDEBUG_CONFIG: remote_host=172.17.0.1 remote_port=9000 remote_enable=1
networks:
- youmas
but i dont know how to fix them
version: "3"
services:
#PHP
youmas-app:
build:
context: .
dockerfile: Dockerfile
args:
- WITH_XDEBUG=true
image: youmas-app
container_name: youmas-app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS:
dev
# If you down want to use xDebug, set remote_enable=0
PHP_IDE_CONFIG: "serverName=Docker"
XDEBUG_CONFIG: remote_host=192.168.178.33 remote_port=9001
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/production/php/local.ini:/usr/local/etc/php/conf.d/local.ini
youmas:
environment:
XDEBUG_CONFIG: remote_host=172.17.0.1 remote_port=9000 remote_enable=1
networks:
- youmas
#Nginx
youmas-webserver:
build:
context: .
dockerfile: "./docker/production/nginx/Dockerfile"
container_name: youmas-webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/production/nginx/conf.d/:/etc/nginx/conf.d/
- ./docker/production/certbot/conf:/etc/letsencrypt
- ./docker/production/certbot/www:/var/www/certbot
networks:
- youmas
command: '/bin/sh -c ''while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g "daemon off;"'''
# Certbot SSL
certbot:
image: certbot/certbot
volumes:
- ./docker/production/certbot/conf:/etc/letsencrypt
- ./docker/production/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
#MySQL
youmas-database:
image: mysql:5.7.22
container_name: youmas-database
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: youmas
MYSQL_ROOT_PASSWORD: K4#|ahdpof8##!~
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./docker/production/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- youmas
#PHP My Admin
youmas-phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "7000:80"
links:
- youmas-database:youmas-database
environment:
MYSQL_USER: root
MYSQL_PASSWORD: codeverze
MYSQL_ROOT_PASSWORD: codeverze
networks:
- youmas
#Networks
networks:
youmas:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
any solution?
Your Syntax is wrong,
Replace the environment section to look like this :
youmas-app:
build:
context: .
...
...
...
environment:
- 'SERVICE_NAME=app'
- 'SERVICE_TAGS=dev'
...
...

What is wrong with my Laravel docker compose File?

Can anybody help me with the following? I have a docker-compose file with the following services PHP, Mysql, Redis, and Nginx. The docker-compose file builds without any issues but when I try the URL its not working. The file is down below
version: '3.8'
services:
php:
build: ./docker-compose/php-81/build
image: php-81
container_name: cumax-php-db
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
- ./docker-files/storage/logs/php:/var/www/default/logs
networks:
- onboarding
db:
image: mysql:8.0
container_name: cumax-mysql-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql8.0/storage:/var/lib/mysql
networks:
- onboarding
nginx:
image: nginx:1.17-alpine
container_name: cumax-nginx
restart: unless-stopped
ports:
- "8070:80"
volumes:
- ./:/var/www
- ./docker-compose/nginx/conf.d:/etc/nginx/conf.d
networks:
- onboarding
redis:
image: "redis:alpine"
ports:
- "6379:6379"
networks:
- onboarding
networks:
onboarding:
On php you don't publish the port.
On the Nginx you only publish to port 8070 on the host.
What is the URL you are using? It should be http://< host >:8070/
And then the nginx should proxy onwards to php.

Why do extra folders get created in laravel public folder when i use docker as a dev environment and how do i stop it

In the image above the extra folders in the public folder only appears when ever i set up a dev environment with docker, the logs go to these folders instead of the /storage/logs/.
Another thing i experience when i use docker dev environment is i always have to reload cache (artisan optimize) before my config or route changes can be applied in the container.
THIS IS MY Docker-Compose FILE
version: "3"
networks:
laravel:
services:
nginx:
container_name: testproj-nginx
image: nginx:stable-alpine
ports:
- "2022:80"
volumes:
- ./:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8
container_name: testproj-mysql
restart: unless-stopped
tty: true
ports:
- "2306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: testproj
MYSQL_USER: root_user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build: ./
container_name: testproj-php
volumes:
- ./:/var/www/html
networks:
- laravel
mailhog:
image: "mailhog/mailhog:latest"
container_name: testproj-mailhog
ports:
- "${FORWARD_MAILHOG_PORT:-2025}:1025"
- "${FORWARD_MAILHOG_DASHBOARD_PORT:-9025}:8025"
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: testproj-pma
links:
- mysql
environment:
PMA_HOST: testproj-mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8083:80
networks:
- laravel
depends_on:
- php
- mysql
# ngrok:
# image: wernight/ngrok:latest
# container_name: testproj-ngrok
# ports:
# - 4041:4040
# environment:
# NGROK_AUTH: ${NGROK_AUTH_KEY}
# NGROK_REGION: eu
# NGROK_PROTOCOL: http
# NGROK_PORT: testproj-nginx:80
# networks:
# - laravel
# depends_on:
# - nginx

How to run queue work on Laravel with docker?

I create an docker setup on my Laravel project using sail.
În my project I use jobs and I want when I run docker-compose up to start command php artisan queue:work. In my docker-compose.yml I defined an service php-queue like this:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
php-queue:
restart: always
image: sail-8.0/app:latest
command: 'php artisan queue:work'
volumes:
- '.:/var/www/html'
mysql:
image: 'mysql:5.7'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: [ "CMD", "redis-cli", "ping" ]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
when I run docker-conpose up in docker desktop it shows this error:
error: failed switching to "php": unable to find user php: no matching entries in passwd file
what I missed?
change command: 'php artisan queue:work' to command: ['/bin/sh', '-c', 'php artisan queue:work' thats should work
Also this could help you :)
https://laravel.com/docs/8.x/sail#executing-sail-commands
Running Artisan commands locally...
php artisan queue:work
Running Artisan commands within Laravel Sail...
sail artisan queue:work

How to access host machine's mongo db inside docker container [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(41 answers)
Closed 2 years ago.
I am a bit new to docker. I am developing a laravel application, I have my app container running.
Inside app container I want to connect to my locally hosted mongo db server, which is generally localhost:27017.
This is my docker-compose.yml file
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: bns_app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker_files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: bns_web
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker_files/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: bns_db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: 12345
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./docker_files/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#REDIS Service
redis:
build:
context: ./docker_files/redis
dockerfile: Dockerfile
container_name: bns_redis
volumes:
- ./docker_files/redis:/data
ports:
- "6379:6379"
networks:
- app-network
# Laravel Echo Server
laravel-echo-server:
build:
context: ./docker_files/laravel-echo-server
dockerfile: Dockerfile
container_name: bns_echo_server
volumes:
- ./laravel-echo-server.json:/var/www/laravel-echo-server.json:ro
ports:
- "6001:6001"
links:
- redis
networks:
- app-network
# PHP-WORKER
php-worker:
build:
context: ./docker_files/php-worker
dockerfile: Dockerfile
container_name: bns_worker
volumes:
- ./php-worker/supervisord.d:/etc/supervisord.d
depends_on:
- app
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
So inside my app container when I run artisan commands such as docker-compose exec app php artisan migrate:fresh --seed mysql tables are migrated successfully but mongo db (which is not in any container but it is in host machine only) documents are not migrate/seeded and I get error :
No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on '172.18.0.3:27017']
at /var/www/vendor/mongodb/mongodb/src/functions.php:431
427| // TODO: PHPLIB-476: Read transaction read preference once PHPC-1439 is implemented
428| $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
429| }
430|
> 431| return $manager->selectServer($readPreference);
432| }
433|
Exception trace:
1 MongoDB\Driver\Manager::selectServer(Object(MongoDB\Driver\ReadPreference))
/var/www/vendor/mongodb/mongodb/src/functions.php:431
2 MongoDB\select_server(Object(MongoDB\Driver\Manager), [])
/var/www/vendor/mongodb/mongodb/src/Database.php:419
Please use the argument -v to see more details.
This is my ENV file:
MONGO_DATABASE=exchange
MONGO_HOST=172.18.0.3 # << This is the IP address of my app container <<
MONGO_PORT=27017
MONGO_USERNAME=
MONGO_PASSWORD=
Please do help me. Where am I going wrong ?
I don't know how but using static ip as 172.17.0.1 worked for me.
Now my ENV file is:
MONGO_DATABASE=exchange
MONGO_HOST=172.17.0.1
MONGO_PORT=27017
MONGO_USERNAME=
MONGO_PASSWORD=
Got reference from here
It must be well documented.

Categories