skip to Main Content

How can I access a db in a docker image that I’ve run docker-compose up on?

In the docker compose file I have:

  metadata-db:
    image: fastdb #${ECR}/bmll/sampledb:latest
    build:
      context: ./docker
      dockerfile: fastdb.dockerfile
    networks:
      - dd-internal
    ports:
      - 5577:5432

And I have successfully launched the docker image with docker-compose up.

However, now I would like to access the above metadata-db, but I’m not sure how I can do this- any suggestions?

2

Answers


  1. The port field in the yaml translates as if you were running a container with the -p option so in your case that’d be -p 5577:5432 meaning your mapping the port 5577 on your host to the container port 5432.

    tl;dr http://localhost:5577

    Login or Signup to reply.
  2. Try following, may be it works:

    Identify Container Name: In your docker-compose.yml file, you have a service named metadata-db. Use this name to access the container.

    Run the following command to connect to the PostgreSQL database inside the container:

    docker exec -it metadata-db psql -U <username> -d <database_name>
    

    After connecting, you’ll be in the PostgreSQL prompt. You can now run SQL queries and interact with the database.

    **Note:**For accessing the database from your local machine (host), you can use the exposed port (5577) in your database client or application, just like you would for connecting to a remote database server.

    #Apache-Age #docker

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search