Docker ENTRYPOINT bash script is executed over and over again - shell

Everything in my docker container initialisation goes well except when I run an ENTRYPOINT script at the end of my Dockerfile with
# ...
ENTRYPOINT ["bash", "./shell_scripts/init.sh"]
which consists of
#!/bin/bash
echo "Init app..."
composer update
composer dump-autoload
php artisan migrate
and when I run docker-compose up --build it keeps running the script over and over again....
docker-compose.yml
version: '3.7'
services:
mysql_db:
image: mysql:8.0.13
container_name: mysql_8.0.13
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
tty: true
ports:
- 3307:3306
environment:
SERVICE_TAGS: dev
SERVICE_NAME: mysql
MYSQL_ROOT_PASSWORD: mypass
networks:
- app-network
app_n_php:
build:
context: .
dockerfile: Dockerfile
container_name: app_php_7.3-rc-fpm
volumes:
- type: bind
source: ./app
target: /var/www/app
restart: unless-stopped
tty: true
ports:
- 8001:8000
depends_on:
- mysql_db
environment:
SERVICE_NAME: app_n_php
SERVICE_TAGS: dev
networks:
app-network:
driver: bridge
Any idea what is going on?

Related

Trying to run Vite inside Docker container

I'm new in Docker and JS technologies so don't judje too hard. I'm trying to run npm run dev from node.js container (called npm). It goes right and shows that css and scripts are available at localhost:3009, but they're not.
Am I right that the problem is with node.js container, which is trying to restart all the time and doesn't exposes any ports? If yes, how can I fix it? Thank you in advance.
Here is my docker-compose.yml and vite.config.js
version: '3'
networks:
laravel:
services:
site:
build:
context: ./dockerfiles
dockerfile: nginx.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: nginx
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
- redis
- mysql
- mailhog
networks:
- laravel
mysql:
image: mariadb:10.6
container_name: mysql
restart: unless-stopped
tty: true
volumes:
- ./db-data:/var/lib/mysql
ports:
- 3306:3306
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: php
restart: unless-stopped
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
redis:
image: redis:alpine
container_name: redis
restart: unless-stopped
ports:
- 6379:6379
networks:
- laravel
composer:
build:
context: ./dockerfiles
dockerfile: composer.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:latest
container_name: npm
restart: always
volumes:
- ./src:/var/www/html
ports:
- 3000:3000
- 3001:3001
- 3009:3009
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: ./dockerfiles
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
container_name: artisan
volumes:
- ./src:/var/www/html:delegated
depends_on:
- mysql
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
mailhog:
image: mailhog/mailhog:latest
container_name: mailhog
ports:
- 1025:1025
- 8025:8025
networks:
- laravel
vite.config.js
import {defineConfig} from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
server: {
port: 3009
},
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
});
You should use the "--host" key starting your Vite

Laravel with docker on RPI - Failed to open stream: Permission denied

I am testing deployment with docker on RPI but getting "Failed to open stream: Permission denied"
I tried few solutions but none of them worked for me
Permission Denied Error using Laravel & Docker
Laravel & Docker: The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
my Dockerfile:
FROM php:8.0-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN chown -R www-data:www-data /var/www
RUN chmod -R 755 .
and docker-compose.yml
version: "3.7"
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
restart: unless-stopped
container_name: nginx
ports:
- "80:80"
volumes:
- ./:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mariadb
networks:
- laravel
mariadb:
image: yobasystems/alpine-mariadb:latest
restart: unless-stopped
container_name: mariadb
tty: true
ports:
- "4306:3306"
volumes:
- ./mariadb:/var/lib/mariadb
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: laravel_dev
MYSQL_USER: laravel_dev
MYSQL_PASSWORD: laravel_dev
SERVICE_TAGS: dev
SERVICE_NAME: mariadb
networks:
- laravel
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
restart: unless-stopped
ports:
- 4380:80
environment:
PMA_ARBITRARY: 1
PMA_PORT: 3306
PMA_HOST: mariadb
PMA_USER: laravel_dev
PMA_PASSWORD: laravel_dev
depends_on:
- mariadb
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
volumes:
- ./:/var/www/html
working_dir: /var/www/html
networks:
- laravel
npm:
image: node:current-alpine3.14
container_name: npm
volumes:
- ./:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: .
dockerfile: Dockerfile
container_name: artisan
volumes:
- ./:/var/www/html
depends_on:
- mariadb
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
Only thing that worked was chmod 777 on project root folder but that is not the solution i would be comfortable with using.
So i found a solution on reddit
https://www.reddit.com/r/docker/comments/hjsipd/permission_denied_with_volumes/fwoixqe/
I had to sign project folder to container user, which i found with
less /etc/passwd. In this case name of user was www-data with UID 82. And command chown -R 82:82 .

Problems dockerizing Laravel application - database hosts array is empty

I'm trying to dockerize existing Laravel + Vue.js application, however, when trying to access database getting an error:
Database hosts array is empty.
It looks like that connection fails from PHP to MYSQL (but MySQL image is running and I can connect to it from terminal). Not sure how to proceed with this. It doesn't look to me a misconfiguration. Is there a way to debug it?
Here is what I have:
docker-compose.yml:
version: '3'
networks:
backoffice:
services:
backoffice_nginx:
image: nginx:stable
container_name: backoffice_nginx
restart: unless-stopped
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- backoffice_php
networks:
- backoffice
backoffice_mysql:
image: mysql:8.0.13
container_name: backoffice_mysql
restart: unless-stopped
tty: true
ports:
- "33067:3306"
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--default-authentication-plugin=mysql_native_password']
environment:
MYSQL_DATABASE: my_db
MYSQL_USER: dbuser
MYSQL_PASSWORD: mypass
MYSQL_ROOT_PASSWORD: myrootpass
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./mysql/db:/var/lib/mysql
- ./mysql/conf.d:/etc/mysql/conf.d
networks:
- backoffice
backoffice_php:
build:
context: .
dockerfile: php.dockerfile
container_name: backoffice_php
restart: unless-stopped
volumes:
- ./src:/var/www/html
networks:
- backoffice
backoffice_composer:
image: composer:latest
container_name: backoffice_composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- backoffice_php
networks:
- backoffice
backoffice_artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: backoffice_artisan
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
networks:
- backoffice
php.dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/html
RUN docker-php-ext-install bcmath pdo pdo_mysql
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis
DB configs part in the .env:
DB_CONNECTION=mysql
DB_HOST=backoffice_mysql
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=myrootpass
Here are running containers:
Any ideas?
Thanks.
You are not sharing your .env file with your container. Map your whole . Volume to the container instead of only your ./src directory.

Connection refused when running docker-compose exec app php artisan migrate

I'm getting PDOException::("SQLSTATE[HY000] [2002] Connection refused") when running docker-compose exec app php artisan migrate on the terminal
here's my db config on .env file
DB_CONNECTION=mysql
DB_HOST=172.20.0.4
DB_PORT=3306
DB_DATABASE=queue_db
DB_USERNAME=root
DB_PASSWORD=root
I also inspect the container ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' worker-db
Output: 172.20.0.4
here's my docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./docker/Dockerfile
image: digitalocean.com/php
container_name: worker-app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: worker-webserver
restart: unless-stopped
tty: true
ports:
- "8090:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/config/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql
container_name: worker-db
# restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: queue_db
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
There's a weird error on the stack trace where ip is different i'm not sure why it's getting a different ip, but in my .env file i'm using the container ip which is "172.20.0.4"
Exception trace:
1 PDOException::("SQLSTATE[HY000] [2002] Connection refused")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=172.20.0.3;port=3306;dbname=queue_db", "root", "root", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Thanks in advance.
ok, after i tried to run php artisan config:clear it's working now.

Docker postgres container loses data that should be stored in volume

I am running a postgres database generated by the below docker-compose file on Windows. Before running docker-compose up --build, I created a docker volume with docker volume --name postgresdata --driver local. The latter is done to avoid mounting a Windows folder into Postgres.
However, when I run docker-compose down followed by docker-compose up --build, the database is empty which I would not have expected. Any ideas or suggestions?
This is the docker-compose.yml file I am using:
version: '3.0'
services:
db:
image: postgres:latest
restart: always
ports:
- 5432:5432
env_file:
- env_file
volumes:
- postgresdata
networks:
- db1
market_data:
build: .
environment:
PYTHONUNBUFFERED: 'true'
stdin_open: true
tty: true
links:
- db:db
container_name: market_data_container
volumes:
- '.:/market_data'
depends_on:
- db
networks:
- db1
adminer:
image: adminer
restart: always
ports:
- 8080:8080
networks:
- db1
depends_on:
- db
volumes:
market_data:
postgresdata:
external: true
networks:
db1:
driver: bridge
Postgres uses already a volume to persist data, but docker-compose down deletes this volume. You are using named volumes in your compose file, but don't mount it correctly.
version: '3.0'
services:
db:
image: postgres:latest
restart: always
ports:
- 5432:5432
env_file:
- env_file
volumes:
- postgresdata:/var/lib/postgresql/data
networks:
- db1
Add the default path for postgres data to your volume postgresdata:/var/lib/postgresql/data. This should fix it.

Resources