Can't use apt-get when docker-compose on a php container - php

I am trying to setup a container that is pulling php:8.1-fpm through docker-compose, but I need to a some libs from apt-get that I require apt-get install -y php8.1-gmp
docker-compose
version: '3'
services:
php:
image: php:8.1-fpm
depends_on:
- mysql
command: bash -c "apt-get update && apt-get install -y php8.1-gmp"
volumes:
- ./:/var/www/html:rw,cached
mysql:
image: mysql:debian
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: password
nginx:
depends_on:
- php
image: nginx:1.19
ports:
- 80:80
volumes:
- ./:/var/www/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
but when I docker-compose I get an error
php_1 | Reading package lists...
php_1 | Reading package lists...
php_1 | Building dependency tree...
php_1 | Reading state information...
php_1 | E: Unable to locate package php8.1-gmp
php_1 | E: Couldn't find any package by glob 'php8.1-gmp'
php_1 | E: Couldn't find any package by regex 'php8.1-gmp'
if I remove the command: line it works fine but if I exec into php container and run the same command it gives me the same error.

Related

Symfony- docker container Could not open input file: bin/bash

I started my Symfony 6 project with Docker successfully.
I have a problem when accessing the PHP container and trying to run php bin/console doctrine:migrations.
The error:
Could not open input file: bin/console
My steps:
docker ps //checked php container name
docker exec -it php_container_name /bin/bash //entered with success
Then I am in container like;
root#2a9a3e61c23b:/var/www/html#
And when entering the above command, the error above is shown.
Docker file:
FROM php:8.1-fpm
RUN apt-get update && apt-get install -y \
git \
unzip \
libicu-dev \
libpq-dev \
wget
# Installing Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && composer --version
RUN apt-get -y install cron default-mysql-client
RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql
and docker .yml file:
version: '3'
services:
nginx:
image: nginx:1.17
ports:
- "80:80"
volumes:
- "./docker-configs/nginx.conf:/etc/nginx/conf.d/default.conf"
- ".:/app:cached"
php:
build:
context: ./docker-configs/php
volumes:
- "./docker-configs/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
- ".:/app:cached"
environment:
PHP_IDE_CONFIG: "serverName=Docker"
db:
image: mysql:5.7
platform: linux/amd64
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db
MYSQL_USER: root
MYSQL_PASSWORD: root
ports:
- 3306:3306
volumes:
- ./mysql-data:/var/lib/mysql
postgresdata:

Cannot not connect to database server within docker-compose (php and MariaDB)

I've built a docker-compose file that creates 2 services (PHP and MariaDB). Somehow I cannot connect to the database from the PHP service: Within the PHP service, a Laravel-app is running.
The error message (redirect is a table within the database):
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from redirects)
All env-variables should be correct.
Here is my docker-compose.yml:
version: "3.7"
services:
faaren_backend:
image: php:alpine
container_name: FAA-Backend
volumes:
- "./:/faaren_backend"
working_dir: /faaren_backend
command: "php artisan serve --host=0.0.0.0 --port=8000"
ports:
- 8000:8000
build:
context: docker/php
dockerfile: dev.Dockerfile
faaren_database:
image: mariadb
container_name: FAA-Database
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: ${DB_DATABASE}
volumes:
- "faa-db-data:/var/lib/mysql/data"
volumes:
faa-db-data: {}
My dev.Dockerfile
FROM php:7.3-fpm
RUN apt-get update
RUN apt-get update && apt-get install -y libzip-dev \
&& docker-php-ext-install zip
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN pecl install imagick
RUN docker-php-ext-enable imagick
RUN apt-get install $PHPIZE_DEPS && \
pecl install xdebug && docker-php-ext-enable xdebug && \
docker-php-ext-install pdo_mysql pcntl
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
VOLUME /faaren_backend
And finally my .env-file:
DB_CONNECTION=mysql
DB_HOST=faaren_backend
DB_PORT=3306
DB_DATABASE=faaren_db
DB_USERNAME=root
DB_PASSWORD=
Mostly I've followed this tutorial: https://medium.com/swlh/laravel-with-docker-compose-de2190569084
You need change your docker database host:
DB_CONNECTION=mysql
DB_HOST=faaren_database <-- edit
DB_PORT=3306
DB_DATABASE=faaren_db
DB_USERNAME=root
DB_PASSWORD=
And in docker-compose.yml faaren_backend section add:
depends_on:
- faaren_database
Hope it's help.
Looks like you deal with a race condition. You need to use depends_on directive to control startup order and use wait script to make sure that service ready to accept connections:
version: "3.7"
services:
faaren_backend:
image: php:alpine
container_name: FAA-Backend
volumes:
- "./:/faaren_backend"
working_dir: /faaren_backend
command: entrypoint.sh
ports:
- 8000:8000
depends_on:
- faaren_database
build:
context: docker/php
dockerfile: dev.Dockerfile
faaren_database:
image: mariadb
container_name: FAA-Database
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: ${DB_DATABASE}
volumes:
- "faa-db-data:/var/lib/mysql/data"
volumes:
faa-db-data: {}
There are a bunch of ways to check service availability, sometimes wait-for-it can be really handy. In the example below used netcat(you need to install it into the container).
Also, it'a good idea to put a script into a separated file:
entrypoint.sh
#!/usr/bin/env bash
until nc -w 1 -z faaren_database 3306; do
>&2 echo "Mysql is unavailable - sleeping"
sleep 1
done
sleep 10
>&2 echo "Mysql is up - executing command"
php artisan serve --host=0.0.0.0 --port=8000
UPDATE: Specify correct DB_HOST how Dmitry suggested as well.

Docker PHP Image when using Redis extension exited on signal 2 (SIGINT)

I have the following DockerFile and docker-compose.yml for an Laravel application. I have just added Redis today and this appeared in my terminal in the end of docker-compose up when I tried to connect and use Redis from PHP.
zinc_app | [23-Dec-2019 11:31:27] ALERT: oops, unknown child (32) exited on signal 2 (SIGINT). Please open a bug report (https://bugs.php.net).
zinc_app | [23-Dec-2019 11:31:27] ALERT: oops, unknown child (31) exited on signal 2 (SIGINT). Please open a bug report (https://bugs.php.net).
zinc_app | [23-Dec-2019 11:32:09] ALERT: oops, unknown child (20) exited on signal 9 (SIGKILL). Please open a bug report (https://bugs.php.net).
I am pretty sure the error came from the PHP Redis extension, because it didn't have the problem before. Have anyone encountered this problem before? Have I configured anything wrong?
I have tested this on another computer, but it didn't exhibit the same behavior. I have tried docker system prune -a and build and up again, but my computer still has the same problem.
As for the code I have run. I have run this in artisan tinker.
$redis = new Redis();
$redis->connect('zinc_redis', 6379);
$redis->get('1'); // The Oops message originated from this line.
Docker Compose:
version: '3.6'
services:
#PHP Service
zinc_app:
build:
context: .
dockerfile: Dockerfile
image: zinc/php
container_name: zinc_app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: zinc_app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- /tmp:/tmp #For CS Fixer
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
zinc_webserver:
image: nginx:alpine
container_name: zinc_webserver
restart: unless-stopped
tty: true
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Postgres Service
zinc_db:
image: postgres:12-alpine
container_name: zinc_db
restart: unless-stopped
tty: true
ports:
- "5432:5432"
environment:
POSTGRES_DB: zinc
POSTGRES_PASSWORD: admin
SERVICE_TAGS: dev
SERVICE_NAME: postgres
volumes:
- dbdata:/var/lib/postgresql
- ./postgres/init:/docker-entrypoint-initdb.d
networks:
- app-network
zinc_redis:
image: redis:5.0-alpine
container_name: zinc_redis
restart: unless-stopped
tty: true
ports:
- "6379:6379"
volumes:
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf
- redisdata:/data
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
redisdata:
driver: local
FROM php:7.4-fpm
# Switch to root user
USER root
# Set working directory
WORKDIR /var/www
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
RUN chown www:www /var/www
# Copy composer
COPY --chown=www:www composer.json composer.lock /var/www/
# Install dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential \
libpq-dev \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
libzip-dev \
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 pgsql pdo_pgsql zip exif pcntl
# Enable igbinary & PHP Redis ext using igbinary
RUN pecl install -o -f igbinary && y | pecl install -o -f redis && docker-php-ext-enable redis igbinary
RUN rm -rf /tmp/pear
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
COPY --chown=www:www . /var/www/
# Change current user to www
USER www
# Install packages
RUN composer global require hirak/prestissimo && composer install
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Docker Slimframework MySQL

i'm new to docker and slimframework, i'm trying to make a docker file or docker-compose file that when i send to someone else they can run and have my sql db running with the tables already created i dont know if this is right but i get an error.
dockerfile
FROM php:7.1-apache
COPY apache2.conf /etc/apache2
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
this is my docker-compose
version: '2'
volumes:
logs:
driver: local
services:
db-mysql:
image: mysql:5
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: path
MYSQL_USER: root
MYSQL_PASSWORD: root
slim:
image: php:7-alpine
working_dir: /var/www
command: php -S 0.0.0.0:8080 -t public
environment:
docker: "true"
depends_on:
- db-mysql
ports:
- 80:8080
volumes:
- .:/var/www
- logs:/var/www/logs

cannot connect mysql and php

Having this lamp docker setup (Im a sort of docker newbie):
docker-compose.yml
version: '2'
services:
webserver:
build: .
ports:
- "8080:80"
- "443:443"
volumes:
- ./:/var/www/html
links:
- db
db:
image: mysql:5.6
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=adminpasswd
- MYSQL_DATABASE=se_racken_dev
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- "88:80"
links:
- db:db
Dockerfile
FROM php:5.6-apache
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
Just cant get my local environment to work.
Get this error message at localhost:8088:
SQLSTATE[HY000] [2002] No such file or directory
How can I configure my docker setup to get past this connection problem?
Got some hints here:
Starting with Zend Tutorial - Zend_DB_Adapter throws Exception: "SQLSTATE[HY000] [2002] No such file or directory"
Do I need to install vim and do what they suggest in above or can I solve it in my docker files?
On webserver image you should define the connection values such as database's hostname,database name, username and password.
What can you can is to specify a .env file as seen in https://docs.docker.com/compose/env-file/
In your case that should be:
DB_HOSTNAME=db:/var/run/mysqld/mysqld.sock
DB_USER=root
DB_PASSWORD=somepassword
DB_NAME=se_racken_dev
Then to your Dockerfile specify:
FROM php:5.6-apache
ENV MYSQL_HOSTNAME="localhost"
ENV MYSQL_PASSWORD=""
ENV MYSQL_USERNAME="root"
ENV MYSQL_DATABASE_NAME=""
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
Then modify your docker-compose.yml like that:
version: '2'
services:
webserver:
build: .
ports:
- "8080:80"
- "443:443"
volumes:
- ./:/var/www/html
links:
- db
environment:
- MYSQL_HOSTNAME=$DB_HOSTNAME
- MYSQL_PASSWORD=$DB_USER
- MYSQL_USERNAME=$DB_PASSWORD
- MYSQL_DATABASE_NAME=$DB_NAME
db:
image: mysql:5.6
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=$DB_PASSWORD
- MYSQL_DATABASE=$DB_NAME
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- "88:80"
links:
- db:db
Then you can use php's getenv to retreive the values from enviromental variables you specified. eg In your case the getenv('MYSQL_DATABASE_NAME') will retreive the value "se_racken_dev" as a string. So you need to modify the database configuration file in order to retreive correctly the database connection credentials using the function mewntioned above.
A simple way to remember is whatever you specify via ENV in your dockerfile you can retreive via getenv.

Categories