Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Rust: ccould not extract weather information after I dockerize the code

$
0
0

Just a follow up to the question

The code in question being

use actix_web::{web, App, HttpServer, HttpResponse};use reqwest::Client;use serde::Deserialize;use std::env;use chrono::{DateTime, Utc, Local};#[derive(Deserialize)]struct WeatherResponse {    main: Main,    name: String,}#[derive(Deserialize)]struct Main {    temp: f32,}async fn index() -> HttpResponse {    let api_key = env::var("OPENWEATHERMAP_API_KEY").expect("weather.env");    let city = "tokyo";    let url = format!("http://api.openweathermap.org/data/2.5/weather?q={}&appid={}&units=metric",        city, api_key    );    let response = Client::new().get(&url).send().await;    match response {        Ok(res) => {            let weather_data: WeatherResponse = res.json().await.expect("Failed to parse JSON response");            let temperature = weather_data.main.temp;            let city_name = weather_data.name;            let utc_time: DateTime<Utc> = Utc::now();            let local_time: DateTime<Local> = utc_time.with_timezone(&Local);            HttpResponse::Ok().body(format!(" Today is {} \n Weather in {}: {:.1}°C", local_time, city_name, temperature))        }        Err(_) => HttpResponse::InternalServerError().body("Failed to fetch weather data"),    }}#[actix_web::main]async fn main() -> std::io::Result<()> {    dotenv::dotenv().ok();    HttpServer::new(|| {        App::new().route("/", web::get().to(index))    })    .bind("127.0.0.1:4040")?    .run()    .await}

And the dockerfile being

# Rust as the base imageFROM rust:bookworm as build# Create a new empty shell projectRUN USER=root cargo new --bin weatherWORKDIR /weather# Copy our manifestsCOPY ./Cargo.lock ./Cargo.lockCOPY ./Cargo.toml ./Cargo.toml# Build only the dependencies to cache themRUN cargo build --releaseRUN rm src/*.rs# Copy the source codeCOPY ./src ./srcCOPY .env  .env# Build for release.RUN rm ./target/release/deps/weather*RUN cargo build --release# The final base imageFROM debian:bookworm-slim# Copy from the previous buildCOPY --from=build /weather/target/release/weather /usr/src/weatherCOPY --from=build /weather/.env /usr/src/.env# get SSLRUN apt-get update && apt install -y opensslEXPOSE 8080 4040 3000 80 443 22# Run the binaryCMD ["/usr/src/weather"]

I can see the weather information if i build and rn it locally but the weather information could not be retrieved after i dockerize the apps

I have run it with docker run -p 3000:4040 -p 8080:8080 devops_rust

Do I have to start another server in my code??

Thanks


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>