База в Docker недоступна с хоста: как зайти в неё и внести изменения?
Asked at
AI answer
If the database port is not published to the host, you can connect to it from inside the container using docker exec. For example, for PostgreSQL:
docker exec -it <db-container> psql -U <user> -d <database>
For MySQL:
docker exec -it <db-container> mysql -u <user> -p <database>
With Docker Compose, it is more convenient to use the service name: docker compose exec db psql -U <user> -d <database>. If the container does not have a client installed, you can start a separate container with a client on the same Docker network. Once connected, make changes using regular SQL commands.
To connect from the host, publish the port, for example in Compose: ports: ["5432:5432"], and connect to localhost:5432. Take access settings and credentials into account. Do not edit database files directly inside a Docker volume—use a database client, and make a backup before risky changes.
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?