Multiple database in docker compose - docker

i am trying to create 2 database in a docker compose yml file, one is for the app and the other is for the test part, in the java spring framework i do use the the url like "jdbc:postgresql://localhost:5401/webTest", but id does not work.
From the cmd, i can connect to the database-user with no problem and the table are there but i can not connect to the database-test, is there a specific issue i am blind about?
#service
services:
database-user:
#container_name: postgres-user
image: postgres
ports:
- 5401:5432
volumes:
- postgres-user:/var/lib/postgresql/data
- ./scripts/create-table-db.sql:/docker-entrypoint-initdb.d/create-table-db.sql
environment:
- POSTGRES_USER=webAppUser
- POSTGRES_PASSWORD=user
- POSTGRES_DB=webApp
database-test:
#container_name: postgres-test
image: postgres
ports:
- 5402:5432
volumes:
- postgres-test:/var/lib/postgresql/data
- ./scripts/create-table-db.sql:/docker-entrypoint-initdb.d/create-table-db.sql
environment:
- POSTGRES_USER=webAppTest
- POSTGRES_PASSWORD=test
- POSTGRES_DB=webTest
volumes:
postgres-user:
postgres-test:
i did try to follow some example like here but its not clear.
(the database-user does work also in the java part)

Related

Docker-Compose not creating schema from local db while building docker image

I have created a docker-compose file with 2 services web and POSTGRES. When I run the below docker-compose file, it does not create tables that is in my local POSTGRES db. I have provided the environment variables to use. I think it should use these variables and grab the schema and creates the schema in docker right? When I call the rest API, I am getting relation "x" does not exist.
version: "3"
services:
web:
build: .
depends_on:
- postgres
ports:
- "8000:8000"
volumes:
- postgres-db:/var/lib/postgresql/data
environment:
- DATABASE_HOSTNAME=postgres
- DATABASE_PORT=5432
- DATABASE_PASSWORD=password!
- DATABASE_NAME=dump
- DATABASE_USERNAME=postgres
- SECRET_KEY=021158d8d8d8d8d8d8d8d87
- ALGORITHM=HS256
- ACCESS_TOKEN_EXPIRE_MINUTES=50
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=password!
- POSTGRES_DB=dump
volumes:
postgres-db: {}

understanding Docker compose mongodb and rails application

there is ruby on rails application which uses mongodb and postgresql databases. When I run it locally everything works fine, however when I try to open in a remote container, it throws error message
2021-03-14T20:22:27.985+0000 Failed: error connecting to db server: no reachable servers
the docker-compose.yml file defines following services:
redis mongodb db rails
I start remote containers with following command:
docker-compose build - build successful
docker-compose up -d - containers are up and running
when I connect to the rails container and try to do
bundle exec rake aws:restore_db
error mentioned above is thrown. I don't know what is wrong here. The mongodb container is up and running.
the docker-compose.yml is mentioned below:
version: '3.4'
services:
redis:
image: redis:5.0.5
mongodb:
image: mongo:3.6.13
volumes:
- mongo-data:/data/db
db:
image: postgres:11.3
volumes:
- db-data:/var/lib/postgresql/data
rails:
build: .
image: proj:latest
depends_on:
- db
- mongodb
- redis
volumes:
- .:/proj
ports:
- "3000:3000"
tty: true
stdin_open: true
env_file:
- .env/development.env
volumes:
db-data:
mongo-data:
this is how I start all four remote containers:
$ docker-compose up -d
Starting proj_db_1 ... done
Starting proj_redis_1 ... done
Starting proj_mongodb_1 ... done
Starting proj_rails_1 ... done
please help me to understand how remote containers should interact with each other.
Your configuration should point to the services by name and not to a port on localhost. For example, if you ware connecting to redis as localhost:6380 or 127.0.0.1:6380, now you need to use redis:6380
If this is still not helping, you can try to add links between containers in order the names given to them as services to be resolved. So the file will look something like this:
version: '3.4'
services:
redis:
image: redis:5.0.5
networks:
- front-end
links:
- "mongodb:mongodb"
- "db:db"
- "rails:rails"
mongodb:
image: mongo:3.6.13
volumes:
- mongo-data:/data/db
networks:
- front-end
links:
- "redis:redis"
- "db:db"
- "rails:rails"
db:
image: postgres:11.3
volumes:
- db-data:/var/lib/postgresql/data
networks:
- front-end
links:
- "redis:redis"
- "mongodb:mongodb"
- "rails:rails"
rails:
build: .
image: proj:latest
depends_on:
- db
- mongodb
- redis
volumes:
- .:/proj
ports:
- "3000:3000"
tty: true
stdin_open: true
env_file:
- .env/development.env
networks:
- front-end
links:
- "redis:redis"
- "mongodb:mongodb"
- "db:db"
volumes:
db-data:
mongo-data:
networks:
front-end:
The links will allow for a hostnames to be defined in the containers.
The link flag is legacy, and in new versions of docker-engine it's not required for user defined networks. Also, the links will be ignored in case of docker swarm deployment. However since there are sill old installations of Docker and docker-compose, this is one thing to try in troubleshooting.

Publish multiple images on hub.docker.com in a single repository

I am new to Docker so and this is giving me a headache. I finish developing a site in Magento linking multiple images using docker-compose.yml.
Here is my docker-compose.yml
version: '3'
services:
web:
image: webdevops/php-apache-dev:7.1
container_name: web
restart: always
user: application
environment:
- WEB_ALIAS_DOMAIN=local.domain.com
- WEB_DOCUMENT_ROOT=/app/pub
- PHP_DATE_TIMEZONE=EST
- PHP_DISPLAY_ERRORS=1
- PHP_MEMORY_LIMIT=2048M
- PHP_MAX_EXECUTION_TIME=300
- PHP_POST_MAX_SIZE=500M
- PHP_UPLOAD_MAX_FILESIZE=1024M
volumes:
- "./:/app:cached"
ports:
- "80:80"
- "443:443"
- "32823:22"
links:
- mysql
mysql:
image: mariadb:10
container_name: mysql
restart: always
ports:
- "52000:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=magento
volumes:
- db-data:/var/lib/mysql
phpmyadmin:
container_name: phpmyadmin
restart: always
image: phpmyadmin/phpmyadmin:latest
environment:
- MYSQL_ROOT_PASSWORD=root
- PMA_USER=root
- PMA_PASSWORD=root
ports:
- "8080:80"
links:
- mysql:db
depends_on:
- mysql
volumes:
db-data:
external: false
Then docker-compose up -d --build. I have 3 images and 3 containers running on my local machine.
I want to publish these image on hub.docker.com so anyone can download the image and get all the containers running.
Also is there a way to add a MySQL DB to the image, so anyone can have the same running website like I had on my local?
Remember that the only thing you can publish on Docker Hub is Docker images; you can't publish containers, volumes, Docker Compose YAML files, or other artifacts. Since the YAML file is a fairly straightforward text file it's very common to publish that on GitHub, along with a README file explaining how to use it.
You don't need to push the phpmyadmin/phpmyadmin or mariadb images because those are standard Docker Hub images, so you only need to push your custom image. I would highly recommend removing the volumes: that mounts your local development tree over the image contents to validate that the image actually has what you expect.
Is there a way to add mysql DB to the image
No. The various standard Docker database images are built in a way that it is extremely difficult to build an image containing prepopulated data. Wordpress image with mysql data has some good discussion on the topic, and MySQL Docker container is not saving data to new image has some good analysis in the question proper.

Deploy existing Prestashop to server using Docker

I've created PrestaShop store on server. Is there any possible way to use docker for my store and migrate it into another server using docker? I know that I'll need docker-compose but to be honest I don't know what to do with files on current server.
Ok, so I deeped into problem and solution for ma quesstion is as below. What I did is pull original image from prestashop and copy there my files.
Next step was use mariadb image. I had backup.sql file exported from previous store phpmyadmin
version: '2'
services:
prestashop:
image: prestashop
ports:
- 80:80
links:
- mariadb:mariadb
depends_on:
- mariadb
volumes:
- ./src:/var/www/html
- ./src/modules:/var/www/html/modules
- ./src/themes:/var/www/html/themes
- ./src/override:/var/www/html/override
environment:
- PS_DEV_MODE=1
- DB_SERVER=mariadb
- DB_USER=root
- DB_PASSWD=root
- DB_NAME=prestashop
- PS_INSTALL_AUTO=0
mariadb:
image: mariadb
volumes:
- backup.sql:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=prestashop
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- mariadb
ports:
- 81:80
environment:
- PMA_HOST=mariadb
- PMA_USER=root
- PMA_PASSWORD=root
The biggest issue is IP in docker-machine. Keep in mind that if you are using docker toolbox you have IP 192.168.99.100 but in Docker for Windows your IP depends on localhost (or just type localhost).
You can use this docker-compose.yml :
version: "3"
services:
prestashop:
image: prestashop/prestashop
networks:
mycustomnetwork:
ports:
- 82:80
links:
- mariadb:mariadb
depends_on:
- mariadb
volumes:
- ./src:/var/www/html
- ./src/modules:/var/www/html/modules
- ./src/themes:/var/www/html/themes
- ./src/override:/var/www/html/override
environment:
- PS_DEV_MODE=1
- DB_SERVER=mariadb
- DB_USER=root
- DB_PASSWD=mycustompassword
- DB_NAME=prestashop
- PS_INSTALL_AUTO=0
mariadb:
image: mariadb
networks:
mycustomnetwork:
volumes:
- presta_db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=mycustompassword
- MYSQL_DATABASE=prestashop
phpmyadmin:
image: phpmyadmin/phpmyadmin
networks:
mycustomnetwork:
links:
- mariadb:mariadb
ports:
- 1235:80
depends_on:
- mariadb
environment:
- PMA_HOST=mariadb
- PMA_USER=root
- PMA_PASSWORD=mycustompassword
volumes:
presta_db:
networks:
mycustomnetwork:
external: true
Replace mycustomnetwork and mycustompassword
Then run docker-compose up
Web url : localhost:82
PHP MyAdmin url : localhost:1235
You can follow this tutorial to setup Prestashop in a Docker environment.
https://hub.docker.com/r/prestashop/prestashop/
You will need to add your current files to the Prestashop container and most likely import your database in a MySQL container. Docker-compose will be used to launch those containers together. Once this is done, you will be able to deploy the whole thing anywhere.
You should also include bridge network in your compose file, some examples might work from here https://runnable.com/docker/docker-compose-networking.
This way db can be configured to be accessed only by prestashop on local docker network without being exposed outside. Presta db can also be pointed to the name of the running image, in case your IP changes or something. All what you would leave running is port 80 on the app.

Why is drone.io keeping information of previous runs?

I want to delete all previous information of drone and make a completely new installation. So what I'm doing is this.
With this dockerfile:
version: '2'
services:
drone-server:
image: drone/drone:0.8
ports:
- 80:8000
- 9000
volumes:
- /var/lib/drone:/var/lib/drone/
restart: always
environment:
- DRONE_OPEN=true
- DRONE_HOST=http://drone-server:8000
- DRONE_GITEA=true
- DRONE_GITEA_URL=http://web:3000
- DRONE_SECRET=${DRONE_SECRET}
drone-agent:
image: drone/agent:0.8
command: agent
restart: always
depends_on:
- drone-server
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- DRONE_SERVER=drone-server:9000
- DRONE_SECRET=${DRONE_SECRET}
web:
image: gitea/gitea:1.3.2
volumes:
- ./data:/data
ports:
- "3000:3000"
- "22:22"
depends_on:
- db
restart: always
db:
image: mariadb:10
restart: always
environment:
- MYSQL_ROOT_PASSWORD=changeme
- MYSQL_DATABASE=gitea
- MYSQL_USER=gitea
- MYSQL_PASSWORD=changeme
volumes:
- ./db/:/var/lib/mysql
I'm made a docker-compose up
Then to delete everything I made a docker-compose down. And make sure to delete every volume and every container manually. But when I do docker-compose up again the old information is still there. Why? Where is drone getting that information? I'm new with drone and docker so probably I'm doing something wrong because this does not make sense. Maybe I'm forgetting to delete something.
Can you help me with that?
Drone stores details about its runs in an SQLite db in /var/lib/drone by default, which you have mounted as a volume, so the stuff it saves in there is kept on your machine when you spin things down and passed back to the new containers when you create them.
If you want to completely reset everything you need to remove the files in your host machines /var/lib/drone folder too.

Resources