Docker提供了基於鏡像的運行環境,可以將操作系統、應用程式以及相關依賴打包,為使用者提供完整的使用體驗,因此一經推出大獲好評,迅速成為主流的軟體開發技術之一。 ...
基於Docker的資料庫開發環境
前文介紹了sqlite/mysql/mssql等資料庫系統在ubuntu的安裝與部署過程,相對是比較複雜的,需要耐心等待下載以及排除各種故障,對於開發人員來說是不太友好。在某些情況下,開發人員要測試在多個資料庫環境下軟體的正確性,需要部署多個資料庫,為此可以使用Docker技術。
Docker提供了基於鏡像的運行環境,可以將操作系統、應用程式以及相關依賴打包,為使用者提供完整的使用體驗,因此一經推出大獲好評,迅速成為主流的軟體開發技術之一。Docker有著完善的命令行功能,在Windows環境下有多種GUI管理軟體,本文對Docker不再贅述。下麵先在ubuntu bionic上建立Docker服務,隨後基於Docker composer將多個資料庫打包啟動,為開發者提供多資料庫支持環境。
# 刪除舊版本 sudo apt-get remove docker docker-engine docker.io containerd runc sudo apt-get update sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common # 真正有用的部分 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" # aliyun mirrors curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" sudo apt-get update sudo apt-get install -y docker-ce sudo systemctl enable docker sudo systemctl start docker sudo usermod -aG docker $USER sudo systemctl daemon-reload sudo systemctl restart docker # 不在此機上開發python可以直接安裝這個基於python2.7.17的低版本應用 apt install docker-compose
使用scp或者直接vi編輯Docker-compse.yml文件如下:
version: "3.3" services: clickhouseserver: restart: always image: yandex/clickhouse-server:21.6.5.37-alpine container_name: clickhouseserver hostname: clickhouseserver environment: - TZ=Asia/Shanghai ports: - 8123:8123 - 9000:9000 ulimits: nofile: soft: 262144 hard: 262144 sqlserver: image: mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04 restart: always container_name: mssql environment: - ACCEPT_EULA=Y - SA_PASSWORD=Zm88488848 ports: - 1433:1433 mysql: image: mysql command: --default-authentication-plugin=mysql_native_password restart: always ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: 88488848 pg: image: postgres restart: always ports: - 5432:5432 environment: POSTGRES_PASSWORD: 88488848
隨後運行以下命令啟動。
# 啟動編排,可以按ctrl-c中止 docker-compose up # 類似於服務的永久啟動。 docker-compose up -d
在第一次啟動時,由於本地沒有對應的鏡像,會自動下載,時間稍長些。後續啟動時將非常快速。由於以上資料庫不真正投入生產環境,所以沒有引入掛載點。此外,根據官方文檔要求,sqlserver鏡像在配置時,必須使用強口令(8位大小寫字母與符號的混合)。另,8848的意思是珠峰的高度。