国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
Running Databases in Docker Containers
Using Volumes for Data Persistence
Networking Between Apps and Databases
Managing Multiple Services with Docker Compose
首頁 運維 Docker Docker如何使用數(shù)據(jù)庫(MySQL,PostgreSQL,MongoDB)?

Docker如何使用數(shù)據(jù)庫(MySQL,PostgreSQL,MongoDB)?

Jun 22, 2025 am 12:05 AM
docker 資料庫

Docker 支持MySQL、PostgreSQL 和MongoDB 等數(shù)據(jù)庫的良好運行,但需正確配置數(shù)據(jù)持久化、網(wǎng)絡和環(huán)境變量。首先使用官方鏡像啟動容器並設置環(huán)境變量;其次通過命名卷或綁定掛載實現(xiàn)數(shù)據(jù)持久化,推薦使用命名卷;再次將應用與數(shù)據(jù)庫置於同一Docker 網(wǎng)絡以實現(xiàn)通信;最後建議使用Docker Compose 管理多服務項目以簡化流程並提升可維護性。

Docker works pretty well with databases like MySQL, PostgreSQL, and MongoDB — as long as you know how to set things up properly. It's not just about running the container; it's more about handling data persistence, networking, and configuration in a way that keeps your database reliable and accessible.

Running Databases in Docker Containers

You can pull official images for MySQL, PostgreSQL, and MongoDB from Docker Hub and run them using docker run . Each of these databases has its own set of environment variables for initial setup.

For example:

  • MySQL uses MYSQL_ROOT_PASSWORD , MYSQL_DATABASE , and MYSQL_USER
  • PostgreSQL uses POSTGRES_PASSWORD , POSTGRES_DB , and POSTGRES_USER
  • MongoDB doesn't require much by default but supports authentication when needed

Here's a basic command for starting a MySQL container:

 docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest

This starts a new container with MySQL, sets the root password, and runs it in detached mode.

One thing to note is that if you don't configure storage correctly, any data stored inside the container will be lost when the container stops or is removed.

Using Volumes for Data Persistence

Containers are ephemeral by nature, which means data inside them isn't safe unless you persist it. To keep your database data safe across container restarts or replacements, you should use volumes .

There are two main ways to do this:

  • Bind mounts: Mount a directory from your host machine into the container
  • Named volumes: Let Docker manage where the data goes on the host

Using named volumes is generally cleaner and more portable. Here's how you might start a PostgreSQL container with a named volume:

 docker volume create pgdata
docker run --name pg-container -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=pass123 -d postgres

This makes sure all PostgreSQL data is stored in the pgdata volume and won't disappear when the container goes away.

A common mistake is to forget setting the right permissions on the host folder when using bind mounts — especially on Linux systems. If the database process inside the container can't write to the mounted directory, it won't start.

Networking Between Apps and Databases

If your application is also running in Docker and needs to access the database, they need to be on the same Docker network.

You can either:

  • Use the default bridge network (not ideal for multiple containers)
  • Create a custom network with docker network create and attach both containers to it

Once connected, your app can reach the database using the container name as the hostname. For example, if your MySQL container is named mysql-container , your app can connect to it at mysql-container:3306 .

If you're using Docker Compose, defining services under the same docker-compose.yml file automatically puts them on the same network. That's one reason why Compose is handy when working with apps and databases together.

Managing Multiple Services with Docker Compose

For real-world setups, especially when you have an app, a database, maybe a cache, and others — managing everything through individual docker run commands gets messy fast. That's where Docker Compose shines.

A simple docker-compose.yml for a Node.js app and a PostgreSQL database looks like this:

 version: '3'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://postgres:pass123@db:5432/mydb?schema=public
  db:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=pass123
      - POSTGRES_DB=mydb
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"

volumes:
  pgdata:

With this setup, you just run docker-compose up and everything comes up connected and configured. Plus, you can easily scale or update components later.

One small gotcha: If you change the database schema or seed data during development, make sure your volume isn't holding onto old data that might conflict — sometimes you need to delete and recreate the volume.

So yeah, Docker works great with databases, but you need to handle persistence, networking, and config carefully. Once you've got that down, it becomes a solid part of your dev or deployment workflow.

基本上就這些。

以上是Docker如何使用數(shù)據(jù)庫(MySQL,PostgreSQL,MongoDB)?的詳細內容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

本網(wǎng)站聲明
本文內容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1600
29
PHP教程
1502
276
如何在Windows/Linux上安裝MySQL 8.0? 如何在Windows/Linux上安裝MySQL 8.0? Jun 11, 2025 pm 03:25 PM

安裝MySQL8.0的關鍵在於按步驟操作並註意常見問題。 Windows上推薦使用MSI安裝包,步驟包括下載安裝包、運行安裝程序、選擇安裝類型、設置root密碼、啟用服務啟動,並註意端口衝突或手動配置ZIP版;Linux(如Ubuntu)則通過apt安裝,步驟為更新源、安裝服務器、運行安全腳本、檢查服務狀態(tài)及修改root認證方式;無論哪個平臺,都應修改默認密碼、創(chuàng)建普通用戶、設置防火牆、調整配置文件以優(yōu)化字符集等參數(shù),確保安全性與正常使用。

如何使用雄辯在數(shù)據(jù)庫中創(chuàng)建新記錄? 如何使用雄辯在數(shù)據(jù)庫中創(chuàng)建新記錄? Jun 14, 2025 am 12:34 AM

要使用Eloquent在數(shù)據(jù)庫中創(chuàng)建新記錄,有四種主要方法:1.使用create方法,傳入屬性數(shù)組快速創(chuàng)建記錄,如User::create(['name'=>'JohnDoe','email'=>'john@example.com']);2.使用save方法手動實例化模型並逐個賦值保存,適用於需要條件賦值或額外邏輯的場景;3.使用firstOrCreate根據(jù)搜索條件查找或創(chuàng)建記錄,避免重複數(shù)據(jù);4.使用updateOrCreate查找記錄並更新,若無則創(chuàng)建,適合處理導入數(shù)據(jù)等可能重

什麼是Docker BuildKit,它如何改善構建性能? 什麼是Docker BuildKit,它如何改善構建性能? Jun 19, 2025 am 12:20 AM

DockerBuildKit是一個現(xiàn)代化的鏡像構建后端,它通過1)并行處理獨立構建步驟、2)更高級的緩存機制(如遠程緩存復用)、3)結構化輸出提升構建效率與可維護性,顯著優(yōu)化了Docker鏡像構建的速度與靈活性,用戶只需啟用DOCKER_BUILDKIT環(huán)境變量或使用buildx命令即可激活該功能。

Docker如何與Docker Desktop一起使用? Docker如何與Docker Desktop一起使用? Jun 15, 2025 pm 12:54 PM

dockerworkswithdockerdesktopbyprovidingauser-frencylyintlyterfaceandonvironmenttomanagecontainers,圖像,AndResourcesonlocalmachines.1.dockerdesktopbundlesdockerdockerdockerengine,cli,cli,cli,cli,copsose,copsose,copsose,andotherToolSintoonePackage.2.itustersoruses.2.itiperslialdialdialdimize(例如

如何監(jiān)視Docker容器的資源使用情況? 如何監(jiān)視Docker容器的資源使用情況? Jun 13, 2025 am 12:10 AM

要監(jiān)控Docker容器資源使用情況,可採用內置命令、第三方工具或系統(tǒng)級工具。 1.使用dockerstats實時監(jiān)控:運行dockerstats可查看CPU、內存、網(wǎng)絡和磁盤IO等指標,支持過濾特定容器並結合watch命令定期記錄。 2.通過cAdvisor獲取容器洞察:部署cAdvisor容器以獲取詳細的性能數(shù)據(jù),並通過WebUI查看歷史趨勢與可視化信息。 3.結合系統(tǒng)級工具進行深入分析:利用top/htop、iostat、iftop等Linux工具監(jiān)控系統(tǒng)層面的資源消耗,並可集成Prometheu

什麼是Kubernetes,與Docker有何關係? 什麼是Kubernetes,與Docker有何關係? Jun 21, 2025 am 12:01 AM

Kubernetes不是Docker的替代品,而是管理大規(guī)模容器的下一步。 Docker用於構建和運行容器,而Kubernetes則用於跨多臺機器編排這些容器。具體來說:1.Docker打包應用,Kubernetes管理其運行;2.Kubernetes自動化部署、擴展和管理容器化應用;3.它通過節(jié)點、Pod和控制平面等組件實現(xiàn)容器編排;4.Kubernetes與Docker協(xié)同工作,自動重啟失敗容器、按需擴展、負載均衡及無停機更新;5.適用於需要快速擴展、運行微服務、高可用及多環(huán)境部署的應用場景。

如何解決Docker問題 如何解決Docker問題 Jul 07, 2025 am 12:29 AM

遇到Docker問題應先定位出問題的環(huán)節(jié),是鏡像構建、容器運行或網(wǎng)絡配置等問題,再按步驟排查。 1.查看容器日誌(dockerlogs或docker-composelogs)以獲取錯誤信息;2.檢查容器狀態(tài)(dockerps)和資源使用情況(dockerstats),判斷是否因內存不足或端口問題導致異常;3.進入容器內部(dockerexec)驗證路徑、權限和依賴;4.回顧Dockerfile和compose文件是否存在配置錯誤,如環(huán)境變量拼寫或卷掛載路徑問題,並建議cleanbuild避免緩存幹

選擇...更新的目的是什麼? 選擇...更新的目的是什麼? Jun 11, 2025 pm 03:37 PM

themainpurposeofselect ... forupdateIstolockSelectedRowsdurwsationTopreventothersessionsSersessionsFromedIfifyingThemuntiltherthtransactionCompletesWhichenSistersIsistensistencyInconCurrentenCurrentenCurrentenVironmentsSuchasBankingSuchingandInventorySunventOndoryStemssssssss1itplaceSrow-Levellockslocksolocksallowsallow

See all articles