Как подключиться к базе данных, запущенной в Docker, чтобы что-то в ней посмотреть или изменить?
Asked at
AI answer
The method depends on where the database client is running and whether the container's port has been published.
If the port is published, connect to the host address and published port. For example, a PostgreSQL container is often run with -p 5432:5432, after which the client connects to localhost:5432 with the required database name, username, and password. You can check the settings with docker ps and docker inspect.
To use the command line inside the container, use docker exec, for example:
docker exec -it <container> psql -U <user> -d <database>
For MySQL, you can use mysql instead of psql. If the client is in another container, it usually connects using the service name on the shared Docker network, such as db:5432, rather than localhost.
Do not expose the database port externally unless necessary: restrict access, use strong credentials, and do not make changes to a production database without a backup and verification.
Also asked at Касперский
Similar questions
- The application in a Docker container cannot connect to a database in another container. What could be the reason?
- Tell me how to package and run a Python service in a Docker container.
- Do you use Docker or Docker Compose?
- What needs to be done with the product to switch to microservice architecture? Is it enough to move it to Docker containers?
- What is the name of the file or structure that defines the parameters and settings of a container in Docker?