New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Diesel to connect from a Docker container to a Postgres instance via docker-compose #1218

Closed
alexanderbanks opened this Issue Sep 29, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@alexanderbanks

alexanderbanks commented Sep 29, 2017

Setup

I'm trying to run a Diesel/Iron app that connects to a Postgres instance. For development I'm trying to use Docker/Docker-Compose. This might not be a bug with Diesel (it probably isn't), but I was curious if the Diesel maintainers might have some insight into setting this up properly?

Versions

In terms of versions, I'm using the official Rust Docker image, version rust:1.19.0, postgres:9.6.

What are you trying to accomplish?

The dockerfile looks like this:

FROM rust:1.19.0
WORKDIR app
ADD . /app
EXPOSE 3000
RUN cargo build
ENTRYPOINT ["target/debug/app-api"]

With a docker-compose file:

version: '3'
services:
  db:
    image: "postgres:9.6"
    ports:
      - "5432:5432"
    volumes:
      - ./db/db.sql:/docker-entrypoint-initdb.d/01_db.sql
      - ./db/data.sql:/docker-entrypoint-initdb.d/02_data.sql
    environment:
      POSTGRES_DB: my_db
      POSTGRES_PASSWORD: docker
      POSTGRES_USER: docker
  web:
    build: .
    ports:
     - "3000:3000"
    links:
      - "db:db"
    env_file:
      - .env

The env file:

DATABASE_URL=postgresql://docker:docker@db:5432/my_db

What is the expected output?

Diesel connects to the postgres image just like it would a postgres image not on a local network

What is the actual output?

 --> src/schema.rs:1:1
  |
1 | infer_schema!("dotenv:DATABASE_URL");
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: Could not load table names from database `postgresql://docker:docker@db:5432/my_db`: StringError("Failed to establish a database connection at postgresql://docker:docker@db:5432/my_db. Error: BadConnection(\"could not translate host name \\\"db\\\" to address: Name or service not known\\n\")")
  = note: this error originates in a macro outside of the current crate

It appears that Diesel isn't resolving db into its actual IP? But that might make sense? Because the image hasn't been created,so there isn't a configuration setup yet to convert db to an IP?

Are you seeing any additional errors?

Nope, that's the only one. I had it working prior to using diesel/infer_schema.

Steps to reproduce

It fails while building the web image, so using the above config will never successfully build an image.

Again, I doubt this is an error with Diesel, I'm mostly just looking for help? I figured submitting an issue might be the best way define the problem?

@sgrif

This comment has been minimized.

Member

sgrif commented Sep 29, 2017

I don't know enough about docker to help with your issue, but you can use diesel print-schema instead of infer_schema! to avoid needing a database at compile time.

@ghotiphud

This comment has been minimized.

ghotiphud commented Oct 14, 2017

Docker compose doesn't wait for the DB container to start up. Try https://github.com/vishnubob/wait-for-it

@ghotiphud

This comment has been minimized.

ghotiphud commented Oct 14, 2017

@alexanderbanks

This comment has been minimized.

alexanderbanks commented Oct 16, 2017

@ghotiphud thanks for the response. I actually took the seemingly simpler approach of running diesel print-schema on each DB change and then modifying the schema.rs file with its output.

We can close this, I apologize for not responding to @sgrif . All is well.

@sgrif sgrif closed this Oct 16, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment