Docker
為什麼我的容器在我送出後不包含數據庫數據?
我正在嘗試創建一個已經包含開發數據的 PostgreSQL 容器。我可以看到我創建的數據,但是當我
commit
、、push
和pull
圖像時,數據不存在。下面是我的 Dockerfile:
FROM postgis/postgis:11-2.5 MAINTAINER Me # Contents of 20-init_db.sql: # CREATE EXTENSION IF NOT EXISTS btree_gist; # CREATE EXTENSION IF NOT EXISTS hstore; COPY ./20-init_db.sql /docker-entrypoint-initdb.d/20-init_db.sql COPY ./make_data.sh /make_data.sh
我使用下面的腳本創建了容器,並且成功填充了數據。
#!/bin/bash aws ecr get-login-password --region us-east-1 | \ docker login --username AWS --password-stdin 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com aws ecr create-repository --repository-name dev_postgres --region us-east-1 # Make sure the build environment is clean docker stop dev_postgres_container docker rm dev_postgres_container docker rmi dev_postgres_img # Prepare the container docker build -f postgres.Dockerfile -t dev_postgres_img . docker run -d --name dev_postgres_container \ -e POSTGRES_DB=the_database \ -e POSTGRES_USER=the_user \ -e POSTGRES_PASSWORD=the_password \ -e PGDATA=/var/lib/postgresql/data/pgdata dev_postgres_img docker exec -it dev_postgres_container bash /make_data.sh ### ### This returns the correct number of users, so there is data in the container at this point. ### docker exec -it dev_postgres_container /usr/bin/psql -U the_user the_database -c "select count(*) from auth_user;" # Commit the container state and save it to Amazon ECR: docker commit $(docker ps -q --filter "name=dev_postgres_container") 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres:11-2.5-with-data # Also tag it with "latest" and the timestamp docker tag 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres:11-2.5-with-data 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres:latest-with-data docker tag 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres:11-2.5-with-data 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres:$( date -u +%Y%m%d_%H%MZ )-with-data # Push docker push 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres --all-tags
但是,當我在隔離環境中拉容器時,沒有數據:
brew install virtualbox brew install multipass sudo multipass set local.driver=virtualbox multipass launch --name ubuntu2 multipass mount ~/.aws ubuntu2:/home/ubuntu/.aws multipass mount ./db ubuntu2:/home/ubuntu/db multipass exec ubuntu2 -- bash /home/ubuntu/db/test_db_container.sh ### Contents of test_db_container.sh are below: ### curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh sudo apt-get install -y awscli # Pull Docker container: aws ecr get-login-password --region us-east-1 | \ sudo docker login --username AWS --password-stdin 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com sudo docker pull 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres:latest-with-data sudo docker run -d --name the_db 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres:latest-with-data ### ### The auth_user table does not exist. ### sudo docker exec -it the_db /usr/bin/psql -U the_user the_database -c "select count(*) from auth_user;" sudo docker history 23456789JQKA.dkr.ecr.us-east-1.amazonaws.com/dev_postgres:latest-with-data ## Result: IMAGE CREATED CREATED BY SIZE COMMENT 854dbef239c2 23 hours ago postgres 70B 4b1bd66a8d33 23 hours ago COPY ./make_data.sh /make_data.sh 3.03kB buildkit.dockerfile.v0 <missing> 23 hours ago COPY ./20-init_db.sql /docker-entrypoint-ini… 188B buildkit.dockerfile.v0 <missing> 23 hours ago MAINTAINER Me 0B buildkit.dockerfile.v0 [...]
旁注:我考慮過創建一個數據卷容器,但這種方法似乎更簡單且文件更好。
其他注意事項:我無法在 Dockerfile 中添加數據,因為 Postgres 需要在插入數據之前執行。
為什麼不
docker commit
保存我的容器的狀態?為什麼圖像不包含數據?
根據主執行緒下的評論,該解決方案類似於此現有答案中建議的解決方案。它歸結為數據目錄是一個卷,因此位於被修改/送出的容器的上下文之外。