问题
I tried to install postgresql-9.3 in the container contains images "ubuntu: 14:04"
The first step, which I did make images "ubuntu: 14:04"
$ sudo Docker pull ubuntu:14.04
The second step, I create a container with the command
$ sudo docker run -t -i ubuntu:14.04 /bin/bash
The third step, I install postgresql-9.3 in a container contain "ubuntu:14.04" (in a container containing ubuntu)
I tried to get in postgresql could, then I tried to get out containers, straight out with the command
# exit
I tried to log back in, the container postgresql I install missing, whether it can do and helped explain why?
Docker itself, whether running in the kernel?
Hardware> OS> Docker.
Docker concept:
Docker = images> Container
回答1:
You should not install anything in a bash session.
Since docker uses UnionFS (union filesystem service), as soon as you exit your bash session, any modification is lost when the container is removed.
You could try a docker commit of your "Exited" container, but that is not how you build a new image.
What you do is create a text file named Dockerfile, in which you follow similar steps as the official postgres image Dockerfile (except you want to start from ubuntu).
It includes:
RUN apt-get update \
&& apt-get install -y postgresql-common \
&& sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf \
&& apt-get install -y \
postgresql-$PG_MAJOR=$PG_VERSION \
postgresql-contrib-$PG_MAJOR=$PG_VERSION \
&& rm -rf /var/lib/apt/lists/*
You the run docker build -t mypostgres .: that will build a new image which will contain postgres.
In other words, the Dockerfile is there to persists the installation steps in a declarative text file, from which you can build an image at any time.
回答2:
If you find easier to install all with bash and not with Dockerfile and build command you could do exactly as you wrote for first and second steps but after as third step quit bash with
CONTROL + P followed by CONTROL + Q
that's will exit container leaving it running and after use
docker commit mycurrentcontainer mynewimagewithpostgrestobeuseinfuture
回答3:
This has fixed the issue. These steps are followed
Step1: Docker builds -t Mohan. step2: Docker run -it Mohan bash Mohan is your container name
This is my Dockerfile:
FROM Ubuntu: 18.04
FROM postgres: 9.6
RUN apt-get update && apt-get install -q -y postgresql-9.6 postgresql-client-9.6 postgresql-contrib-9.6 postgresql-client-common postgresql-common RUN echo postgres:postgres | chpasswd
RUN pg_createcluster 9.6 main --start
CMD /etc/init.d/postgresql start
CMD su -c "psql -c \"ALTER USER postgres PASSWORD 'postgres';\"" postgres
来源:https://stackoverflow.com/questions/33913474/docker-install-postgresql-in-a-container-contain-ubuntu14-04-lost-when-out