Running PostgreSql inside Docker

I have the following config lines:

RUN     sudo apt-get -y install postgresql
USER    postgres
RUN    /etc/init.d/postgresql start &&\
    psql --command "CREATE USER test WITH SUPERUSER PASSWORD 'test';" &&\
    createdb -O test test

CMD     ["mono", "src/Rest.Api/bin/Debug/Rest.Api.exe"]

However, running the final command to spin up my API yields this:

  • Docker in the big picture for complete DevOps
  • Docker --add-host flags not working for registry 2.0 webhooks notifications
  • docker-compose pull results in x509: certificate signed by unknown authority
  • Docker container published via port forward can't connect to its own published port
  • Window version of Docker not able to mount local directory to ubuntu image on container?
  • ASP.NET 5 on Azure Docker on Ubuntu Server
  • setting listen on
    Failed to establish a connection to 'localhost'.
      at Npgsql.NpgsqlClosedState.Open (Npgsql.NpgsqlConnector context, Int32 timeout) [0x00000] in <filename unknown>:0
      at Npgsql.NpgsqlConnector.Open () [0x00000] in <filename unknown>:0
      at Npgsql.NpgsqlConnectorPool.GetPooledConnector (Npgsql.NpgsqlConnection Connection) [0x00000] in <filename unknown>:0

    Which looks like PostgreSQl isn’t running – what do I need to do to get postgresql running?

  • How to automatically start services inside a docker container
  • Docker TLS Security on Remote Client
  • Restart failed Selenium browser
  • Nodejs error: uncaughtException: connection timeout. Occurs renadomly
  • Marathon treats healthy tasks as unhealthy and kills them
  • Connecting a Docker container running PHP to the host's(ubuntu on VirtualBox) MSQL server
  • One Solution collect form web for “Running PostgreSql inside Docker”

    The line

    RUN    /etc/init.d/postgresql start

    only serves to start Postgres while your image is being built.

    To ensure it is running at execution, you will want to create a script as entrypoint (using either ENTRYPOINT or CMD depending on what you want, which starts Postgres and runs your application.

    The simplest form of this would be something like

    /etc/init.d/postgresql start
    exec mono src/Rest.Api/bin/Debug/Rest.Api.exe

    You could save this as and use CMD [""] as the last line in your Dockerfile.

    However, at this point it might be worth looking into something more robust like Phusion’s baseimage.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.