Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I was previously running the Docker container for my web application below:

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

Which became the below docker-compose script:

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"

When I launch the container, it seems like postgres is no longer running (Connection refused, netstat -al | grep postgres doesn't show it), and thus, the app doesn't work. I reviewed the change I have made, and realized that the docker-compose.yml has no analog for the tomcat sleep and curl command I had given in the run script - should it? I couldn't find any information online regarding a proper analog. Is something else potentially afoot here? Please advise if any further details are required.

EDIT: docker ps yields:

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

and the logs for the container:

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

and thus inside the container, :

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:*                          

We can see that the memcached container is up and running, however postgres is absent. The only thing running on the localhost interface is bound to 8005, and it isn't postgres. I should add finally that I am able to telnet memcached on its port from the web container and confirm its operation, however I can't do the same with postgres, and I'm not sure why.

share|improve this question
    
"it seems like postgres is no longer running" Well, is it? That's what docker ps (or docker-compose ps) is for. And if it has failed docker logs might show you why. Also, something is terribly wrong with the indentation of the docker-compose.yml document you posted; if you take a moment to clean it up, you'll avoid answers along the lines of "that couldn't possibly work". – larsks Jun 1 '16 at 13:08
    
My apologies, I always grapple with the code pasting on Stack Overflow - it only idents the first line properly and I have to do the rest by hand. Yes, according to docker ps, the container is running, and on the correct port - I've added that response, logs, and the netstat result. – Rome_Leader Jun 1 '16 at 13:24
    
When I connected to the _db_1 container, I can see postgres running there. How might I bridge the gap? – Rome_Leader Jun 1 '16 at 15:44
up vote 3 down vote accepted

There are a number of things that can probably be removed from your docker-compose.yml file, assuming that the database and memcached services are dedicated to your web application (that is, they don't need to be accessed from anywhere else).

It doesn't make sense to specify both image and build in a service definition, so I've made a few assumptions about what you actually want.

I would start with something like this:

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

You don't need ports directives for either the db or mem services, because you don't need to expose these services to your host (or elsewhere). A docker container can access services on another container on the same network without any special directives.

Within the web container, you can access either the db or mem container by name, because this is how newer versions of Docker operate when using a non-default network: Docker creates an internal DNS service and registers the container names there. So, for example, from inside the web container I can use the psql command line postgresql client to connect to the database server like this:

$ 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=#

You would configure your web application to access the database server at host db, port 5432.

With this environment up and running, you could connect to your web service from your host at http://localhost:8000, or from either your host or another host on the network at http://<your_hostname_or_ip_here>:8000.

share|improve this answer
    
"You would configure your web application to access the database server at host db, port 5432." GAH! Would you believe after all this, my warehouse.xml still had a reference to localhost in one of the connection URLs that was causing my headache... Many thanks for your help in cleaning up/consolidating my compose file, and helping me to understand docker a little better. – Rome_Leader Jun 3 '16 at 11:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.