制作一个包含Tomcat和PostgreSQL的Docker镜像

由于很难找到“将Tomcat和PostgreSQL镜像分别创建并在docker-compose中运行”的例子,因此我进行了一些尝试和错误,试图将它们合并到一个容器中。

创建Dockerfile。

    Dockerize PostgreSQL | Docker Documentation

因为在一个基于Ubuntu镜像上有一个安装PostgreSQL 9.3的示例,所以我决定按照这个示例,以一个带有Tomcat的镜像作为基础,而不是使用Ubuntu作为基础。

Dockerfile

Docker构建文件

FROM tomcat:9.0-jre8
MAINTAINER SvenDowideit@docker.com

# Add the PostgreSQL PGP key to verify their Debian packages.
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --no-tty --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8

# Add PostgreSQL's repository. It contains the most recent stable release
#     of PostgreSQL, ``9.6``.
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list

# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.6
#  There are some warnings (in red) that show up during the build. You can hide
#  them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y software-properties-common postgresql-9.6 postgresql-client-9.6 postgresql-contrib-9.6

# Note: The official Debian and Ubuntu images automatically ``apt-get clean``
# after each ``apt-get``

# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.6`` package when it was ``apt-get installed``
USER postgres

# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
# then create a database `docker` owned by the ``docker`` role.
# Note: here we use ``&&\`` to run commands one after the other - the ``\``
#       allows the RUN command to span multiple lines.
RUN    /etc/init.d/postgresql start &&\
    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    createdb -O docker docker

# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.6/main/pg_hba.conf

# And add ``listen_addresses`` to ``/etc/postgresql/9.6/main/postgresql.conf``
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.6/main/postgresql.conf

# Expose the PostgreSQL port
EXPOSE 5432

# Add VOLUMEs to allow backup of config, logs and databases
VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

# Set the default command to run when starting the container
CMD ["/usr/lib/postgresql/9.6/bin/postgres", "-D", "/var/lib/postgresql/9.6/main", "-c", "config_file=/etc/postgresql/9.6/main/postgresql.conf"]

从tomcat:9.0-jre8

因为我想使用Java 8,所以需要带有JRE 8。

使用中文进行本地化的重新表述:
运行 apt-key adv …

添加了 –no-tty。如果没有它,将会出现 gpg: cannot open ‘/dev/tty’: No such device or address 的错误消息(参考)。

运行 apt-get update 并且安装 …

把9.3替换为9.6。
删除python-software-properties,好像只需要software-properties-common就可以了。

制作Docker映像

docker build ./pg9_tomcat9 -t pg9_tomcat9
image.png

创建容器并运行PostgreSQL。

docker run -d --rm -p 8888:8080 -p:5432:5432 --name pg9_tomcat9_1 pg9_tomcat9

启动容器确认

docker ps
image.png

确认连接至PostgreSQL

尝试从Docker主机连接到psql。

psql -h localhost -U docker -d docker
# Password: docker

docker=# \l
image.png

tomcat 启动

docker exec -d pg9_tomcat9_1 bash catalina.sh run

确认Tomcat操作

用主机的Web浏览器打开 http://localhost:8888/。

image.png

我想在 Dockerfile 中写入 CMD,以便启动tomcat,但是我尝试过但没有成功(如果启动tomcat,postgres将无法启动)。

其他

进入容器,并以 bash 命令行方式操作

docker exec -it -u 0 pg9_tomcat9_1 bash

如果不加 -u 0 ,就会以 postgres 用户登录。

停止Tomcat

docker exec pg9_tomcat9_1 bash catalina.sh stop

在运行 Docker 时,您可以自由指定卷。

广告
将在 10 秒后关闭
bannerAds