Configure PHP/Apache (Lumen Project), MySQL ECR containers on AWS EC2 (Fargate) - php

I have uploaded container images for PHP/Apache (Lumen Project) & MySQl to AWS ECR successfully from my local development environment.
I am using Docker Desktop 4.5.1 (74721) on Windows 10.
But when I create Task definitions & services on AWS EC2, I get errors related to MySQL.
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
I am sharing my Dockerfile & docker-compose.yml files below that I used for the same process.
Dockerfile
FROM php:8.0-apache
RUN docker-php-ext-install pdo_mysql
WORKDIR /var/www/html/
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
COPY . ./Author-API
WORKDIR /var/www/html/Author-API
RUN composer install
COPY mysites.conf /etc/apache2/sites-available/mysites.conf
RUN a2enmod rewrite &&\
a2dissite 000-default.conf &&\
a2ensite mysites.conf &&\
service apache2 restart
EXPOSE 8085
Docker-compose.yml
version: '3.5'
services:
php:
container_name: "php_apache_service"
build:
context: .
dockerfile: Dockerfile
ports:
- "8090:8085"
volumes:
- .:/var/www/html/Author-API
restart: always
depends_on:
- db
links:
- db
db:
image: mysql
container_name: "mysql_service"
command: --default-authentication-plugin=mysql_native_password
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: author_api_db
volumes:
- ./database/mysql-data:/var/lib/mysql:rw
adminer:
image: adminer
container_name: "adminer_service"
restart: always
ports:
- 8080:8080
volumes:
mysql-data:
My Lumen project's .env file:
APP_NAME=Lumen
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_TIMEZONE=UTC
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=author_api_db
DB_USERNAME=root
DB_PASSWORD=example
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
On my local development machine everything works fine using docker-compose up -d.
But on AWS EC2 I couldn't find a way to configure MySQL properly or am I missing something?
Waiting to hear from you...
Thank you

Related

Docker container can't call localhost inside container itself

I have a docker-compose environment setup.
But inside service "Lumen" im trying to make a CURL request to the service itself.
However, the container can't access itself from localhost:8000 OR lumen:8000??
When I call lumen:8000 from the service it just never returns a response and just keeps loading ( And the curl request is to a different url so no infinite loop )
In my Laravel controller I found the protocal, host and port to be: http://lumen:8000
I seems like Laravel can't connect to itself, which I really need for my project.
I can connect to the Laravel from my own computer through localhost, but I need the Laravel to call it self.
Error message from Laravel controller after doing a CURL request:
Failed to connect to localhost port 8000 after 0 ms: Connection refused
Changing host to "lumen" just makes the request load infinite. No matter what page I try to connect to.
Docker-compose file:
version: "3.5"
services:
lumen:
expose:
- "8000"
ports:
- "8000:8000"
volumes:
- ./server:/var/www/html
- ./server/vendor:/var/www/html/vendor/
build:
context: server
dockerfile: Dockerfile
command: php -S lumen:8000 -t public
restart: always
privileged: true
depends_on:
- database
networks:
- database
frontend:
build:
context: client
dockerfile: Dockerfile
volumes:
- ./client/src:/app/src
ports:
- 3000:3000
stdin_open: true
#restart: always
networks:
- database
# Database Service (Mysql)
database:
image: mysql:latest
container_name: blogmoda_mysql
environment:
MYSQL_DATABASE: blogmoda-app
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci','--default-authentication-plugin=mysql_native_password']
ports:
- "127.0.0.1:3306:3306"
volumes:
- db-data:/var/lib/mysql
networks:
- database
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: dev_phpmyadmin
links:
- database
environment:
PMA_HOST: database
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
depends_on:
- database
ports:
- 9001:80
networks:
- database
volumes:
db-data:
# Networks to be created to facilitate communication between containers
networks:
database:
Server dockerfile:
FROM php:8.1-fpm-alpine
RUN apk update && apk add bash
RUN apk add chromium
RUN apk add --no-cache zip libzip-dev
RUN docker-php-ext-configure zip
RUN docker-php-ext-install zip
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install opcache
WORKDIR /var/www/html/
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
COPY . .
RUN composer install --ignore-platform-req=ext-zip --ignore-platform-reqs
I believe localhost should work. Assuming curl is installed in lumen, in your compose file under the lumen service can you try changing your command
command: php -S lumen:8000 -t public
to a direct curl via bash as
command: sh -c "curl -s localhost:8000"
Then check the logs of the lumen container to see whether or not the curl ran successfully.
Try 0.0.0.0:8000 instead localhost:8000. It works for localhost too

query in controller in dockerized laravel project

i dockerized my laravel project (9.25.1) and my problem is my query in tinker totally works and php artisan migrate works too but when i query in controller i get mysql error SQLSTATE[HY000] [2002] Connection refused
Dockerfile:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql sockets
RUN curl -sS https://getcomposer.org/installer​ | php -- \
--install-dir=/usr/local/bin --filename=composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /app
COPY . .
RUN composer install
docker-composer.yml:
version: '3.8'
services:
main:
build:
context: .
dockerfile: Dockerfile
command: 'php artisan serve --host=0.0.0.0'
volumes:
- .:/app
ports:
- 8000:8000
db:
platform: linux/x86_64
image: mysql:8.0
environment:
MYSQL_DATABASE: main
MYSQL_USER: admin
MYSQL_ROOT: admin
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: root
volumes:
- ./storage/dbdata:/var/lib/mysql
ports:
- 4306:3306
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=4306
DB_DATABASE=main
DB_USERNAME=admin
DB_PASSWORD=admin
Check this. If your application will run in a docker container, you need to use internal port. In your scenario it's 3306. If you want to use tinker run it on docker container.
For example:
docker-compose exec main php artisan tinker
For other artisan commands:
docker-compose exec main php artisan migrate

How to solve Laravel cant connect with mysql through docker

Im trying to deploy my laravel app using docker. Then I created docker-compose.yml file and Dockerfile like below.
docker-compose.yml
version: "3.7"
services:
app:
build:
args:
user: sammy
uid: 1000
context: ./
dockerfile: Dockerfile
image: travellist
container_name: travellist-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- travellist
db:
image: mysql:8.0
container_name: travellist-db
restart: unless-stopped
environment:
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- travellist
nginx:
image: nginx:alpine
container_name: travellist-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- travellist
networks:
travellist:
driver: bridge
Dockerfile
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
And when I try docker-compose up, all the containers run with a any error. And also when I run docker-compose ps, it shows like this,
Name Command State Ports
------------------------------------------------------------------------------------------------
travellist-app docker-php-entrypoint php-fpm Up 9000/tcp
travellist-db docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
travellist-nginx /docker-entrypoint.sh ngin ... Up 0.0.0.0:8000->80/tcp,:::8000->80/tcp
But, when laravel application, tries to connect with mysql, it doesnt happens. And even I cant connect using TablePlus, it shows this.
Lost connection to MySQL server at 'waiting for initial communication packet', system error: 10060
And sometimes, I get like below,
SQLSTATE[HY000] [1045] Access denied for user 'root'#'192.168.48.3' (using password: YES)
How can I fix this ??
Since you're networking the services, you only need to expose the port 3306/tcp (this is the default port mysql serves). This makes it inaccessible from 0.0.0.0 but accessible from your nginx and app container.
I have also added the correct volume to persist data (which I am assuming is what you're trying to do). I moved mysql to latest - feel free to rollback or stick to 8 if you really? need too.
db:
image: 'mysql:latest'
restart: 'unless-stopped'
expose:
- '3306'
environment:
- 'MYSQL_RANDOM_ROOT_PASSWORD=true'
- 'MYSQL_DATABASE=${DB_DATABASE}'
- 'MYSQL_USER=${DB_USER}'
- 'MYSQL_PASSWORD=${DB_PASSWORD}'
- 'MYSQL_ALLOW_EMPTY_PASSWORD=true'
volumes:
- 'laravel-database:/var/lib/mysql/'
networks:
- 'travellist'
volumes:
laravel-database:
Further reading can be found on the Docker hub under mysql.
Inside your Laravel .env, you should connect to the container via its DNS.
DB_HOST=db
If you want to connect to this container externally, you'll need to bind the port to the server. Do so by replacing expose with the following:
ports:
- '3306:3306'

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.

Laravel w/ Docker - Safe for production?

I recently spun up a Laravel / Docker environment by following this digitalocean tutorial, and was just wondering if anybody sees any concerns with using this in a production environment? If there are concerns, could you perhaps explain why they are a concern and what I can do to circumvent them?
I would have asked the question in the tutorial comments instead, but that never gets enough visibility.
EDIT: Here are the docker-compose.yml, Dockerfile and .env files, just so you have a little more context without having to visit the tutorial. Let me know if you need anything else.
docker-compose.yml:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: .docker/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
- ./.docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
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
- ./.docker/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: laravel_root_password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./.docker/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Dockerfile:
FROM php:7.2-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
default-mysql-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
nano \
unzip \
git \
curl
# 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-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN 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"]
.env:
APP_NAME=Laravel
APP_ENV=production
APP_KEY=base64:000/000000000000000000000000000000000000000=
APP_DEBUG=true
APP_URL=http://example.com
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=your_laravel_db_password
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
About a year ago, I deployed a Laravel app using Docker on an Ubuntu host. We've had no issues. Having a homogeneous environment eases team development and has helped streamline continuous deployment.
The production docker environment includes:
PHP
NGINX
MySQL
Redis
Additionally, the local docker environment includes:
Mailhog
A 'toolbox' that includes npm, mysql-client and other tools that would make things more convenient for the team.
There's a chance you'll want different docker-compose configurations for development vs. production. You can manage this by creating a docker-compose.prod.yml file and have your CD pipeline overwrite docker-compose.yml with the prod version upon deployment.
Or, if no CD is in place, you could use a docker-compose.dev.yml file to overwrite production values and add new configurations.
Then run
docker-compose up -d -f docker-compose.yml -f docker-compose.dev.yml
Good luck!

Categories