multiple docker compose container - single ip - networking

I have this application.
How can I assign a single ip-address to the web-service and use the database within?
version: "2"
services:
web:
image: kartoffeltoby/typo3:latest
hostname: localhost
container_name: web
ports:
- 80:80
link:
- db:database
volumes:
- ./www/:/var/www/
environment:
- DOCROOT=/var/www/web
- PAGESPEED=Off
db:
image: mysql:5.6
hostname: database
volumes:
- ./DB/:/var/lib/mysql/
- ./db/:/var/tmp/db
environment:
- MYSQL_ROOT_PASSWORD=passwort

The solution is:
network_mode: service:web
version: "2"
services:
typo3:
image: kartoffeltoby/typo3:latest
hostname: axdbw.vagrant
container_name: typo3
volumes:
- ./www/:/var/www/
environment:
- DOCROOT=/var/www/web
- PAGESPEED=Off
networks:
mynet:
ipv4_address: 172.16.47.14
db:
image: mysql:5.6
volumes:
- ./DB/:/var/lib/mysql/
- ./db/:/var/tmp/db
environment:
- MYSQL_ROOT_PASSWORD=passwort
network_mode: service:web
networks:
mynet:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.16.47.0/24

There is no reason to use a specific IP. The solution can be as simple as this:
version: "3"
services:
typo3:
image: kartoffeltoby/typo3:latest
container_name: typo3
networks:
- mynet
db:
image: mysql:5.6
network_mode: "service:typo3"
networks:
mynet:
driver: bridge
This will place the db container onto the same IP as the typo3 container. Beware of possible port collisions.
In my use-case, I had to avoid nasty cross origin errors. Hosting the database on the same IP as my frontend container helped get around those issues.

Related

Can't get a Docker image of both PHPMyAdmin and MariaDB to work together

I'm using Docker and a docker-compose.yml file to pop up a WordPress site using Bitnami and MariaDB. With just these two services alone, everything is fine, but I have no way to view/access the data in MariaDB. To solve the issue, I added the PHPMyAdmin service to the compose file since I'm familiar with that service. However, one of two problems happen: either all three services stay up and work fine and I can't login to PHPMyAdmin, or MariaDB shuts down every time immediately after starting up and the whole image doesn't work.
This configuration keeps the image up, but doesn't let me login to PHPMyAdmin...
services:
# Database
mariadb:
image: docker.io/bitnami/mariadb:latest
container_name: test_mariadb_database
volumes:
- 'mariadb_data:/bitnami/mariadb'
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- MARIADB_USER=test_bn_wp
- MARIADB_DATABASE=test_bitnami_wordpress
networks:
- test_bn
# phpmyadmin
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: test_pma
links:
- mariadb
environment:
PMA_HOST: mariadb
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
# Wordpress
wordpress:
image: docker.io/bitnami/wordpress:latest
ports:
- '80:8080'
- '443:8443'
restart: always
container_name: test_bitnami_wordpress
volumes: ['./docroot:/bitnami/wordpress']
depends_on:
- mariadb
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- WORDPRESS_DATABASE_HOST=mariadb
- WORDPRESS_DATABASE_PORT_NUMBER=3306
- WORDPRESS_DATABASE_USER=test_bn_wp
- WORDPRESS_DATABASE_NAME=test_bitnami_wordpress
networks:
- test_bn
networks:
test_bn:
volumes:
mariadb_data:
driver: local
wordpress_data:
driver: local
So I change to this configuration, thinking that the password can't be blank, but it causes MariaDB to shut down immediately after starting up, thus rendering the whole image useless...
services:
# Database
mariadb:
image: docker.io/bitnami/mariadb:latest
container_name: test_mariadb_database
volumes:
- 'mariadb_data:/bitnami/mariadb'
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=no
- MARIADB_USER=test_bn_wp
- MARIADB_PASSWORD=test_password
- MARIADB_ROOT_PASSWORD=test_password
- MARIADB_DATABASE=test_bitnami_wordpress
networks:
- test_bn
# phpmyadmin
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: test_pma
links:
- mariadb
environment:
PMA_HOST: mariadb
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
# Wordpress
wordpress:
image: docker.io/bitnami/wordpress:latest
ports:
- '80:8080'
- '443:8443'
restart: always
container_name: test_bitnami_wordpress
volumes: ['./docroot:/bitnami/wordpress']
depends_on:
- mariadb
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- WORDPRESS_DATABASE_HOST=mariadb
- WORDPRESS_DATABASE_PORT_NUMBER=3306
- WORDPRESS_DATABASE_USER=test_bn_wp
- WORDPRESS_DATABASE_NAME=test_bitnami_wordpress
networks:
- test_bn
networks:
test_bn:
volumes:
mariadb_data:
driver: local
wordpress_data:
driver: local
Edit - This is the error I get when trying to login to PHPMyAdmin with the first config.

nextcloud in docker-compose not reachable

I am using docker compose on my raspberry pi. In the yaml file I have a nextcloud and nginx (for outside communication). But as my raspberry pi is in my private network, I want to have also a direct connection for my immobile setup.
Something like an ip: 192.168.0.100:1080
What I have tried:
version: '3'
volumes:
nextcloud-data:
nextcloud-db:
npm-data:
npm-ssl:
npm-db:
networks:
frontend:
backend:
services:
nextcloud-app:
image: nextcloud
restart: always
ports:
- "1080:80"
- "1443:443"
volumes:
- nextcloud-data:/var/www/html
environment:
- MYSQL_PASSWORD=replace-with-secure-password
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
- MYSQL_HOST=nextcloud-db
networks:
- frontend
- backend
nextcloud-db:
image: mariadb
restart: always
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
volumes:
- nextcloud-db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=replace-with-secure-password
- MYSQL_PASSWORD=replace-with-secure-password
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
networks:
- backend
npm-app:
image: jc21/nginx-proxy-manager:latest
restart: always
ports:
- "80:80"
- "81:81"
- "443:443"
environment:
- DB_MYSQL_HOST=npm-db
- DB_MYSQL_PORT=3306
- DB_MYSQL_USER=npm
- DB_MYSQL_PASSWORD=replace-with-secure-password
- DB_MYSQL_NAME=npm
volumes:
- npm-data:/data
- npm-ssl:/etc/letsencrypt
networks:
- frontend
- backend
npm-db:
image: jc21/mariadb-aria:latest
restart: always
environment:
- MYSQL_ROOT_PASSWORD=replace-with-secure-password
- MYSQL_DATABASE=npm
- MYSQL_USER=npm
- MYSQL_PASSWORD=replace-with-secure-password
volumes:
- npm-db:/var/lib/mysql
networks:
- backend
Additional information
The docker is started with docker-compose up -d
Nginx is not configured yet
Nextcloud is not configured yet
But with this approach the address 192.168.0.100:1080 does not resolve. Is this the right way to do it or why is the port 1080 not reachable?

How to add wp-cli to docker wordpress?

Is there a way to do this without having to go into the container and install it? Seems kind of weird they wouldn't include this with the Wordpress image. Anyway - Heres what I have at the moment.
How would you do this?
version: '3.3'
services:
db:
image: mysql
restart: always
volumes:
- mysql_data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_DATABASE=admin
- MYSQL_USER=admin
- MYSQL_PASSWORD=admin
wordpress:
image: wordpress
depends_on:
- db
ports:
- '1234:80'
restart: always
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
- WORDPRESS_DB_HOST=db:3306
- WORDPRESS_DB_USER=admin
- WORDPRESS_DB_PASSWORD=admin
- WORDPRESS_DB_NAME=admin
- WORDPRESS_TABLE_PREFIX=admin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
- PMA_HOST=db
- MYSQL_ROOT_PASSWORD=admin
volumes:
mysql_data:
driver: local
You can use one of the images tagged with cli, such as:
image: worpress:cli-php7.4
Find the list of available tags on Docker Hub

Traefik not routing to other containers

I've set up Traefik and Portainer on my server running Ubuntu 20.04 that is in my front room (I used this guide and this one, but didn't set up the default IP whitelist in the second tutorial as I want it to be a publicly accessible webserver). Both apps work and appear to be using HTTPS. I can manage and create containers in Portainer.
To test out my configuration, I added two containers - MySQL and Wordpress. I added in the Traefik labels from the above tutorials like when I set up Traefik, and I set the Wordpress container's domain name in Portainer, but whenever I try to access the Wordpress site at that domain, I get a Bad Gateway error (just the words 'Bad Gateway', not even a status code).
I'm not sure where I've gone wrong. Here are my configuration files:
traefik.yml:
api:
dashboard: true
entryPoints:
http:
address: ":80"
https:
address: ":443"
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
file:
filename: /config.yml
version: '3'
services:
traefik:
image: traefik:v2.0
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
networks:
- proxy
ports:
- 80:80
- 443:443
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./data/traefik.yml:/traefik.yml:ro
- ./data/acme.json:/acme.json
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=http"
- "traefik.http.routers.traefik.rule=Host(`traefik.mywebsite.com`)"
- "traefik.http.middlewares.traefik-auth.basicauth.users=michael:$$apr1$$.m1mfSB0$$6Ypx6rfih8y.vHkNQe9rJ0"
- "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
- "traefik.http.routers.traefik-secure.entrypoints=https"
- "traefik.http.routers.traefik-secure.rule=Host(`traefik.mywebsite.com`)"
- "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
- "traefik.http.routers.traefik-secure.tls=true"
- "traefik.http.routers.traefik-secure.tls.certresolver=http"
- "traefik.http.routers.traefik-secure.service=api#internal"
networks:
proxy:
external: true
certificatesResolvers:
http:
acme:
email: me#myemail.com
storage: acme.json
httpChallenge:
entryPoint: http
config.yml:
http:
middlewares:
https-redirect:
redirectScheme:
scheme: https
docker-compose.yml:
version: '3'
services:
traefik:
image: traefik:v2.0
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
networks:
- proxy
ports:
- 80:80
- 443:443
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./data/traefik.yml:/traefik.yml:ro
- ./data/acme.json:/acme.json
- ./data/config.yml:/config.yml:ro
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=http"
- "traefik.http.routers.traefik.rule=Host(`traefik.mywebsite.com`)"
- "traefik.http.middlewares.traefik-auth.basicauth.users=michael:$$apr1$$.m1mfSB0$$6Ypx6rfih8y.vHkNQe9rJ0"
- "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
- "traefik.http.routers.traefik-secure.entrypoints=https"
- "traefik.http.routers.traefik-secure.rule=Host(`traefik.mywebsite.com`)"
- "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
- "traefik.http.routers.traefik-secure.tls=true"
- "traefik.http.routers.traefik-secure.tls.certresolver=http"
- "traefik.http.routers.traefik-secure.service=api#internal"
networks:
proxy:
external: true
Wordpress/MySQL docker-compose.yml:
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpressdb
volumes:
- wordpress:/var/www/html
networks:
- proxy
labels:
- "traefik.enable=true"
- "traefik.http.routers.wordpress.entrypoints=http"
- "traefik.http.routers.wordpress.rule=Host(`myblog.com`)"
- "traefik.http.routers.wordpress.middlewares=https-redirect#file"
- "traefik.http.routers.wordpress-secure.entrypoints=https"
- "traefik.http.routers.wordpress-secure.rule=Host(`myblog.com`)"
- "traefik.http.routers.wordpress-secure.tls=true"
- "traefik.http.routers.wordpress-secure.tls.certresolver=http"
- "traefik.http.routers.wordpress-secure.service=wordpress"
- "traefik.http.services.wordpress.loadbalancer.server.port=9000"
- "traefik.docker.network=proxy"
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
networks:
- proxy
labels:
- "traefik.enable=true"
- "traefik.http.routers.mysql.entrypoints=http"
- "traefik.http.routers.mysql.middlewares=https-redirect#file"
- "traefik.http.routers.mysql-secure.entrypoints=https"
- "traefik.http.routers.mysql-secure.tls=true"
- "traefik.http.routers.mysql-secure.tls.certresolver=http"
- "traefik.http.routers.mysql-secure.service=mysql"
- "traefik.http.services.mysql.loadbalancer.server.port=9000"
- "traefik.docker.network=proxy"
volumes:
wordpress:
db:
networks:
proxy:
external: true
I can provide the Portainer docker-compose.yml file too if needed, but I don't really think it's necessary. Any help here would be great!
For network connectivity between the different applications you must create the network in one of your applications. I would do that in your traefik docker-compose.yml
Meaning, that in your traefik compose file you must NOT specify the proxy network as external, because you create it internally in that application like this:
networks:
proxy:
In your Wordpress/MySQL docker-compose.yml you must specify a name for the external network like this:
networks:
proxy:
external:
name: "traefik_proxy"
When you create a new application using compose, everything in the application gets a prefix, that is the directoryname in which the compose file is placed.
Meaning the above example only works if your traefik compose file is placed in a directory named "traefik"
This should fix your issue with connectivity.

Unable to connect container port from localhost when network_mode: "bridge" (docker-compose)

This is my docker-compose.yml
yml
version: '2'
services:
admin_db:
build:
context: .
dockerfile: postgres.dockerfile
args:
- DB_NAME=admin_db
- DB_USER=admin
- DB_PASSWORD=admin_pass
network_mode: "default"
admin:
build:
context: .
dockerfile: admin.dockerfile
args:
- UID=$UID
- GID=$GID
- UNAME=$UNAME
command: /bin/bash
depends_on:
- admin_db
ports:
- "8000:8000"
links:
- admin_db
network_mode: "bridge"
If with networking_mode:"bridge" I should be able to access my app (admin) on http://127.0.0.1:8000/ from localhost, but currently, I'm able to access it only on random-ip:8000 from localhost.
I'm able to http://127.0.0.1:8000/ access when networking_mode is "host", but then I'm unable to link containers.
Is there any solution to have both things ?
- linked containers
- app running on http://127.0.0.1:8000/ from localhost
If for some unknown reason normal linking doesn't work you can always create another bridged network and connect directly to that docker image. By doing that IP address of that running image will always be the same.
I would edit it like this:
version: '2'
services:
admin_db:
build:
context: .
dockerfile: postgres.dockerfile
args:
- DB_NAME=admin_db
- DB_USER=admin
- DB_PASSWORD=admin_pass
networks:
back_net:
ipv4_address: 11.0.0.2
admin:
build:
context: .
dockerfile: admin.dockerfile
args:
- UID=$UID
- GID=$GID
- UNAME=$UNAME
command: /bin/bash
depends_on:
- admin_db
ports:
- "8000:8000"
extra_hosts:
- "admin_db:11.0.0.2"
networks:
back_net:
ipv4_address: 11.0.0.3
networks:
back_net:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
com.docker.network.bridge.name: "back"
ipam:
driver: default
config:
- subnet: 11.0.0.0/24
gateway: 11.0.0.1
Hope that helps.

Resources