1

I've been trying to see my web app started from my docker-compose file but nothing is appearing. It works when I serve the app locally but not through docker. I'm using rust and actix-web for my backend set for localhost on port 8080 and I'm exposing the ports for docker-compose but it still isn't working.

my docker-compose file:

services:
  database:
    image: postgres
    restart: always
    expose:
      - 5432
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: zion
      PGDATA: /var/lib/postgresql/data/
    healthcheck:
      test: ["CMD", "pg_isready", "-d", "zion", "-U", "postgres"]
      timeout: 25s
      interval: 10s
      retries: 5
    networks:
      - postgres-compose-network

  test:
    build:
      context: .
      dockerfile: Dockerfile
    entrypoint: ./test-entrypoint.sh
    depends_on:
      database:
        condition: service_healthy
    networks:
      - postgres-compose-network
      
  server:
    build:
      context: .
      dockerfile: Dockerfile
    entrypoint: ./run-entrypoint.sh
    restart: always
    expose:
      - 8080
    ports:
      - 8080:8080
    depends_on:
      database:
        condition: service_healthy
    networks:
      - postgres-compose-network
 
networks:
  postgres-compose-network:
        driver: bridge

backend main.rs

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
    log::info!("starting HTTP server at http://localhost:8080");
    let secret_key = Key::generate();

    let pool = establish_connection();
    log::info!("database connection established");
    HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(pool.clone()))
            .wrap(middleware::Logger::default())
            .wrap(IdentityMiddleware::default())
            .configure(database::routes::user::configure)
            .service(
                Files::new("/", "../frontend/dist")
                    .prefer_utf8(true)
                    .index_file("index.html"),
            )
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}
Sarah
  • 33
  • 5
  • Do you get any errors? Maybe port 8080 is already in use by some other app (like your local deploy) – Nikolay Jan 10 '23 at 06:50
  • @Nikolay It isn't in use by anything, I just get an error like there isn't anything being hosted. – Sarah Jan 10 '23 at 06:58
  • What does `docker ps -a` shows about ports? And also I am not sure, but maybe check syntax `- 8080:8080` -> `- "8080:8080"` – Nikolay Jan 10 '23 at 10:58
  • 1
    Your `bind()` call needs to bind to `0.0.0.0`; if it binds to `127.0.0.1` it will be unreachable from outside its own container. Also see [Rust actix_web inside docker isn't attainable, why?](https://stackoverflow.com/questions/57177889/rust-actix-web-inside-docker-isnt-attainable-why) or more generally [Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip](https://stackoverflow.com/questions/59179831/docker-app-server-ip-address-127-0-0-1-difference-of-0-0-0-0-ip). – David Maze Jan 10 '23 at 11:44

1 Answers1

2

The answer, as provided by @David-Maze, was to bind my app's address to 0.0.0.0 instead of 127.0.0.1.

buddemat
  • 4,552
  • 14
  • 29
  • 49
Sarah
  • 33
  • 5