Docker - Ubuntu - Nginx - MariaDB - Connection refused - php

Im trying to setup a docker container on OSX with Docker - Ubuntu - Nginx - MariaDB to run a Laravel App
My docker settings are:
version: "2"
services:
nginx:
build:
context: ./nginx
ports:
- "8080:80"
volumes:
- ./app:/var/app
fpm:
build:
context: ./fpm
volumes:
- ./app:/var/app
expose:
- "9000"
environment:
- "DB_HOST=db"
- "DB_DATABASE=laravel_db"
db:
image: mariadb
ports:
- "33061:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=laravel_db
volumes:
- ./database:/var/lib/mysql
And the 2 docker files:
FROM nginx
ADD ./default.conf /etc/nginx/conf.d/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
CMD service nginx start
FROM ubuntu:latest
RUN apt-get update && apt-get install -y software-properties-common language-pack-en-base \
&& LC_ALL=en_US.UTF-8 add-apt-repository -y ppa:ondrej/php \
&& apt-get update \
&& apt-get install -y php7.0 php7.0-fpm php7.0-mysql mcrypt php7.0-gd curl \
php7.0-curl php-redis php7.0-mbstring sendmail supervisor \
&& mkdir /run/php \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN sed -i -e 's/listen = \/run\/php\/php7.0-fpm.sock/listen = 0.0.0.0:9000/g' /etc/php/7.0/fpm/pool.d/www.conf \
&& sed -i -e 's/;daemonize = yes/daemonize = no/g' /etc/php/7.0/fpm/php-fpm.conf
WORKDIR /var/app
EXPOSE 9000
CMD ["/usr/bin/supervisord"]
So far so good. I can access the Laravel App homepage as localhost:8080 and use Sequel Pro to access the MySQL DB.
But when access to the Laravel route that requires DB query, it returns "Connection refused"
Then I create a raw PHP file to make a DB connection test:
$link = mysqli_connect('127.0.0.1', 'root', 'root', 'laravel_db', 33061);
if(!$link) {
die('failed to connect to the server: ' . mysqli_connect_error());
}
And I get connection refused error as well.
I tried using 127.0.0.1 and localhost but no hope.
Tried to google it but most answers are about ports are not published...
Thanks

You need to use the link directive to connect various docker containers when using docker-compose. Ie, if you want to have docker container A communicate with docker container B, they need to be 'linked'
docker-compose documentation on links

Related

I cannot connect my dockerize symfonfy 4.2 project with mySql database in another container

I am trying to move an existing Symfony 4.2 project to a docker container with the existing database engine but the project doesn't seem to connect with the database.
As I viewed the log on the php-apache container, I saw this error message displayed **Uncaught PHP Exception Doctrine\DBAL\Exception\ConnectionException: "An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused" ** And on a browser I got this message: An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
These are my steps to set up the project on docker if needed to diagnose my issue
Downloaded a php apache docker image using php-apache-7.4
Downloaded MySQL 8.0 image, 5.4/5.7 isn't supported on the Mac M1 processor
Note my docker-compose.yml file my environment: MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
I installed the following PHP module into the php apache container: pdo, MySQL, pdo_mysql and zip
Created a self-signed SSL certificate and installed it on the PHP apache container
The following ports were exposed 3304: MySQL, 80: web and 443: SSL port
created a network without a subnet in the docker-compose.yml file
added a name server test domain name in the /etc/hosts file on the local machine - restarted apache
Below is my full docker-compose.yml file
version: "3.8"
networks:
abc-network:
services:
#mysql
mysql-service:
image: mysql
networks:
- abc-network
container_name: abc_db_container
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- ./mysql:/var/lib/mysql
restart: always
#php
php-service:
build:
context: .
dockerfile: ./symfony/Dockerfile
container_name: abc_php_container
networks:
- abc-network
ports:
- "80:80"
- "443:443"
volumes:
- ./symfony/docker/ssl/mycert.crt:/etc/ssl/certs/mycert.crt
- ./symfony/docker/ssl/mycert.key:/etc/ssl/private/mycert.key
- ./symfony:/var/www/symfony
depends_on:
- mysql-service
Below is my DockerFile configuration
# PHP + Apache
FROM php:7.4-apache
#Enable apache module and ssl self sign installation
RUN a2enmod rewrite
RUN a2enmod ssl && a2enmod socache_shmcb
RUN sed -i '/SSLCertificateFile.*snakeoil\.pem/c\SSLCertificateFile \/etc\/ssl\/certs\/mycert.crt' /etc/apache2/sites-available/default-ssl.conf && sed -i '/SSLCertificateKeyFile.*snakeoil\.key/cSSLCertificateKeyFile /etc/ssl/private/mycert.key\' /etc/apache2/sites-available/default-ssl.conf
RUN a2ensite default-ssl
RUN apt-get update && apt-get upgrade -y
#updates apt get and install essential php libraries
RUN apt-get update \
&& apt-get install -y libzip-dev git wget --no-install-recommends \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
#Install mysql driver for php and zip
RUN docker-php-ext-install pdo mysqli pdo_mysql zip;
#Installs composer
RUN wget https://getcomposer.org/download/2.0.9/composer.phar \
&& mv composer.phar /usr/bin/composer && chmod +x /usr/bin/composer
RUN chmod 777 /var/lib
#Copies ABC's virtual host conf file to the docker container
COPY ./symfony/docker/apache.conf /etc/apache2/sites-enabled/000-default.conf
COPY ./symfony/docker/ssl-conf.conf /etc/apache2/sites-available/default-ssl.conf
#Copies ABC CMS Symfony project
COPY ./symfony /var/www/symfony/
#Points the working directory
WORKDIR /var/www/symfony
CMD ["apache2-foreground"]

Opening Docker container port on localhost in browser returns empty response

I'm trying to run a web project from a Docker container,
when I Dockerize the application on a macBook with an intel chip, everything runs fine and I can make a call to the Docker container. But when I run the same project, with the same setup on my M2 MacBook Air, the browser returns an empty response.
("this page isn't working" --> in Chrome)
This happens even though the containers appear to be running...
(Both containers are green lit up --> in Docker Desktop)
The container makes use of an Nginx service and a php service. The .yml file looks as below:
Docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: php:8.0.6-fpm
container_name: Asset-Service
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: Asset-Service
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:stable
container_name: Asset-Web-Server
restart: unless-stopped
tty: true
ports:
- "8087:80"
- "4487:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
My Dockerfile is the following, even though I don't think that this file causes the problem:
Dockerfile
FROM php:8.0.6-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
USER root
RUN apt-get update && apt-get install -y \
mariadb-client-10.3 \
libcurl4-openssl-dev \
pkg-config \
libssl-dev \
libpng-dev \
libzip-dev \
libonig-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
nano
RUN pecl uninstall mongodb
RUN pecl install mongodb
RUN echo "extension=mongodb.so" >> /usr/local/etc/php/conf.d/mongodb.ini
# 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-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
#RUN docker-php-ext-enable mongodb
# Install composer
## RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -fg 1000 www
RUN id -u 1000 >/dev/null 2>&1 || 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
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
The container logs look normal and the ports in the inspect tab show 0.0.0.0:8087 and 0.0.0.0:4487.
Is this a recurring issue with the apple silicon version of Docker,
and is there anything I can do about it?
I have already tried to replicate the issue on an Intel macBook, but got the desired result instead of the empty response.
On my M2 I tried reinstalling Docker and rebuilding the containers but this didn't seem to fix anything...
Nevermind this issue.
This problem occurred when trying to build the containers from the Desktop folder.

docker-compose volume mount is empty when using docker toolbox (windows)

I am trying to dockerise my laraver 5.5 application using docker-compose.
Here's my docker-compose.yml file definition:
version: '2.1'
services:
# The Database
database:
image: mysql:5.7
restart: always
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
environment:
- "MYSQL_DATABASE=myapp"
- "MYSQL_USER=myapp"
- "MYSQL_PASSWORD=123456"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306"
# The Application
app:
depends_on:
database:
condition: service_healthy
build:
context: ./
dockerfile: ./docker-compose/app.dockerfile
volumes:
- ./:/var/www/html
environment:
- "DB_CONNECTION=mysql"
- "DB_HOST=database"
- "DB_PORT=3306"
- "DB_DATABASE=myapp"
- "DB_USERNAME=myapp"
- "DB_PASSWORD=123456"
ports:
- "8080:80"
and this is my ./docker-compose/app.dockerfile:
# Base image
FROM php:7.1-apache
# Configure system
RUN apt-get update && apt-get install -y \
libmcrypt-dev \
mysql-client \
zlib1g-dev \
--no-install-recommends \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-install mcrypt pdo_mysql
# Add php.ini and apache2.conf
COPY docker-compose/php.ini $PHP_INI_DIR/php.ini
COPY docker-compose/apache2.conf /etc/apache2/apache2.conf
# Configuring Apache
RUN rm -rf /etc/apache2/sites-available/* \
&& rm -rf /etc/apache2/sites-enabled/*
# Enable rewrite module
RUN a2enmod rewrite
# Download and install composer globally
RUN curl -s http://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer
# Add vendor binaries to PATH
ENV PATH=/var/www/html/vendor/bin:$PATH
I use the following command to start up my stack:
docker-compose -d --build via the Docker Quickstart Terminal on my Windows 10.
Everything builds fine and runs (I checked via docker-compose ps). When I visit the app url, I am getting forbidden error from apache, so I decided to login to the container using docker exec -it my_app_1 /bin/bash command and I went into /var/www/html directory and noticed that it's empty.
Doesn't volume mounting works in windows?

Docker PHP image. lstat docker-php-source: no such file or directory

I am trying to setup PHP 5.6.30 fp, but i got error.
Error the same as here (Docker) Getting error: docker-php-source: no such file or directory when building docker file
But i can`t understand how to resolve this.
my folder looks like
app
--docker
--workspace
inside workspace Dockerfile i paste all from https://github.com/docker-library/php/blob/eadc27f12cfec58e270f8e37cd1b4ae9abcbb4eb/5.6/fpm/Dockerfile
Step 15/22 : COPY docker-php-source /usr/local/bin/
ERROR: Service 'workspace' failed to build: lstat docker-php-source: no such file or directory
So what i must to do?
i did this command docker pull php before and it is not helped.
So i have this .yml file
version: '2'
services:
app:
build: ./docker/app
volumes:
- .:/var/www/html
command: "true"
workspace:
build: ./docker/workspace
volumes_from:
- app
links:
- php
- nginx
tty: true
depends_on:
- app
nginx:
build: ./docker/nginx
ports:
- 8080:80
links:
- php
volumes_from:
- app
php:
build: ./docker/php
expose:
- "9000"
volumes_from:
- app
mysql:
image: mysql:5.7
volumes_from:
- app
expose:
- "3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app
MYSQL_USER: root
MYSQL_PASSWORD: root
memcached:
image: memcached
ports:
- "11211:11211"
inside workspace/Dockerfile i have all strings from github image 6.6.30fpm
Then inside this folder i do
docker-compose build workspace
Before this in workspace i had another settings
FROM ubuntu:14.04
ENV DEBIAN_FRONTEND noninteractive
RUN \
sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list && \
apt-get update && \
apt-get -y upgrade && \
apt-get install -y build-essential && \
apt-get install -y software-properties-common && \
apt-get install -y byobu curl git htop man unzip vim wget nano locate
RUN apt-get update --fix-missing --allow-unauthenticated
RUN apt-get install -y --allow-unauthenticated libmcrypt-dev libxml2-dev mysql-client
RUN apt-get update -y --allow-unauthenticated && apt-get upgrade -y --allow-unauthenticated && apt-get install -q -y php5 php5-dev php5-fpm php5-mysqlnd php5-mcrypt
RUN echo "extension=/usr/lib/php5/20121212/mcrypt.so" > /etc/php5/mods-available/mcrypt.ini \
&& ln -s /etc/php5/mods-available/mcrypt.ini /etc/php5/cli/conf.d/20-mcrypt.ini
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
WORKDIR /var/www/html
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
and this works but i want to 5.6.30 instead of 5.5.9
Perhaps it is due to the fact that there is no permission to execute.
Try
chmod +x docker-php-*
UPD:
Just put all these files inside directory with your php 5.6 Dockerfile
If you want to use php 7.2 version, this link is for you
Anyway for other googlers. Just explore this repository and you will find that you want. :)

Cannot start service php-fpm: invalid header field value "oci runtime error: container_linux.go:247: starting container

I am working in a few containers (for learn Docker) and this is what I have so far:
A CentOS 7 container
A Nginx container inherit from previous CentOS7
A PHP71-FPM container inherit from previous Nginx
I am trying them from a docker-compose.yml file that looks as follow:
version: '2'
services:
php-fpm:
container_name: "php71-fpm-nginx"
image: reynierpm/docker-php-fpm
ports:
- 80:80
- 9001:9001
environment:
PHP_ERROR_REPORTING: 'E_ALL & ~E_DEPRECATED & ~E_NOTICE'
STATUS_PAGE_ALLOWED_IP: '127.0.0.1'
volumes:
- D:\Development\www\:/data/www
links:
- db
db:
container_name: "db_mysql"
image: mysql
environment:
MYSQL_ROOT_PASSWORD: "gT927twQVwN2du&F!29*$Jb2"
MYSQL_DATABASE: "nortwind"
MYSQL_USER: "user_db"
MYSQL_PASSWORD: "4t6V2M3#2Q2CDpxYb*fp6e8V"
volumes:
- D:\Development\data\db:/var/lib/mysql
elk:
container_name: "elk"
image: willdurand/elk
ports:
- 81:80
volumes:
- ./elk/logstash:/etc/logstash
- ./elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- php-fpm
As soon as I run: docker-compose up -d it ends with the following message:
> docker-compose up -d
WARNING: The Jb2 variable is not set. Defaulting to a blank string.
db_mysql is up-to-date
Starting php71-fpm-nginx
ERROR: for php-fpm Cannot start service php-fpm: invalid header field value "oci runtime error: container_linux.go:247:
starting container process caused \"exec: \\\"/config/bootstrap.sh\\\": permission denied\"\n"
ERROR: Encountered errors while bringing up the project.
I have found some post speaking about the same issue:
docker compose oci runtime error, executable file not found in $PATH
Oci runtime error docker-compose not a directory
And this search results
But nothing from there works for me. I know I am missing something but I am not able to find what is, can I get any help from community?
I just found that file /config/bootstrap.sh hasn't proper permissions on first parent image:
FROM centos:latest
RUN \
yum update -y && \
yum install -y epel-release && \
yum install -y iproute python-setuptools hostname inotify-tools yum-utils which jq && \
yum clean all && \
easy_install supervisor
COPY container-files /
VOLUME ["/data"]
ENTRYPOINT ["/config/bootstrap.sh"]
Adding RUN chmod +x /config/bootstrap.sh make this to work properly:
FROM centos:latest
RUN \
yum update -y && \
yum install -y epel-release && \
yum install -y iproute python-setuptools hostname inotify-tools yum-utils which jq && \
yum clean all && \
easy_install supervisor
COPY container-files /
RUN chmod +x /config/bootstrap.sh
VOLUME ["/data"]
ENTRYPOINT ["/config/bootstrap.sh"]

Categories