How do i Connect my Docker container in EC2 to Mysql DB in the same EC2

0

Hello pls i need help in connecting my nodejs app container in EC2 to Mysql DB(on the machine- EC2) in the same EC2 and also would really appreciate if i could be shown too how to also connect to my xampp mysql on my local computer when doing development

I am using docker-compose below is a sample code

version: "3"
services:
  backend:
    build: #Allowed[image/build]
      context: .
      dockerfile: backend/src/express/userapp/codebase/Dockerfile
      # container_name: my-custom-container-name # Specify your custom container name
      # image: my-custom-image:latest # Specify your custom image name and tag
      args:
        SOURCE_DIR: "./backend/src/express/userapp/codebase"
    ports: #[HOST_PORT:CONTAINER_PORT]
      - "4000:4000"
      # - "4000:80" # Map container port 80 (default) to host port 4000
    environment: #[Setting up my Env variable]
      - NODE_ENV=development
    volumes: #[codebase folder is where my package.json resides]
      - ./backend/src/express/userapp/codebase:/app # [Host volume (-Host:Container) for storing the Source for Live Reloading]
      - /app/node_modules #[Anonymous Volume(-Container) for my node_modules || due to bcrypt]
    # network_mode: "host" # Allow you to attach the container to a specific Docker network or host network.
    extra_hosts: #[hostname:ip_address] #using CMD[--add-host host.docker.internal:host-gateway]
      - "host.docker.internal:host-gateway"
    networks:
      - backendNet

networks:
  backendNet: {}
Nafiu
asked 13 days ago152 views
1 Answer
2
Accepted Answer
profile picture
EXPERT
answered 13 days ago
profile picture
EXPERT
reviewed 13 days ago
profile picture
EXPERT
reviewed 13 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions