NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Middle+88 views

Как подключиться к базе данных, запущенной в Docker, чтобы что-то в ней посмотреть или изменить?

Practice

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.