将Docker运行脚本转移到撰写文件 - postgres不运行?

时间:2016-06-01 13:01:33

标签: postgresql docker docker-compose

我以前在我的网络应用程序中运行Docker容器:

echo Starting postgres container...
docker run -d -p 5432:5432 --name db my_db
echo Postgres container started

echo Starting memcached container...
docker run -d -p 11211:11211 --name mem memcached
echo Memcached container started

echo Starting web container...
docker run -d -p 8000:8080 --name web --link db:postgres --link      mem:memcache my_web_app 
echo Web container started

echo Waiting for tomcat to initialize...
sleep 5

echo curling logon.jsf
curl http://111.111.11.111:8000/app/logon.jsf #Real URL redacted
echo Application started

哪个成为下面的docker-compose脚本:

version: '2'
services:
  web:
    image: tomcat:7.0.69-jre8
    build: ./ci
    ports: 
      - "8000:8080"
    depends_on:
      - db
      - mem
  db:
    image: postgres:9.4.7
    build: ./db
    ports: 
      - "5432:5432" 
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
  mem:
    image: memcached
    ports: 
      - "11211:11211"

当我启动容器时,似乎postgres不再运行(连接被拒绝,netstat -al | grep postgres没有显示它),因此,应用程序不起作用。我回顾了我所做的改变,并意识到docker-compose.yml对于我在运行脚本中给出的tomcat sleep和curl命令没有模拟 - 应该吗?我无法在网上找到有关正确模拟的任何信息。还有其他可能在这里发生的事吗?如果需要进一步的详细信息,请告知。

编辑: docker ps yield:

CONTAINER ID        IMAGE                COMMAND                    CREATED             STATUS              PORTS                      NAMES
a942be02fc22        tomcat:7.0.69-jre8   "catalina.sh run"        5 minutes ago       Up About a minute   0.0.0.0:8000->8080/tcp        memcachedocker_web_1
fd6074cf8ec9        postgres:9.4.7       "/docker-entrypoint.s"   5 minutes ago       Up About a minute   0.0.0.0:5432->5432/tcp     memcachedocker_db_1
ce18771fee28        memcached            "/entrypoint.sh memca"   5 minutes ago       Up About a minute   0.0.0.0:11211->11211/tcp   memcachedocker_mem_1

和容器的日志:

Success. You can now start the database server using:

postgres -D /var/lib/postgresql/data
or
pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....LOG:  database system was shut down at    2016-06-01 13:09:23 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
done
server started
CREATE DATABASE

CREATE ROLE


/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
CREATE DATABASE


LOG:  received fast shutdown request
LOG:  aborting any active transactions
LOG:  autovacuum launcher shutting down
LOG:  shutting down
waiting for server to shut down....LOG:  database system is shut down
done
server stopped

PostgreSQL init process complete; ready for start up.

LOG:  database system was shut down at 2016-06-01 13:09:25 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

因此在容器内,:

Proto Recv-Q Send-Q Local Address           Foreign Address          State      
tcp        0      0 127.0.0.11:44095        0.0.0.0:*               LISTEN     
tcp        0      0 172.18.0.4:38868        132.246.2.23:80         TIME_WAIT  
tcp        0      0 172.18.0.4:36424        5.153.231.35:80         TIME_WAIT  
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN     
tcp6       0      0 :::8009                 :::*                    LISTEN     
tcp6       0      0 :::8080                 :::*                    LISTEN     
tcp6       0      0 172.18.0.4:47522        172.18.0.2:11211        ESTABLISHED
tcp6       0      0 172.18.0.4:47518        172.18.0.2:11211        ESTABLISHED
tcp6       0      0 172.18.0.4:47524        172.18.0.2:11211        ESTABLISHED
tcp6       0      0 172.18.0.4:47520        172.18.0.2:11211        ESTABLISHED
tcp6       0      0 172.18.0.4:47516        172.18.0.2:11211        ESTABLISHED
tcp6       0      0 172.18.0.4:47514        172.18.0.2:11211        ESTABLISHED
udp        0      0 172.18.0.4:48360        10.0.2.3:53             ESTABLISHED
udp        0      0 127.0.0.11:40276        0.0.0.0:*                          

我们可以看到memcached容器已启动并运行,但是没有postgres。在localhost接口上运行的唯一东西绑定到8005,并且它不是postgres。我最后应该补充说,我能够从Web容器上telnet memcached并确认其操作,但我不能对postgres做同样的事情,我不知道为什么。

1 个答案:

答案 0 :(得分:3)

有许多内容可能可能从您的内容中删除 docker-compose.yml文件,假设数据库和memcached 服务专用于您的Web应用程序(也就是说,它们不是 需要从其他任何地方访问。)

在服务中指定imagebuild没有意义 定义,所以我对你的实际做了一些假设 想。

我会从这样的事情开始:

version: '2'
services:
  web:
    build: ./ci
    ports:
      - '8000:8080'
    depends_on:
      - db
      - mem
  db:
    image: postgres
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
  mem:
    image: memcached

portsdb您不需要mem指令 服务,因为您不需要将这些服务公开给您的主机 (或其他地方)。 docker容器可以访问另一个的服务 容器在同一网络上,没有任何特殊指令。

web容器中,您可以访问dbmem 容器名称,因为这是更新版本的Docker 在使用非默认网络时运行:Docker创建内部网络 DNS服务并在那里注册容器名称。所以,例如, 从web容器内部我可以使用psql命令行 postgresql客户端连接到数据库服务器,如下所示:

$ docker exec -it myservice_web_1 sh
web_1# psql -h db -U admin
Password for user admin: 
psql (9.4.6, server 9.5.1)
WARNING: psql major version 9.4, server major version 9.5.
         Some psql features might not work.

admin=#

您可以配置Web应用程序以访问数据库服务器 在主机db,端口5432

启动并运行此环境后,您可以通过http://localhost:8000的主机或主机或网络上http://<your_hostname_or_ip_here>:8000的其他主机连接到您的网络服务。