我正在尝试创建一个已包含开发数据的 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
保存容器的状态?为什么图像不包含数据?
答案1
根据主线程下的评论,解决方案与建议的解决方案类似这个现有的答案。归根结底,数据目录是一个卷,因此存在于被修改/提交的容器上下文之外。