Page keeps loading when using gulp browser-sync in Docker environment - docker

I have set up a Docker environment for my Drupal installation, I created a theme and I want it to work with gulp and browser-sync. When I run my gulp script with browser-sync that is shown below it opens a new browser tab but the page just keeps loading.
My Docker file:
version: "2"
services:
mariadb:
image: wodby/mariadb:$MARIADB_TAG
container_name: "${PROJECT_NAME}_mariadb"
stop_grace_period: 30s
environment:
MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
php:
image: wodby/drupal-php:$PHP_TAG
container_name: "${PROJECT_NAME}_php"
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
DB_HOST: $DB_HOST
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
DB_NAME: $DB_NAME
DB_DRIVER: $DB_DRIVER
volumes:
- ./:/var/www/html
nginx:
image: wodby/nginx:$NGINX_TAG
container_name: "${PROJECT_NAME}_nginx"
depends_on:
- php
environment:
NGINX_STATIC_OPEN_FILE_CACHE: "off"
NGINX_ERROR_LOG_LEVEL: debug
NGINX_BACKEND_HOST: php
NGINX_SERVER_ROOT: /var/www/html/web
NGINX_VHOST_PRESET: $NGINX_VHOST_PRESET
volumes:
- ./:/var/www/html
labels:
- 'traefik.backend=nginx'
- 'traefik.port=80'
- 'traefik.frontend.rule=Host:${PROJECT_BASE_URL}'
pma:
image: phpmyadmin/phpmyadmin
container_name: "${PROJECT_NAME}_pma"
environment:
PMA_HOST: $DB_HOST
PMA_USER: $DB_USER
PMA_PASSWORD: $DB_PASSWORD
PHP_UPLOAD_MAX_FILESIZE: 1G
PHP_MAX_INPUT_VARS: 1G
labels:
- 'traefik.backend=pma'
- 'traefik.port=80'
- 'traefik.frontend.rule=Host:pma.${PROJECT_BASE_URL}'
portainer:
image: portainer/portainer
container_name: "${PROJECT_NAME}_portainer"
command: --no-auth -H unix:///var/run/docker.sock
volumes:
- /var/run/docker.sock:/var/run/docker.sock
labels:
- 'traefik.backend=portainer'
- 'traefik.port=9000'
- 'traefik.frontend.rule=Host:portainer.${PROJECT_BASE_URL}'
traefik:
image: traefik
container_name: "${PROJECT_NAME}_traefik"
command: -c /dev/null --web --docker --logLevel=INFO
ports:
- '8000:80'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
My gulpfile:
(I'm not using that environmental variable, but it is to show what url I used there.)
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
gulp.task('browser-sync', function() {
browserSync.init({
proxy: "${PROJECT_BASE_URL}"
});
});

I guess your frontend is running on the nginx-container or php-container.
Try to use:
...
proxy: "http://nginx"
...
or
...
proxy: "http://php"
...
I think you should at least be able to see your browser-sync at your configurated localhost with the right port.
Maybe it does help but I am not sure either.

Related

PHPUnit Doesn't Connect To Database

I have built a project based on Docker Sail (Ubuntu on windows),
the project is working fine, except when PHPUnit connects with the Database.
I tried to install mysql using "sudo apt-get install php-mysql"
now I get this error
here is my docker file
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- './_dockerdata/sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
#phpmyadmin
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
restart: unless-stopped
ports:
- 8085:80
environment:
- PMA_ARBITRARY=1
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
here is my phpunitxml regarding using tests
<server name="APP_ENV" value="testing"/>
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
here is .env file regarding db connection
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
Please try to change DB_HOST to localhost. Mysql service should run at localhost/127.0.0.1 inside docker container.

Make call from one docker container to another one

So I have two different applications one wordpress and other is api. And both running on docker containers and have their own configurations. This is their docker-compose settings:
version: "3.8"
services:
app:
container_name: ${APP_NAME}_app
build:
context: .
dockerfile: ./.docker/php/Dockerfile
expose:
- 9000
volumes:
- .:/usr/src/app
- ./public:/usr/src/app/public
depends_on:
- db
networks:
- app_network
nginx:
container_name: ${APP_NAME}_nginx
build:
context: .
dockerfile: ./.docker/nginx/Dockerfile
volumes:
- ./public:/usr/src/app/public
ports:
- "8081:8081"
expose:
- 8081
environment:
NGINX_FPM_HOST: app
NGINX_ROOT: /usr/src/app/public
depends_on:
- app
networks:
- app_network
db:
container_name: ${APP_NAME}_db
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
networks:
- app_network
networks:
app_network:
driver: bridge
volumes:
db_data:
driver: local
And this is my wordpress configuration:
version: '3.8'
services:
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
expose:
- 3306
- 33060
healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD
interval: 1s
timeout: 3s
retries: 30
networks:
- app_network
wordpress:
build:
context: .
dockerfile: Dockerfile
depends_on:
mysql:
condition: service_healthy
volumes:
- .:/var/www/html/wp-content/plugins/name
ports:
- "80:80"
restart: always
environment:
- WORDPRESS_URL=http://localhost
- WORDPRESS_DB_HOST=mysql
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
networks:
- app_network
networks:
app_network:
driver: bridge
And when I try to make request to api to this URL http://localhost:8081 well nothing happens. Locally works everything fine but on docker it doesn't.
Would appreciate some help how to make this work :)
If you have a docker container and you do http://localhost:8081, you wont go to your host pc, but to the container itself.
In docker-compose you need to replace localhost with the service name:
For example if you want to access 8081 of the nginx, you need to connect to http://nginx:8081

Docker Compose Adminer

I want to use the adminer image that is raised with the following docker command:
docker run \
--rm
-p 8000:80
-e MEMORY=512M
-e UPLOAD=4096M
dockette/adminer:full
My docker-compose.yml is as follows:
version: '2.4'
services:
mariadb:
container_name: mariadb-prueba
image: mariadb
restart: always
volumes:
- ./mariadb-data:/var/lib/mysql
environment:
MYSQL_DATABASE: 'db_prueba'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'admin'
MARIADB_ROOT_PASSWORD: 'admin'
ports:
- 2000:3306
adminer:
container_name: adminerprueba
image: dockette/adminer
restart: always
ports:
- 8000:80
environment:
- UPLOAD=4096M
- MEMORY=512M
volumes:
mariadb-data:
The problem is that it runs but when I do localhost:8000 the system appears to be down.
Am I missing something in the docker-compose

Preventing development containers from running in production

I have a docker-compose.yml file that includes a container for API mocks as well as phpmyadmin and mongo-express containers none of which should be started in my production environment.
I already have seperate .env files for production and development. Is it possible to use variables from the active .env file to disable a container?
Here is my docker-compose.yml:
services:
mysql:
build: ./docker/mysql
command: --default-authentication-plugin=mysql_native_password
container_name: mysql
entrypoint: sh -c "/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USERNAME}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_RANDOM_ROOT_PASSWORD=yes
- MYSQL_ONETIME_PASSWORD=yes
ports:
- 3306:3306
restart: unless-stopped
volumes:
- ./data/mysql:/var/lib/mysql
phpmyadmin:
container_name: phpmyadmin
environment:
- PMA_HOST=mysql
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
mongo:
build: ./docker/mongo
container_name: mongo
environment:
- MONGO_INITDB_DATABASE=${MONGO_DATABASE}
- MONGO_INITDB_ROOT_USERNAME=${MONGO_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASSWORD}
ports:
- 27017:27017
restart: unless-stopped
volumes:
- ./data/mongo:/data/db
mongo-express:
build: ./docker/mongo-express
container_name: mongo-express
depends_on:
- mongo
environment:
- ME_CONFIG_BASICAUTH_PASSWORD=redacted
- ME_CONFIG_BASICAUTH_USERNAME=username
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_USERNAME}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_PASSWORD}
ports:
- 8081:8081
redis:
build: ./docker/redis
container_name: redis
ports:
- 6379:6379
restart: unless-stopped
volumes:
- ./data/redis:/data
mock-apis:
build: ./docker/mock-apis
container_name: mock-apis
command: >
/initscript.bash
ports:
- 81:80
volumes:
- ./mock-apis:/home/nodejs
php-fpm:
build:
context: ./docker/php-fpm
args:
HOST_UID: ${HOST_UID}
command: >
/initscript.bash
container_name: php-fpm
restart: unless-stopped
depends_on:
- mongo
- mysql
- redis
volumes:
- ./laravel:/var/www/
nginx:
build: ./docker/nginx
container_name: nginx
depends_on:
- php-fpm
ports:
- 80:80
restart: unless-stopped
volumes:
- ./laravel:/var/www/
version: "3"
I'm using profiles to scope my services. If I want to use PhpMyAdmin only in dev I add this profile to the service:
phpmyadmin:
container_name: phpmyadmin
environment:
- PMA_HOST=mysql
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
profiles: ["dev"]
So now I have to tell to docker compose if I want to use the dev profile. Else it will not start.
You can use one of these command (with this way you have to type --profile your_profil for each profile):
$ docker-compose --profile dev up -d
$ docker-compose --profile dev --profile profil2 up -d <- for multiple profiles
Or the cleaner way you can separate your services with a comma:
$ COMPOSE_PROFILES=dev,profil2 docker-compose up -d
Services without a profiles attribute will always be enabled.
Care about when you stop your services you have to specify the profile too:
$ COMPOSE_PROFILES=dev docker-compose down

Can't access docker url via curl or Postman

I can access my site in the browser with the following path: my-dash.docker.localhost:8000
I am trying to write an API endpoint and can't access the site via Postman or curl.
curl my-dash.docker.localhost:8000
curl: (6) Could not resolve host: my-dash.docker.localhost
This is my docker-compose.yml:
version: "2"
services:
mariadb:
image: wodby/mariadb:10.1-2.1.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: drupal
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
volumes:
- ./mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) here.
php:
image: wodby/drupal:8-7.1-2.1.2
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
DB_HOST: mariadb
DB_USER: drupal
DB_PASSWORD: drupal
DB_NAME: drupal
DB_DRIVER: mysql
PHP_XDEBUG: 1
PHP_XDEBUG_DEFAULT_ENABLE: 1
PHP_XDEBUG_REMOTE_CONNECT_BACK: 0 # This is needed to respect remote.host setting bellow
PHP_XDEBUG_REMOTE_HOST: "10.254.254.254" # You will also need to 'sudo ifconfig lo0 alias 10.254.254.254'
volumes:
- mydash-sync:/var/www/html:nocopy # Docker-sync for macOS users
nginx:
image: wodby/drupal-nginx:8-1.10-2.1.0
depends_on:
- php
environment:
NGINX_STATIC_CONTENT_OPEN_FILE_CACHE: "off"
NGINX_ERROR_LOG_LEVEL: debug
NGINX_BACKEND_HOST: php
NGINX_SERVER_ROOT: /var/www/html/web
volumes:
- mydash-sync:/var/www/html:nocopy # Docker-sync for macOS users
labels:
- 'traefik.backend=nginx'
- 'traefik.port=80'
- 'traefik.frontend.rule=Host:my-dash.docker.localhost'
solr:
image: wodby/drupal-solr:8-6.4-2.0.0
environment:
SOLR_HEAP: 1024m
labels:
- 'traefik.backend=solr'
- 'traefik.port=8983'
- 'traefik.frontend.rule=Host:solr.my-dash.docker.localhost'
mailhog:
image: mailhog/mailhog
labels:
- 'traefik.backend=mailhog'
- 'traefik.port=8025'
- 'traefik.frontend.rule=Host:mailhog.my-dash.docker.localhost'
traefik:
image: traefik
command: -c /dev/null --web --docker --logLevel=INFO
ports:
- '8000:80'
- '8080:8080' # Dashboard
volumes:
- /var/run/docker.sock:/var/run/docker.sock
volumes:
mydash-sync:
external: true
You have to edit your /etc/hosts file to get your computer resolving that name. Add this at the end of the file:
127.0.0.1 my-dash.docker.localhost

Resources