How to link PostgreSQL container and PgAdmin in Docker? - docker

I have a docker-compose file with postgres and pgadmin services in my Laravel project. The problem is that when I do the database migration, it succeeds, but when I go to pgadmin, I do not see the tables I created there. I guess I, as always, made a mistake while writing docker-compose and I just can't find what I was missing.
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: 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:
- postgres
#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:
- postgres
#Redis
redis:
image: 'redis:alpine'
ports:
- "6379:6379"
#PostgreSQL
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-5050}:80"
networks:
- postgres
restart: unless-stopped
#RabbitMQ
rabbit:
image: "rabbitmq:3-management"
hostname: "rabbit"
environment:
RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
RABBITMQ_DEFAULT_USER: "rabbitmq"
RABBITMQ_DEFAULT_PASS: "rabbitmq"
RABBITMQ_DEFAULT_VHOST: "/"
ports:
- "15672:15672"
- "5672:5672"
labels:
NAME: "rabbitmq"
#ElasticSearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
ports:
- "9200:9200"
- "9300:9300"
networks:
- postgres
#Docker Networks
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:

In pgAdmin, when you add a new server, you need to define "host.docker.internal" (when on Windows) as Host (How to add a new server).

Related

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

How do I give a container a static ip, or how do I link two Docker containers?

I have a container with RabbitMQ, it has to connect to the external IP of the application container, the problem is that this external IP is dynamic and changes every time. Accordingly, I have to manually change the configuration of my RabbitMQ every time. Maybe you can somehow set a static ip for the "App" container so that it does not change or somehow connect these two containers?
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: 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:
- postgres
#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:
- postgres
#Redis
redis:
image: 'redis:alpine'
ports:
- "6379:6379"
#PostgreSQL
postgres:
container_name: postgres_container
image: postgres
hostname: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-5050}:80"
networks:
- postgres
restart: unless-stopped
depends_on:
- postgres
#RabbitMQ
rabbit:
image: "rabbitmq:3-management"
hostname: "rabbit"
environment:
RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
RABBITMQ_DEFAULT_USER: "rabbitmq"
RABBITMQ_DEFAULT_PASS: "rabbitmq"
RABBITMQ_DEFAULT_VHOST: "/"
ports:
- "15672:15672"
- "5672:5672"
labels:
NAME: "rabbitmq"
networks:
- postgres
#ElasticSearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
ports:
- "9200:9200"
- "9300:9300"
networks:
- postgres
#Docker Networks
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
If they are in the same networks You can simply use the service name instead of the IP, just use the app, You can ping the app service name inside the rabbitMQ container and it works.
as you are using docker-compose all your containers are already on the same network. You can access a container from another container using the name you have specified:
i.e for rabbitmq it is rabbit
for redis it will be redis

phpMyAdmin not accessible from Docker

I have the following docker-compose file:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./vDocker/php/php.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
- ./vDocker/.env:/var/www/.env
- ./vDocker/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
- ./vDocker/.env:/var/www/.env
- ./vDocker/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: translive
MYSQL_ROOT_PASSWORD: gio123
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./vDocker/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
depends_on:
- db
restart: unless-stopped
tty: true
ports:
- "8080:8080"
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: gio123
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I have four services. Three of them work fine and no problem at all, but phpmyadmin doesn't work at all. The thing is all is work fine, database, webserver, but not phpMyAdmin. When I try to go to localhost:8080, it says site can't be reached. I don't know what's going on. What am I missing?

How to open phpmyadmin as domain in localhost?

I have one docker-compose file that have nginx, mysql and phpmyadmin.
phpmyadmin is open in port 8080 in this address mydomain.com:8080 as well.
How can I convert this address to http://phpmyadmin.mydomain.com or http://pma.mydomain.com in my server?
I have used this docker-compose file:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./Dockerfile/Dockerfile
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./Beh:/var/www
- ./Config/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:
- ./Beh:/var/www
- ./Config/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
database:
image: mariadb:latest
container_name: database
environment:
- "MYSQL_USERNAME=root"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "3306:3306"
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
- "MYSQL_USERNAME=root"
- "MYSQL_ROOT_PASSWORD=secret"
- "PMA_HOST=database"
links:
- database
ports:
- "8080:80"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I don't know how to configure nginx file to achieve this goal.
You can use the repo -> https://github.com/jwilder/nginx-proxy
This is what I'm using to create new local domain name for each project.
You have to add "VIRTUAL_HOST" to the environment, expose the ports ("expose: 80") and also add the new domain in your /etc/hosts.

traefik not respecting frontend rule

I am trying to deploy multiple apps on my docker host and have traefik route traffic based on hostnames to the different apps
I am using docker-compose for all my docker containers
Here is my traeffik.yaml file
version: '3.5'
services:
traefik:
image: traefik
container_name: traefik
command: --api --docker
networks:
- traefik_network
ports:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
traefik_network:
name: traefik_network
here is my wpapp1.yaml file
version: '3.5'
services:
mysql:
image: mysql:5.7
volumes:
- wpapp1_mysql:/var/lib/mysql
restart: always
container_name: wpapp1_mysql
networks:
- traefik_network
environment:
MYSQL_ROOT_PASSWORD: wpapp1
MYSQL_DATABASE: wpapp1
MYSQL_USER: wpapp1
MYSQL_PASSWORD: wpapp1
wordpress:
depends_on:
- mysql
image: wordpress:latest
volumes:
- wpapp1_wordpress:/var/www/html
restart: always
container_name: wpapp1_wordpress
networks:
- traefik_network
labels:
- "traefik.frontend.rule=Host:wpapp1.example.com"
- "traefik.port=80"
- "traefik.docker.network=traefik_network"
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: wpapp1
WORDPRESS_DB_PASSWORD: wpapp1
volumes:
wpapp1_mysql:
name: wpapp1_mysql
wpapp1_wordpress:
name: wpapp1_wordpress
networks:
traefik_network:
external:
name: traefik_network
and here is my wpapp2.yaml file
version: '3.5'
services:
mysql:
image: mysql:5.7
volumes:
- wpapp2_mysql:/var/lib/mysql
restart: always
container_name: wpapp2_mysql
networks:
- traefik_network
environment:
MYSQL_ROOT_PASSWORD: wpapp2
MYSQL_DATABASE: wpapp2
MYSQL_USER: wpapp2
MYSQL_PASSWORD: wpapp2
wordpress:
depends_on:
- mysql
image: wordpress:latest
volumes:
- wpapp2_wordpress:/var/www/html
restart: always
container_name: wpapp2_wordpress
networks:
- traefik_network
labels:
- "traefik.frontend.rule=Host:wpapp2.example.com"
- "traefik.port=80"
- "traefik.docker.network=traefik_network"
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: wpapp2
WORDPRESS_DB_PASSWORD: wpapp2
volumes:
wpapp2_mysql:
name: wpapp2_mysql
wpapp2_wordpress:
name: wpapp2_wordpress
networks:
traefik_network:
external:
name: traefik_network
So now i expect traefik to route based on the hostnames wpapp1.example.com and wpapp2.example.com BUT traefik is loadbalancing traffic!!!
So when i go to http:/wpapp1.example.com, traefik is loadbalancing it between the two apps and same for the other hostnames. Now sure what is going on here since i specifically add the traefik.frontend.rule
I mean how in the hell is that happening?
I have spent hours to figure what is going on and before i go insane i decided to some here to get some help on what is going on here.
Put your database on a different network. Otherwise WordPress will RR load balance to the two mysql instances in the same docker network (that's the expected behavior when you have two containers with the same alias on the same network). You can do that with the default network:
version: '3.5'
services:
mysql:
image: mysql:5.7
volumes:
- mysql:/var/lib/mysql
restart: unless-stopped
networks:
- db
environment:
MYSQL_ROOT_PASSWORD: wpapp
MYSQL_DATABASE: wpapp
MYSQL_USER: wpapp
MYSQL_PASSWORD: wpapp
wordpress:
depends_on:
- mysql
image: wordpress:latest
volumes:
- wordpress:/var/www/html
restart: unless-stopped
networks:
- traefik
- db
labels:
- "traefik.frontend.rule=Host:wpapp1.example.com"
- "traefik.port=80"
- "traefik.docker.network=traefik_network"
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: wpapp
WORDPRESS_DB_PASSWORD: wpapp
volumes:
mysql:
wordpress:
networks:
db:
traefik:
external:
name: traefik_network

Resources