PhpStorm + Docker for mac. Does not assign a port - php

My docker-compose file:
version: '3.8'
services:
cache:
image: memcached:latest
restart: always
ports:
- "10001:11211"
server:
build: '.'
tty: true
working_dir: /var/www
environment:
DISABLE_DEFAULT_SERVER: 1
AUTORELOAD_PROGRAMS: "kid_api"
AUTORELOAD_ANY_FILES: 1
volumes:
- ../../Apps/Server:/var/www
- ./php/config/custom.ini:/usr/local/etc/php/conf.d/custom.ini
- ./supervisor/kid_api.conf:/etc/supervisor/service.d/kid_api.conf
ports:
- "9988:9988"
depends_on:
- cache
links:
- cache
My Api.php file into /var/www open socket port for daemon server with Swoole.
When i tried docker-compose up, then ports is ok:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c80bee2baf7 php74_swoole4510_base_server "/entrypoint.sh" 2 hours ago Up 1 second 0.0.0.0:9988->9988/tcp php74_swoole4510_base_server_1
3b0f67cc3295 memcached:latest "docker-entrypoint.s…" 24 hours ago Up 2 hours 0.0.0.0:10001->11211/tcp php74_swoole4510_base_cache_1
but if i tried to run with PhpStorm
then the ports are not open
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7b534100202 php74_swoole4510_base_server "php /var/www/Api.php" 2 seconds ago Up Less than a second php74_swoole4510_base_server_run_df9a640e18bb
3b0f67cc3295 memcached:latest "docker-entrypoint.s…" 24 hours ago Up 2 minutes 0.0.0.0:10001->11211/tcp php74_swoole4510_base_cache_1
How fix it?

docker-compose up is not equivalent to docker-compose run.
PhpStorm does docker-compose run service_name command.
Docker Compose itself doesn't map ports in this case, you can check it in the terminal.
You can spin up the services with docker-compose up in the terminal manually, and then use PhpStorm in the exec mode (the option is visible on your screenshot).

Related

Docker swarm php-fpm container keeps restarting

I created a basic LAPP stack which runs flawlessly on localhost with docker-compose. When I try to make it run with Swarm on production server (1 manager only, no workers), all services go up and get replicated (1/1) but the php-fpm one which keeps restarting with no apparent error.
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
p0rdrdfmso6x traefik_reverse-proxy replicated 1/1 traefik:v2.4 *:80->80/tcp, *:443->443/tcp
e6vwlo9iw2ny my_stack_apache replicated 1/1 apache:latest *:8000->80/tcp
qy5yigbcjryr my_stack_ftp replicated 1/1 fauria/vsftpd:latest *:20-21->20-21/tcp, *:22100-22110->22100-22110/tcp
n5f9v6bd2854 my_stack_php replicated 0/1 php:latest
rcnbq4vnoz1j my_stack_postgres replicated 1/1 postgres:9.5.24
If we focus on php-fpm container :
docker service ps my_stack_php
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
j6mp3ka40cyo my_stack_php.1 php:latest node.address Ready Ready 2 seconds ago
ezztpsjoglwy \_ my_stack_php.1 php:latest node.address Shutdown Complete 3 seconds ago
gnqjhwpi5y72 \_ my_stack_php.1 php:latest node.address Shutdown Complete 9 seconds ago
0agr3tw0bb9g \_ my_stack_php.1 php:latest node.address Shutdown Complete 15 seconds ago
9a6wsdp4tqqn \_ my_stack_php.1 php:latest node.address Shutdown Complete 21 seconds ago
If I look to the logs :
docker service logs my_stack_php
my_stack_php.1.igd8x7a6ysdi#node.address | Interactive shell
my_stack_php.1.57td5iuk1wwy#node.address | Interactive shell
my_stack_php.1.r03jn931l1uf#node.address | Interactive shell
my_stack_php.1.igd8x7a6ysdi#node.address |
my_stack_php.1.r03jn931l1uf#node.address |
my_stack_php.1.1huf2pdd0bq0#node.address | Interactive shell
my_stack_php.1.57td5iuk1wwy#node.address |
my_stack_php.1.1huf2pdd0bq0#node.address |
It behaves like a container running a command which would end with succes in a few seconds. Swarm launches then an new container to keep the restart contract. However, my php-fpm Dockerfile provide the -F argument that should keep the process running :
PHP Dockerfile
FROM centos:7.4
# ... all installs from Centos to add PHP 7.2 from Remi Collet repositories
RUN mkdir -p /run/php-fpm
RUN usermod -a -G ftp apache
WORKDIR /var/www/html
EXPOSE 9000
# Run in foreground as root (in container POV)
CMD ["php-fpm", "-R", "-F"]
docker-compose.yaml
version: '3.9'
services:
postgres:
image: "postgres:9.5.24"
environment:
POSTGRES_DB: /run/secret/postgres_db
POSTGRES_USER: /run/secret/postgres_user
POSTGRES_PASSWORD: /run/secret/postgres_password
volumes:
- database:/var/lib/postgresql/data
secrets:
- postgres_db
- postgres_user
- postgres_password
deploy:
resources:
limits:
cpus: '0.15'
memory: 128m
networks:
- internal
apache:
env_file: .env
image: apache:latest
build:
context: ./docker/images/apache2.4
dockerfile: prod.Dockerfile
ports:
- 8000:80
environment:
FPM_HOST: php:9000
volumes:
- ./docker/logs/apache/:/var/log/httpd/
networks:
- traefik-public
- internal
deploy:
labels:
- "traefik.enable=true"
- "traefik.http.routers.my_stack.rule=Host(`my-host.com`)"
- "traefik.http.routers.my_stack.entrypoints=websecure"
- "traefik.http.routers.my_stack.tls.certresolver=letsencryptresolver"
- "traefik.http.services.my_stack.loadbalancer.server.port=80"
- "traefik.port=80"
resources:
limits:
cpus: '0.15'
memory: 128m
php:
env_file: .env
image: php:latest
# links:
# - ftp
# - apache
build:
context: ./docker/images/php
dockerfile: prod.Dockerfile
# args:
# TIMEZONE: 'Europe/Paris'
volumes:
- ftp_data:/var/www/ftp:rw
networks:
- internal
deploy:
resources:
limits:
cpus: '0.20'
memory: 512m
ftp:
env_file: .env
image: "fauria/vsftpd:latest"
ports:
- "20:20"
- "21:21"
- "22100-22110:22100-22110"
environment:
FTP_USER: apache
FTP_PASS: /run/secret/automation_client_password
PASV_ADDRESS: 127.0.0.1
PASV_MIN_PORT: 22100
PASV_MAX_PORT: 22110
volumes:
- ftp_data:/home/vsftpd/apache:rw
networks:
- traefik-public
- internal
deploy:
resources:
limits:
cpus: '0.15'
memory: 128m
volumes:
ftp_data:
database:
secrets:
postgres_db:
external: true
postgres_user:
external: true
postgres_password:
external: true
automation_client_password:
external: true
networks:
traefik-public:
external: true
internal:
external: false
Anyone got a clue about this? Any helps/tips will be appreciated.
I don't like this kind of answer, but replica's all went live after complete reboot.

Docker - Nginx, PHP, MySQL - Laravel artisan migrate connection refused

I have Laravel running in Docker with 3 containers - Nginx, MySQL 8 And PHP 8.
I have the following docker-compose.yaml
version: '3.8'
services:
server:
build:
context: .
dockerfile: dockerfiles/nginx.dockerfile
ports:
- '8000:80'
volumes:
- ./src:/var/www/html
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
container_name: server
depends_on:
- app
- db
app:
build:
context: .
dockerfile: dockerfiles/php.dockerfile
volumes:
- ./src:/var/www/html:delegated
container_name: app
db:
image: mysql:8.0
env_file:
- ./env/mysql.env
container_name: db
This command:
docker-compose up -d server
Launches 3 containers:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ff5cbefd12e toolkit_server "/docker-entrypoint.…" 9 seconds ago Up 3 seconds 0.0.0.0:8000->80/tcp server
c7f3c9753909 toolkit_app "docker-php-entrypoi…" 14 seconds ago Up 8 seconds 9000/tcp app
d7b421bd3c3b mysql:8.0 "docker-entrypoint.s…" 14 seconds ago Up 9 seconds 3306/tcp, 33060/tcp db
I want to run php artisan migrate, so I try it like so:
docker-compose exec app php artisan migrate
This gives me the following error:
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = toolkit and table_name = migrations and table_type = 'BASE TABLE')
My mysql.env file is like so:
MYSQL_DATABASE=toolkit
MYSQL_USER=root
MYSQL_PASSWORD=password
MYSQL_ROOT_PASSWORD=password
AND .env:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=toolkit
DB_USERNAME=root
DB_PASSWORD=password
I've tried changing ports, host, but I just can't get access - I must have messed something up somewhere.
please try this in the app Section of the docker-compose.yml
links:
- db
In my haste, I was trying to run commands before mysql was ready. It can take 90 seconds after a rebuild for the container to accept connections.
docker logs db
Once it was ready to connect, my commands would run.

Access index.php from docker

I've created a docker with a database and a php server but I'm failing accessing the php file from the server.
For testing purpose I'm currently having 2 index.php in my test app ./index.php and ./app/index.php
This is my docker-compose.yml
version: '3'
services:
symfony:
build:
context: .
dockerfile: docker/Dockerfile
image: project-manager
ports:
- 80:80
db:
image: mysql
ports:
- 3306:3306
volumes:
- "./.data/db:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: root
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- "8080:80"
links:
- db
This is the php dockerfile
FROM php:7.4-fpm
# Install Composer
COPY --from=composer /usr/bin/composer /usr/bin/composer
# Copy all our files in the docker root
COPY . /
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f80a16af8336 project-manager "docker-php-entrypoi…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, 9000/tcp project-manager_symfony_1
d97688010adf phpmyadmin/phpmyadmin "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 0.0.0.0:8080->80/tcp project-manager_phpmyadmin_1
55781c004031 mysql "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp project-manager_db_1
In my /etc/hosts
#Project Manager
127.0.0.1 project-manager.local
I can successfully access to the phpmyadmin using project-manager.local:8080
But if I try the simple project-manager.local/ or project-manager.local/index
I've got an empty response.
Root cause:
For symfony, you bind 80:80, this means you suppose there is a port 80 open in the php container. But, you use php:7.4-fpm which will just open port 9000.
(If you install net-tools in the container & use netstat -oanltp to check, there won't be 80 port open.)
Solutions:
Option 1:
If you insist to use php-fpm, then you need another web server container to pass the 80 request to php container's 9000 port. Maybe could add a more service with nginx container, and refers to connecting-nginx-to-php-fpm to set your configure for nginx container:
fastcgi_pass symfony:9000;
Option 2:
Switch to use php:7.4-apache, which defaults has a web server in the image open the 80 port, like next:
Dockerfile:
FROM php:7.4-apache
COPY . /var/www/html
index.php:
<?php
phpinfo();
NOTE: you should copy files to /var/www/html.
In a word, you should assure the container which you expose 80:80 really have a port 80 open in the container, otherwise, your expose is useless...

How to increase load time on docker with nginx and php7-fpm on local machine

On my local machine, WordPress Page load time is very slow on docker with nginx and php7-fpm and in network call its shows 2 - 4 sec to load first doc. but when I calculate PHP execution time it shows me 0.02 - 0.1 sec. how can I optimize docker setup to speed up the local environment?
below are some details of my local environment
My Local Environment is set up on Mac Sierra and I run the docker by
docker-compose up -d
and here is my docker-compose.yml file
version: '2'
services:
mysql:
container_name: db
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=dummy
- MYSQL_DATABASE=dummy
- MYSQL_USER=dummy
- MYSQL_PASSWORD=dummy
volumes:
- dummy_path/dump.sql.gz:/docker-entrypoint-initdb.d/sql1.sql.gz
nginx:
container_name: nginx
image: nginx:latest
ports:
- "80:80"
- "443:443"
links:
- mysql:db
- php
volumes:
- dummy_path:/app/www
- dummy_path/nginx/conf.d/:/etc/nginx/conf.d/
- dummy_path/nginx/ssl:/etc/ssl/
- dummy_path/nginx/nginx.conf/:/etc/nginx/nginx.conf
- dummy_path/hosts:/etc/hosts
php:
container_name: php
image: droidhive/php-memcached
links:
- mysql:db
- memcached
volumes:
- dummy_path:/app/www
- dummy_path/php/custom.ini:/usr/local/etc/php/conf.d/custom.ini
- dummy_path/hosts:/etc/hosts
memcached:
container_name: memcached
image: memcached
volumes:
- dummy_path:/app/www
First thing I would try is to update your Dockerfile to ADD or COPY all your files into each image rather than mounting them as volumes. #fiber-optic mentioned this in the comments, but the new Dockerfile for your PHP container would be something like this:
FROM droidhive/php-memcached
ADD dummy_path:/app/www
ADD dummy_path/php/custom.ini:/usr/local/etc/php/conf.d/custom.ini
ADD dummy_path/hosts:/etc/hosts
Do this for at least the PHP container, but the MySQL container might also be an issue.
If that doesn't help or you can't get it to work, try adding :ro or :cached to each of your volumes.
:ro means "read-only", which allows your container to assume the volume won't change. Obviously this won't work if you need to do local dev with the code in a volume, but for some of your configuration files this will probably be fine.
:cached means that the host's files are authoritative, and the container won't constantly be checking for updates internally. This is usually ideal for code that you're editing on your host.

Mysql Exited on Docker Container

I want to make a web development platform on MacOS by using Docker. I installed nginx, and php7-fpm container and they're running and communicating each other. But after installing mysql container, mysql container was exited. I don't know why it exited.
This is docker ps -a output:
2955d2d5c392 nginx "/sbin/my_init" 38 seconds ago Up 36 seconds 0.0.0.0:8080->80/tcp dockertutorial_web_1
ec3c16795f05 php:7-fpm "docker-php-entrypoin" 38 seconds ago Up 37 seconds 9000/tcp dockertutorial_php_1
835e91ba927a mysql:latest "docker-entrypoint.sh" 38 seconds ago Exited (0) 37 seconds ago dockertutorial_mysql_1
As you can see, mysql was exited.
This is my docker-compose.yml file:
web:
image: nginx
ports:
- "8080:80"
volumes:
- ./src/public:/var/www/public
- ./src/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./src/public:/var/www/public
links:
- mysql
mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
command: "true"
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: project
MYSQL_USER: project
MYSQL_PASSWORD: project
Any suggesstion to solve it?
You are overriding command for MySQL image with true which will be run instead of mysqld.
Remove command: "true" from docker-compose.yml from mysql service and it will start mysqld.
See this Dockerfile for reference.
https://github.com/docker-library/mysql/blob/c207cc19a272a6bfe1916c964ed8df47f18479e7/5.7/Dockerfile#L63

Categories