docker-compose mysql Connection refused - docker

my development environment
Spring Boot 2.x, Gradle, Mybatis
Simply build through Docker-compose and create a project for API testing.
Why do I keep getting connection refused?
If you type the command 'docker-compose up', it runs without any problem.
I checked the network and it is connected.
I usually use 'docker exec A ping B' to check.
I don't know why an error occurs when A is a DB container.
I'll attach the code just in case.
# application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://test-db:3306/exdocker?serverTimezone=Asia/Seoul
spring.datasource.username=dockerex
spring.datasource.password=12341234
# docker-compose.yml
version: "3"
services:
test-db:
container_name: test-db
image: mysql:8.0
environment:
MYSQL_DATABASE: "exdocker"
MYSQL_ROOT_PASSWORD: "root"
MYSQL_PASSWORD: "12341234"
MYSQL_USER: "dockerex"
restart: always
networks:
- exdocker-network
ports:
- 3307:3306
test-app:
links:
- test-db
build:
context: .
dockerfile: Dockerfile
ports:
- 8081:8080
restart: always
networks:
- exdocker-network
depends_on:
- test-db
networks:
exdocker-network:
ipam:
driver: default
config:
- subnet: 172.202.0.1/16

Related

Docker compose Unable to connect

I've created docker-compose.yml file with the following content. My docker service runs ok, docker run hello-world works just fine and after running command
docker-compose build && docker-compose up -d
I see no errors but in the browser I'm getting an error Unable to connect when I go to localhost:8000
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
depends_on:
- php
- mysql
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
mysql:
image: mysql:8.0
container_name: mysql
restart: unless-stopped
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: root
SERVICE_NAME: mysql
When I run command
docker-compose exec php ls
I can see the list of files and folders

Golang: `dial tcp 172.20.0.7:8081: connect: connection refused`, error at random times between docker containers / services

I've a docker-compose file with contents like this
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)
version: '3'
services:
john:
build:
context: .
dockerfile: Dockerfiles/cowboys/John/Dockerfile
args:
- COWBOY_NAME_JOHN
- CONTAINER_PORT_JOHN
ports:
- "8081:8081" # Forward the exposed port on the container to port on the host machine
restart: unless-stopped
networks:
- fullstack
depends_on:
db:
condition: service_healthy
links:
- db
philip:
build:
context: .
dockerfile: Dockerfiles/cowboys/Philip/Dockerfile
args:
- COWBOY_NAME_PHILIP
- CONTAINER_PORT_PHILIP
ports:
- "8085:8085"
restart: unless-stopped
networks:
- fullstack
depends_on:
db:
condition: service_healthy
links:
- db
db:
build:
context: Dockerfiles/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
restart: on-failure
networks:
- fullstack
ports:
- "3306:3306"
healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD
networks:
fullstack:
driver: bridge
When I run docker-compose up --build ( or docker-compose up --build --force-recreate to reinitialise db on next run), I get an error like this
sam_1 | panic: Put "http://john:8081/shoot": dial tcp 172.20.0.7:8081: connect: connection refused
john is one of the services name under services. Here is complete screenshot:
Link to repository where all details could be seen including environment variables & relevant Dockerfile(s) - https://github.com/sitetester/distributed-cowboys
Could anyone please explain how to get rid of this error ? It just happens/occurs at random time. Not sure how to fix it. Thanks in advance!
Solved: removing existing containers/images and pruning docker volumes fixed the issue.

Why am I getting a service dependency error when I haven't touched my .yml at all?

Whenever I run docker-compose up -d --build to start working on my project, it started up my environment just fine up until yesterday.
Upon running docker-compose up -d --build, I get this annoying error that says: ERROR: Service 'app' depends on service 'db' which is undefined.
I'm not sure how this is happening out of no where as I've made absolutely no changes whatsoever to the docker-compose.yml file. I've tried troubleshooting this extensively but to no avail.
What's wrong with my file?
Here's my docker-compose.yml file:
version: "3.7"
services:
app:
build:
args:
user: sammy
uid: 1000
context: ./
dockerfile: Dockerfile.dev
working_dir: /var/www/
environment:
- COMPOSER_MEMORY_LIMIT=-1
depends_on:
- db
volumes:
- ./:/var/www
networks:
- lahmi
myApp:
image: mysql:5.7
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
- ./docker-compose/mysql/my.cnf:/etc/mysql/my.cnf
- ./docker-compose/mysql/init:/docker-entrypoint-initdb.d
ports:
- 3307:3306
networks:
- lahmi
nginx:
image: nginx:alpine
ports:
- 8005:80
depends_on:
- db
- app
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- lahmi
networks:
lahmi:
driver: bridge
volumes:
dbdata:
driver: local
There is no service named db in docker-compose.yml. Changing db to myApp (database service) may work.
If you are referencing the database as db in service app, you must use links configuration to change myApp to db or change service name myApp to db.
https://docs.docker.com/compose/compose-file/compose-file-v3/#links

Can't access docker container over public ip

So I am creating an application that is running in a docker container on port 3000 of my local machine. I can open the app at localhost:3000, I can also open it on another device but on the same network by using 192.168.68.100:3000. However, I can't access the app by visiting my_public_ip:3000. I added port forwarding on my router for port 3000. I am running the app using docker-compose up and this is my docker-compose file:
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
command: npm run start:dev
env_file:
- .env
networks:
- webnet
depends_on:
- db
db:
container_name: db
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: "..."
MYSQL_USER: "..."
MYSQL_PASSWORD: "..."
MYSQL_ROOT_PASSWORD: "..."
adminer:
container_name: adminer
image: adminer
restart: always
ports:
- 8080:8080
networks:
webnet:
Any suggestions what am I missing?

How to access docker container localhost

I executed docker-compose up and my services worked as well but unfortunately when i hit the url " http://localhost:3000 " it gives me an error I'll paste the docker-compose.yml here.
version: "3.7"
services:
db:
image: postgres
container_name: psql
environment:
POSTGRES_DB: ${DB_DATABASE}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres:/var/lib/postgresql/data
- ./postgresql/conf:/etc/postgresql/
restart: always
ports:
- '5432:5432'
api:
build: .
container_name: api
restart: always
command: yarn start
depends_on:
- db
ports:
- "3000:3000"
volumes:
- .:/usr/app
volumes:
postgres:
When I get into the container and execute curl localhost:3000 it works fine.
My problem was solved after I changed the host in my node api to '0.0.0.0' it was 'localhost' so I think the application was running only inside the container and after that change it was visible from all the created network.

Resources