I'm setting up a config to run my environment. I've a mysql, redis, nginx and php-fpm.
The services mysql and redis are ok, but when I start nginx and php my environment don't run.
The nginx config call to fastcgi_pass: php:9000
The error is:
[error] 6#6: *1 connect() failed (113: No route to host) while connecting to upstream, client: 192.168.224.1, server: ~.*, request: "POST /sellers HTTP/1.1", upstream: "fastcgi://192.168.224.4:9000", host: "127.0.0.1:8080"
My Dockerfile is:
FROM php:7.2-fpm-alpine
WORKDIR /app
# Install composer
RUN apk --update add curl && rm /var/cache/apk/*
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql
# Copy files
COPY . /app/
## THE LIFE SAVER
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
RUN composer up
CMD /wait && bin/console doctrine:migrations:migrate
and my docker-compose is:
version: '3'
services:
mysql:
image: mysql:5.7
restart: unless-stopped
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: database
MYSQL_DATABASE: cart
redis:
image: redis:alpine
ports:
- '6379:6379'
web:
image: nginx:latest
depends_on:
- php
ports:
- '8080:80'
volumes:
- .:/app
- ./docker/site.conf:/etc/nginx/conf.d/site.conf
php:
build:
context: .
depends_on:
- mysql
volumes:
- .:/app
environment:
- APP_ENV=prod
- APP_SECRET=aaaa
- DATABASE_URL=mysql://root:database#mysql:3306/cart
- REDIS_HOST=redis
- REDIS_PORT=6379
- WAIT_HOSTS= mysql:3306
Related
I'm working on configuring a docker setup for an old php project with apache, php 7.2 and mariadb. After I got the container up and running I get permission denied when I try to write a file from php.
What is the best approach to solve this?
docker-compose.yml
version: "3"
networks:
dirtbike:
services:
webserver:
build:
context: .
dockerfile: Dockerfile
container_name: dirtbike-webserver
restart: 'always'
depends_on:
- database
ports:
- "80:80"
- "443:443"
networks:
- dirtbike
volumes:
- ./public_html:/var/www/html
database:
image: mariadb:10.3
container_name: dirtbike-database
restart: 'always'
networks:
- dirtbike
ports:
- "127.0.0.1:3306:3306"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
Dockerfile
FROM php:7.2-apache-stretch
RUN docker-php-ext-install pdo_mysql
RUN a2enmod ssl && a2enmod rewrite && a2enmod headers
RUN mkdir -p /etc/apache2/ssl
COPY ./Docker/ssl/*.pem /etc/apache2/ssl/
COPY ./Docker/config/apache/dirtbike.conf /etc/apache2/sites-available/000-default.conf
index.php
<?php
file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'test.txt', 'lorem ipsum');
The problem was that the user from the container (www-root) was different then the user from the host.
I added args: uid: ${UID} in the docker-compose file like this:
version: "3"
networks:
dirtbike:
services:
webserver:
build:
context: .
dockerfile: Dockerfile
args:
uid: ${UID}
container_name: dirtbike-webserver
restart: 'always'
depends_on:
- database
ports:
- "80:80"
- "443:443"
networks:
- dirtbike
volumes:
- ./public_html:/var/www/html
in Dockerfile I added ARG uid and RUN usermod -u ${uid} www-data && groupmod -g ${uid} www-data; like this:
FROM php:7.2-apache-stretch
ARG uid
RUN docker-php-ext-install pdo_mysql
RUN a2enmod ssl && a2enmod rewrite && a2enmod headers
RUN mkdir -p /etc/apache2/ssl
COPY ./Docker/ssl/*.pem /etc/apache2/ssl/
COPY ./Docker/config/apache/dirtbike.conf /etc/apache2/sites-available/000-default.conf
RUN usermod -u ${uid} www-data \
&& groupmod -g ${uid} www-data;
In your docker compose file you can to add your local machine user. First you need check current user id, in my case it is ubuntu:
echo ${UID}
Output:
1001
docker-compose.yml:
webserver:
build:
context: .
dockerfile: Dockerfile
container_name: dirtbike-webserver
restart: 'always'
depends_on:
- database
ports:
- "80:80"
- "443:443"
networks:
- dirtbike
user: 1001 # local machine user id
volumes:
- ./public_html:/var/www/html
Hope help you.
I am trying to set up Codeception to do Acceptance and Functional testing for my web app. Below are my files:
docker-compose.yml
version: '3.7'
services:
# nginx - web server
nginx:
build:
context: ./docker-config/nginx
dockerfile: ./Dockerfile
env_file: &env
- ./cms/.env
init: true
ports:
- "8000:80"
volumes:
- cpresources:/var/www/project/cms/web/cpresources
- ./cms/web:/var/www/project/cms/web:cached
networks:
mmc-network:
aliases:
- mmc.nginx
# php - run php-fpm
php:
build: &php-build
context: ./docker-config/php-prod-craft
dockerfile: ./Dockerfile
depends_on:
- "mysql"
- "redis"
env_file:
*env
expose:
- "9000"
init: true
volumes: &php-volumes
- some volumes............
networks:
mmc-network:
aliases:
- mmc.php
# mysql - database
mysql:
build:
context: ./docker-config/mysql
dockerfile: ./Dockerfile
cap_add:
- SYS_NICE # CAP_SYS_NICE
env_file:
*env
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
init: true
ports:
- "3306:3306"
volumes:
- db-data:/var/lib/mysql
- ./db-seed/:/docker-entrypoint-initdb.d
networks:
- MMC-network
# redis - key/value database for caching & php sessions
redis:
build:
context: ./docker-config/redis
dockerfile: ./Dockerfile
expose:
- "6379"
init: true
networks:
- mmc-network
# webpack - frontend build system
webpack:
build:
context: ./docker-config/node-dev-webpack
dockerfile: ./Dockerfile
env_file:
*env
init: true
ports:
- "8080:8080"
volumes:
- some volumes..........
networks:
- mmc-network
# selenium - web driver for codeception testing
selenium:
container_name: mmc-selenium
ports:
- "4444:4444"
volumes:
- ./cms:/data
build:
context: ./docker-config/selenium
dockerfile: ./Dockerfile
networks:
mmc-network:
aliases:
- mmc.selenium
volumes:
db-data:
cpresources:
storage:
networks:
mmc-network:
acceptance.suite.dist.yml:
actor: AcceptanceTester
extensions:
enabled:
- Codeception\Extension\RunFailed
- Codeception\Extension\Recorder
modules:
error_level: "E_ALL"
enabled:
- WebDriver:
url: 'http://mmc.nginx'
host: mmc.selenium
port: '4444'
window_size: 1920x1200
browser: 'chrome'
wait: 60
capabilities:
os: Windows
os_version: 10
browserstack.local: true
- \Helper\Acceptance
NavigationCept.php
<?php
$I = new AcceptanceTester($scenario);
# define purpose of the test
$I->wantTo("check that navigation is functional.");
# check all menu items
$I->amOnPage('/');
$I->see('Search');
***Now, point to be noted, Codeception is already installed inside my PHP container and working perfectly.
When I try to run the test, I get the below error which indicates that the connection to my host (which is my Nginx server) has been refused.
I tried with a different url, for example, https://google.com and it just connected fine and everything was successful. Am I doing something wrong in here? Is my url param incorrect? Please help me out if you can. Thanks in advance.
I have seen such exception earlier with Codeception + Selenium. I do not have that project config file to share with you though.
My strong guess here is this error could be due to conflicts around Codeception-ChromeDriver-Selenium versions
Have you tried to execute this on a lower ChromeDriver version say 80 or 75?
Or change the Selenium version?
You could review the following notes which may help you debug this more
https://github.com/Codeception/Codeception/issues/5062
https://stackoverflow.com/a/63760572/2923098
Will update this answer if I find other resources to resolve this.
Docker service are reachable from each other using the service name as hostname. So your configuration could be:
enabled:
- WebDriver:
url: 'http://nginx'
host: selenium
port: '4444'
An alias to a service could be specified at another service as:
selenium:
links:
- "nginx:mmc.nginx"
Then the url could be url: 'http://mmc.nginx'
Workaround: use service IP address
enabled:
- WebDriver:
url: 'http://172.18.0.3'
host: 172.18.0.2
port: '4444'
Test setup using busybox:
docker-compose.yaml that runs 2 dummy web servers with netcat utility for demonstration purposes.
version: '3.7'
services:
nginx:
container_name: mmc-nginx
hostname: mmc-nginx
image: busybox
command: ["sh", "-c", "while true ; do (echo -e 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-type: application/xml\r\n\r\n'; echo 'response from '`hostname`':80') | nc -l -p 80 ;done" ]
init: true
ports:
- "8000:80"
networks:
- mmc-network
# selenium - web driver for codeception testing
selenium:
container_name: mmc-selenium
hostname: mmc-selenium
command: ["nc", "-l", "-p", "4444"]
ports:
- "4444:4444"
image: busybox
networks:
- mmc-network
networks:
mmc-network:
name: mmc-network
test command:
docker exec -it mmc-selenium wget -q "http://mmc-nginx" -O -
docker exec -it mmc-selenium wget -q "http://172.18.0.3" -O -
Response:
response from mmc-nginx:80
Access from outside docker network:
wget -q "http://localhost:8000" -O -
response from mmc-nginx:80
Ping to mmc-nginx
docker exec -it mmc-selenium ping -c2 mmc-nginx
PING mmc-nginx (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.071 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.111 ms
--- mmc-nginx ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.071/0.091/0.111 ms
Find service IP address:
Using docker. Change svc value accordingly.:
svc='friendly_solomon' docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$svc"
Using jq. Change svc value accordingly. jq is a great json processing tool to have btw.
svc='mmc-nginx'; docker network inspect bridge | jq -r --arg svc "$svc" '.[] | .Containers | .[] | select(.Name == $svc) | .IPv4Address'
Using grep
svc='mmc-nginx' docker network inspect bridge | grep -A5 "$svc" | grep 'IPv4Address
Basically I want to run the LAMP stack in an Docker container.
All services are so far running but as soon I try to connect to an mysql from within an PHP script I get following error:
Warning: mysqli::__construct(): (HY000/2002): Connection refused
docker-compose.yml
version: '3.2'
services:
php:
build: ./php/
networks:
- backend
volumes:
- './public/:/var/www/html'
apache:
build: ./apache/
depends_on:
- php
- mysql
networks:
- frontend
- backend
ports:
- '8080:80'
volumes:
- './public/:/var/www/html'
mysql:
image: 'mysql:5.6.40'
ports:
- "3306:3306"
networks:
- backend
environment:
- MYSQL_DATABASES=test
- MYSQL_ROOT_PASSWORD=root
- MYSQL_HOST=127.0.0.1
- MYSQL_PORT=3306
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_MY_DATABASE=test
networks:
frontend: null
backend: null
Dockerfile for PHP:
FROM php:7.2.7-fpm-alpine3.7
RUN apk update; \
apk upgrade;
RUN docker-php-ext-install mysqli
I had tested it with the following approach:
docker exec -ti <mysql_docker> bash
and used the same crendatial for entering the mysql mode:
mysql -uroot -h127.0.0.1 -P3306 -p
and there is not problem with it.
So what may cause the above connection refused?
I set up 2 projects (Admin and API) and try to move into docker on local.
I can access the running web instances on both without any problems, but when the Admin tries to make a Curl requests to the API, I get a cURL error:
cURL error 7: Failed to connect to localhost port 8080
This is my docker-compose.yml file contents:
version: "3.1"
services:
memcached:
image: memcached:alpine
container_name: project-admin-memcached
redis:
image: redis:alpine
container_name: project-admin-redis
mariadb:
image: mariadb:10.1
container_name: project-admin-mariadb
working_dir: /application
volumes:
- ./Projects:/application
environment:
- MYSQL_ROOT_PASSWORD=docker
- MYSQL_DATABASE=db_test
- MYSQL_USER=test
- MYSQL_PASSWORD=test
ports:
- "8083:3306"
# docker-compose exec webserver sh
# docker exec -it project-admin-webserver nginx -s reload
webserver:
image: nginx:alpine
container_name: project-admin-webserver
working_dir: /application
volumes:
- ./Projects/Api:/application/api
- ./Projects/Admin:/application/admin
- ./Docker/nginx:/etc/nginx/conf.d
ports:
- "8080:8080"
- "8090:8090"
# docker-compose exec php-fpm bash
php-fpm:
build: Docker/php-fpm
container_name: project-admin-php-fpm
working_dir: /application
volumes:
- ./Projects:/application
- ./Docker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
I can access both projects from my browser with:
http://localhost:8080/ <= API
http://localhost:8090/ <= Admin
How can I fix this?
inside your docker network (create by default with a compose), you have to use the container name.
So inside a container you have to use http://webserver:8080
I have this docker stack running nginx, php, and mariadb.
Now I want to add NodeJS in order to migrate my platform to services, one at the time.
I'm trying to add node in the same way I added php,but when I run "docker-compose up", nodejs appears "Exited with code 254".
It appears to be a ENOET error related with my package.json file, but I'm already coping my file into the container, also if I run everything less nodejs, when I access my nginx container the package.json appears in the route it means to be, so I don't know why I'm seeing this error.
Here is my docker-compose.yml
nginx:
image: tutum/nginx
ports:
- "80:80"
- "8080:8080"
# - "8081:8081"
# - "8082:8082"
# - "8083:8083"
# - "8090:8090"
links:
- nodejs
- phpfpm
- mariadb
volumes:
- ./public/leal-api:/var/www
- ./nginx/default:/etc/nginx/sites-available/default
- ./nginx/default:/etc/nginx/sites-enabled/default
- ./public:/usr/share/nginx/html
nodejs:
command: npm start
build: ./node
ports:
- "3000:3000"
links:
- mariadb
volumes:
- ./public/leal-api:/var/www
phpfpm:
build: ./php
ports:
- "9000:9000"
links:
- mariadb
volumes:
- ./public:/usr/share/nginx/html
mariadb:
image: mariadb
environment:
MYSQL_DATABASE: leal
MYSQL_USER: lealadm
MYSQL_PASSWORD: leal2015*
MYSQL_ROOT_PASSWORD: LealColombia2017!
ports:
- "3306:3306"
And here is the Dockerfile of the nodejs
FROM node:7.10
VOLUME ["/var/www"]
# WORKDIR /src
WORKDIR /var/www
# COPY . /src
# RUN npm install
# RUN npm install -g nodemon #hmm idk
EXPOSE 3000
CMD ["npm", "start"]
Thanks in advance for any help that you can provide.