How to run docker-compose with host network? - docker

I was reading a, here, here, here, here, here
none of which answers my question.
What I want to do?
I have a mysql docker-compose image and want to connect to it from my ubuntu host using
localhost:3306
this does not work
it does work if I use
0.0.0.0:3306
which is not what I want to do. Why do I want to do all of that? because I have to start to work on an oooold legacy app, that has an old mysql version. Now I have mysql 8.0 on my computer and dont want to downgrade just for that one project. The legacy code has about 1000 references to localhost:3306 in it. Now I could refactor all that, create a config file etc... but better would be, if I could make it work so that localhost:3306 actually accesses my mysql docker-compose container. Is that possible? What do i have to add to my docker-compose yaml file?
my mysql docker-compose yaml file is this:
version: '3.3'
services:
sciodb:
container_name: sciodb
image: mysql:5.6
restart: always
environment:
MYSQL_DATABASE: 'db'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'myuser'
# You can use whatever password you like
MYSQL_PASSWORD: 'test1234'
# Password for root access
MYSQL_ROOT_PASSWORD: 'test1234'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- /home/myuser/nmyapp_db:/var/lib/mysql
- /media/sf_vmwareshare:/var/vmwareshare

Related

Network Setting in Docker Compose file, to Join Wordpress Containers Together

I'm hosting 2 wordpress website on my VPS, and I'm using Nginx Proxy Manager to proxy them.
I use Docker network connect to join NPM & 2 Wordpress containers together to make them work, but after reload or restart docker the networks between them is broken. (Is that because I use systemctl restart docker? or compose down & up ?)
So now I decide to create a new network in docker called bridge_default, and put this network in docker compose file so I don't have to connect those containers together to make them work every time.
But now I don't know where is wrong in docker compose file, Can any one tell me how to put networks in docker compose file correctly?
version: "3"
# Defines which compose version to use
services:
# Services line define which Docker images to run. In this case, it will be MySQL server and WordPr> db:
image: mariadb:10.6.4-focal
# image: mysql:5.7 indicates the MySQL database container image from Docker Hub used in this inst> restart: always
networks:
- default
environment:
MYSQL_ROOT_PASSWORD: PassWord#123
MYSQL_DATABASE: wordpress
MYSQL_USER: admin
MYSQL_PASSWORD: PassWord#123
# Previous four lines define the main variables needed for the MySQL container to work: databas>
wordpress:
depends_on:
- db
image: wordpress:latest
restart: always
# Restart line controls the restart mode, meaning if the container stops running for any reason, > networks:
- default
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: PassWord#123
WORDPRESS_DB_NAME: wordpress
# Similar to MySQL image variables, the last four lines define the main variables needed for the Word> volumes:
["./wordpress:/var/www/html"]
volumes:
mysql: {}
networks:
default: bridge_default
Docker compose file
Docker networks
Can any one tell me how to put networks in docker compose file correctly?

How do Environment variables with different ports work in docker-compose mariadb?

They have sent me to do an exercise in docker and I have no idea how to do it, I find myself super lost.
What is requested is the following:
"Create a docker compose file that launches two Maria db databases, in 3 different environments "
"Depending on the environment, they should run on 3 different ports:
Development: 3306
Production: 3307
Testing: 3308
The environment should be sent as a parameter added to the command
docker compose "
The databases should be interconnected with each other
and greater than this: "With the command:
docker-compose docker-file --dev docker-compose docker-file --pre docker-compose docker-file --pro
passing that parameter, in the first one I would run a production environment, in the other preproduction and in the other development
In each environment there will be variables that change, such as the database port. "
Up to here is all the information that they have given me and everything that has been asked of me.
Could someone help me solve this ???
The only thing I managed to do was create the 2 databases, but I am missing the environment section, which is what I don't understand.
Code I have in a docker-compose.yml file:
version: '3'
services:
mariadb:
image: mariadb
restart: always
environment:
MYSQL_DATABASE: 'test'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'root'
MYSQL_ROOT_PASSWORD: 'root'
ports:
- 3306:3306
expose:
- "3306"
volumes:
- ./mariadb:/var/lib/mysql
mariadb2:
image: mariadb
environment:
MYSQL_DATABASE: 'test2'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'root'
MYSQL_ROOT_PASSWORD: 'root'
ports:
- 3305:3305
volumes:
- ./mariadb2:/var/lib/mysql
When you launch a docker-compose you can add the enviroment parameters by a .env file. I think that is what they ment when they asked you to do the exercise.
The .env file is loaded by default when you launch the docker-compose but you can specify it.
You may like to read the documentation first so you can try things out https://docs.docker.com/compose/environment-variables/

2 Drupal images in one machine

Description:
im trying to find a way to run 2 Drupal containers using a docker compose on a single machine. I am currently having a problem to 'bind' the each Drupal storage into a separate location in MySQL.
Here's the following YAML file for Docker Compose:
---
version: '3'
services:
drupal:
build: ./
image: geerlingguy/drupal:latest
environment:
DRUPAL_DATABASE_HOST: drupal-mysql
DRUPAL_DATABASE_PORT: 3306
DRUPAL_DATABASE_NAME: drupal
DRUPAL_DATABASE_USERNAME: drupal
DRUPAL_DATABASE_PASSWORD: drupal
# Generate a salt with: `php -r "echo bin2hex(random_bytes(25));"`
DRUPAL_HASH_SALT: db0de8a1556aa5348f87cfc950cd2c9641713d46e9412c8b05
ports:
#assign random port on the host
- "80"
depends_on:
- mysql
restart: always
# new
working_dir: /var/www/html
# Uncomment the volumes line and set to the local path of your Drupal
# installation, if you need to work with a local codebase.
volumes:
# - ~/Sites/drupal-container:/var/www/html:rw,delegated
- /Sites/drupal-container:/var/www/html:rw,delegated
drupal2:
build: ./
image: geerlingguy/drupal:latest
environment:
DRUPAL_DATABASE_HOST: drupal-mysql
DRUPAL_DATABASE_PORT: 3306
DRUPAL_DATABASE_NAME: drupal
DRUPAL_DATABASE_USERNAME: drupal
DRUPAL_DATABASE_PASSWORD: drupal
# Generate a salt with: `php -r "echo bin2hex(random_bytes(25));"`
DRUPAL_HASH_SALT: db0de8a1556aa5348f87cfc950cd2c9641713d46e9412c8b05
ports:
#assign random port on the host
- "80"
depends_on:
- mysql
restart: always
working_dir: /var/www/html
volumes:
- /Sites/drupal-container2:/var/www/html:rw,delegated
mysql:
image: mysql:5.7
container_name: drupal-mysql
# new
volumes:
- /Sites/drupal-container:/var/lib/mysql
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
MYSQL_DATABASE: drupal
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
ports:
- "3306:3306"
networks:
# Define the default network
# Services will use this automatically without having a networks key
default:
external:
name: macvlan_net
FYI the network named macvlan_net is defined manually outside of the YAML file. (this allows me to access the Drupal container using IP address)
Problem:
when I run docker-compose up, I get 2 Drupal containers:
Drupal 1 - 132.177.14.1
Drupal 2 - 132.177.14.2
but once i run through the initial setup on Drupal 1, Drupal 2 seems to be mirroring the setup that I made also.
Expected result:
I should be greeted by the initial setup when visiting Drupal 2.
Since you're using a single MySql container to house the databases for both Drupal instances(containers) you'll need to ensure that you're using unique database names during the Drupal install process. So during setup you could try:
Drupal 1 -> Database Name: drupal_1_db
Drupal 2 -> Database Name: drupal_2_db
Your other options are:
Use table prefixes defined under advanced options instead of unique database names, but this isn't necessary unless you have a reason to keep both websites in a single database schema
Use separate mysql containers for each Drupal instance

Docker compose - save configuration

Here's my docker-compose.yml file, adapted from here:
version: '3.1'
services:
mysql:
image: mariadb
environment:
MYSQL_DATABASE: drupal8
MYSQL_USER: drupal8
MYSQL_PASSWORD: drupal8
MYSQL_ROOT_PASSWORD: admin
volumes:
- /var/lib/mysql
restart: always
drupal:
image: drupal:8.2-apache
ports:
- 8080:80
volumes:
- /var/www/html/modules
- /var/www/html/profiles
- /var/www/html/themes
# this takes advantage of the feature in Docker that a new anonymous
# volume (which is what we're creating here) will be initialized with the
# existing content of the image at the same location
- /var/www/html/sites
restart: always
links:
- mysql
Now on running this and opening up localhost:8080 in my browser, I'm presented with Drupal's configuration setup, which I duly follow and presto, my first Drupal page is created. What I ultimately need to do is:
Save the configuration somehow, so that the settings persist
Be able to push these two containers to a single repository in Docker Hub
The end goal is to be able to issue docker run myDockerHubUsername/myRepo, which would pull these two containers and Drupal would be preconfigured.
Your docker-compose is already saving all the data/configurations you made. Even you destroy the containers, the data persists.
You need to keep your mounted volumes!
If you want to run these somewhere else. You need to always carry your data/volume. Remember to check or change the paths.
For 2nd, it is not advisable to keep multiple images in one image. If you still want. You need to prepare a Dockerfile, and prepare a single image out of that.

Why does docker-compose not lookup the IP address from the MySQL container?

My own_app requires a MySQL database. The problem is that the own_app container needs to ip of the MySQL database. My aim is to solve this issue by using docker-compose.yml.
Dockerfile
ENTRYPOINT ["docker-entrypoint.sh"]
docker-compose.yml
Based on the Wordpress example the docker-compose.yml has been slightly modified as follows:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
own_app:
depends_on:
- db
image: own_app:latest
ports:
- "8000:80"
restart: always
environment:
MYSQL_DB_HOST: db:3306
volumes:
db_data:
but it inserts db:3306
db.default.url="jdbc:mysql://db:3306/database_name"
instead of an IP address
Problem
The sed statement that resides in the docker-entrypoint.sh works well, i.e. it replaces localhost with the $MYSQL_DB_HOST environment variable. The problem is that docker-compose returns db:3306 instead of <ip>:3306
Question
Why is docker-compose not looking up the IP address?
Environment variables in docker-compose are just strings, it doesn't know if it's a hostname or any other text. The only parsing that's done is to expand variables in the ${varname} syntax.
For connecting containers together, you don't want IP addresses anyway, they can change. Use the DNS based discovery which will resolve "db" from the service name in any other containers that are on the same network.

Resources