How to get docker compose to create pg DB if not found - docker-compose

I am using NestJS with TypeOrm and PostGres to define the Database.
I would like to have docker-compose create the DB if it does not exist in the container.
docker-compose.yaml
version: '3.9'
services:
backend:
build: .
ports:
- 8000:3000
volumes:
- .:/app
depends_on:
- db
db:
image: postgres:14.1
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- 33066:5432
pgadmin:
container_name: pgadmin4_container_shibari
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- '5050:80'
redis:
image: redis
ports:
- 6379:6379

Related

How to create two postgres docker containers from docker compose file , one for development and one for testing?

So for example this is my docker-compose.yml file:
version: "3.7"
services:
postgres:
container_name: mydevdb
image: postgres:13
restart: always
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- postgres:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
postgres:
And I want to have an image for development and another for testing with different (container name, POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB and port)
I want the best approach for that. Thanks.
You just have to give the services different names. (and maybe different port exposures, depending on how you want to use them)
version: "3.7"
services:
postgres-dev:
container_name: mydevdb
image: postgres:13
restart: always
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- postgres-dev:/var/lib/postgresql/data
ports:
- "5432:5432"
postgres-test:
container_name: mytestdb
image: postgres:13
restart: always
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- postgres-test:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
postgres-dev:
postgres-test:

ERROR: Named volume "postgres:/data/postgres:rw" is used in service "postgres" but no declaration was found in the volumes section

i am trying to build micro service project.
and i am getting this error when i am trying to run my docker_compose file:
ERROR: Named volume "postgres:/data/postgres:rw" is used in service "postgres" but no declaration was found in the volumes section.
this is my docker_compose file :
services:
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: amigoscode
POSTGRES_PASSWORD: password
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/var/lib/pgadmin
ports:
- "5050:80"
networks:
- postgres
restart: unless-stopped
any hint will be appreciated.
You are missing the relative path, this would work. Either use a relative path or a fixed one!
services:
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: amigoscode
POSTGRES_PASSWORD: password
PGDATA: /data/postgres
volumes:
- ./postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- ./pgadmin:/var/lib/pgadmin
ports:
- "5050:80"
networks:
- postgres
restart: unless-stopped

docker-compose file for nodejs, mongo, redis, rabbitmq

I need a docker compose that have node 12, mongo 4.4, redis 4.0.6 and rabbitmq 3.8.9 . This is what I have in my docker-compose right now and apparently it does not work. The app can't seems to connect to redis and rabbitmq.
version: '3'
services:
app:
container_name: malllog-main
restart: always
build: .
ports:
- '3000:3000'
external_links:
- mongo
- redis
- rabbitmq
mongo:
container_name: malllog-mongo
image: mongo:4.4
ports:
- '27017:27017'
redis:
container_name: malllog-redis
image: redis:4.0.6
ports:
- '6379:6379'
rabbitmq:
container_name: malllog-rabbitmq
image: rabbitmq:3.8.9
ports:
- '15672:15672'
- '5672:5672'
Below is the docker-compose file that I have used for my microservice test project, which works for me as you can try this out.
version: '3'
services:
my-ui:
container_name: my-ui
build: ./my-ui
ports:
- "80:80"
depends_on:
- my-api
networks:
- test-network
my-api:
container_name: my-api
restart: always
build:
context: my-api
dockerfile: Dockerfile
ports:
- "8080:8080"
#command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker
depends_on:
- rabbitmq
- mp-redis
networks:
- test-network
rabbitmq:
container_name: rabbitmq
image: rabbitmq:management
ports:
- "5672:5672"
- "15672:15672"
restart: always
networks:
- test-network
mp-redis:
container_name: mp-redis
image: redis:5
ports:
- "6379:6379"
restart: always
networks:
- test-network
mp-mongodb:
container_name: mp-mongodb
image: mongo:3.6
restart: always
environment:
MONGO_DATA_DIR: /data/db
MONGO_LOG_DIR: /dev/null
volumes:
- mongo-data:/data/db
ports:
- "27017:27017"
command: mongod --smallfiles --logpath=/dev/null # --quiet
networks:
- test-network
volumes:
mongo-data:
networks:
test-network:

Error while running compose up. YAML linter showing no errors

I keep getting a service 'image' error when running docker-compose up for my yml file.
I researched online and it seems like it was mostly some formatting error.
I have run my yml through a YAML linter and there are no errors.
version: '3.5'
services:
server:
image: postgrest/postgrest
ports:
- "3000:3000"
links:
- db:db
environment:
PGRST_DB_URI: postgres://app_user:password#db:5432/app_db
PGRST_DB_SCHEMA: public
PGRST_DB_ANON_ROLE: app_user
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
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
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
swagger:
image: swaggerapi/swagger-ui
ports:
- "8080:8080"
expose:
- "8080"
environment:
API_URL: http://localhost:3000/
Expected would be the images get downloaded and containers get started up
Error is:
ERROR: In file '.\docker-compose.yml', service 'image' must be a mapping not a string.
This issue happens because of indentation.
docker-compose deals with image like a service because of wrong indentation.
I modified your file and start configured containers successfully:
version: "3.5"
services:
server:
image: postgrest/postgrest
ports:
- "3000:3000"
links:
- db:db
environment:
PGRST_DB_URI: postgres://app_user:password#db:5432/app_db
PGRST_DB_SCHEMA: public
PGRST_DB_ANON_ROLE: app_user
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
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
swagger:
image: swaggerapi/swagger-ui
ports:
- "8080:8080"
expose:
- "8080"
environment:
API_URL: http://localhost:3000/
networks:
postgres:
driver: bridge
volumes:
pgadmin:
postgres:

How to get mongo-express to wait for mongo in docker-compose

I'm using the healthcheck/mongo and have mongo-express waiting on service_healthy there. Why does it not wait and the result at localhost:8081 is 404 in the browser? :(
#Docker Engine: 18.06.1-ce
#docker-compose: 1.22.0, build f46880f
version: '2.1'
services:
mongodb:
image: healthcheck/mongo:latest
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=rootpass
ports:
- "27017:27017"
volumes:
- "./mongo/data:/data/db"
mongo-express:
image: mongo-express
restart: always
depends_on:
mongodb:
condition: service_healthy
ports:
- "8081:8081"
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass