rust-lang / docker-rust

The official Docker images for Rust
428 stars 89 forks source link

[ FIXED ] Cannot build Rust App Docker Image because of: spurious network error ... #176

Closed MrMic closed 4 months ago

MrMic commented 4 months ago

Hi,

When I try to build an image with Rust App, I got this:

=======================================================================

❯ docker build -t book-api .
[+] Building 19.0s (8/8) FINISHED                                                                                                                         docker:default
 => [internal] load build definition from Dockerfile                                                                                                                0.0s
 => => transferring dockerfile: 360B                                                                                                                                0.0s
 => [internal] load metadata for docker.io/library/rust:latest                                                                                                      0.4s
 => [internal] load .dockerignore                                                                                                                                   0.0s
 => => transferring context: 2B                                                                                                                                     0.0s
 => [1/4] FROM docker.io/library/rust:latest@sha256:a71cd88f9dd32fbdfa67c935f55165ddd89b7166e95de6c053c9bf33dd7381d5                                                0.0s
 => => resolve docker.io/library/rust:latest@sha256:a71cd88f9dd32fbdfa67c935f55165ddd89b7166e95de6c053c9bf33dd7381d5                                                0.0s

 mic@192.168.1.70  …/book-api    main ? ............................................................................................ 🦀 v1.76.0 is  v0.1.0     13:47 
❯ docker build -t book-api .
[+] Building 13.2s (8/8) FINISHED                                                                                                                         docker:default
 => [internal] load build definition from Dockerfile                                                                                                                0.0s
 => => transferring dockerfile: 331B                                                                                                                                0.0s
 => [internal] load metadata for docker.io/library/rust:latest                                                                                                      0.8s
 => [internal] load .dockerignore                                                                                                                                   0.0s
 => => transferring context: 2B                                                                                                                                     0.0s
 => [1/4] FROM docker.io/library/rust:latest@sha256:a71cd88f9dd32fbdfa67c935f55165ddd89b7166e95de6c053c9bf33dd7381d5                                                0.0s
 => => resolve docker.io/library/rust:latest@sha256:a71cd88f9dd32fbdfa67c935f55165ddd89b7166e95de6c053c9bf33dd7381d5                                                0.0s
 => [internal] load build context                                                                                                                                   0.2s
 => => transferring context: 184.83kB                                                                                                                               0.2s
 => CACHED [2/4] WORKDIR /app                                                                                                                                       0.0s
 => CACHED [3/4] COPY . .                                                                                                                                           0.0s
 => ERROR [4/4] RUN cargo build --release                                                                                                                          12.1s
------                                                                                                                                                                   
 > [4/4] RUN cargo build --release:                                                                                                                                      
0.624     Updating crates.io index                                                                                                                                       
0.627 warning: spurious network error (3 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)                                      
1.930 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)                                      
5.431 warning: spurious network error (1 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)                                      
11.93 error: failed to get `diesel` as a dependency of package `book-api v0.1.0 (/app)`
11.93 
11.93 Caused by:
11.93   failed to query replaced source registry `crates-io`
11.93 
11.93 Caused by:
11.93   download of config.json failed
11.93 
11.93 Caused by:
11.93   failed to download from `https://index.crates.io/config.json`
11.93 
11.93 Caused by:
11.93   [6] Couldn't resolve host name (Could not resolve host: index.crates.io)
------
Dockerfile:11
--------------------
   9 |     
  10 |     # Build the application in release mode
  11 | >>> RUN cargo build --release
  12 |     
  13 |     # Set the command to run the binary
--------------------
ERROR: failed to solve: process "/bin/sh -c cargo build --release" did not complete successfully: exit code: 101

==================================================================================
On my host, I can build, update, download everything for Rust but inside the container 
during the build phase, I got this damned Issue. I am stuck and cannot anything!

Just in case here are my Cargo.toml and my main.rs:

=====================================================================
[package]
name = "book-api"
version = "0.1.0"
edition = "2021"

[dependencies]
diesel = { version = "2.1.4", features = ["postgres"] }
dotenv = "0.15.0"
rocket = "0.5.0"
rocket_contrib = "0.4.11"

=====================================================================
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate rocket;

use diesel::pg::PgConnection;
use diesel::prelude::*;
use dotenv::dotenv;
use std::env;

use rocket::response::content::Json;
use rocket_contrib::json::JsonValue;

pub fn establish_connection() -> PgConnection {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
}

#[get("/books")]
fn books() -> Json<JsonValue> {
    use crate::schema::books::dsl::*;

    let connection = establish_connection();
    let results = books
        .limit(10)
        .load::<Book>(&connection)
        .expect("Error loading books");

    let mut books_json = json![];
    for book in results {
        books_json.push(json!({
            "title": book.title,
            "author": book.author,
            "publisher": book.publisher,
            "year": book.year,
        }));
    }

    Json(json!({ "books": books_json }))
}

fn main() {
    rocket::ignite().mount("/", routes![books]).launch();
}

============================================================

Thank for the help. Rgds, Michaël

jdno commented 4 months ago

I cannot reproduce the issue locally using the same image. I'm using this command to pull the exact same image as you used above and open in interactive Bash shell:

$ docker run -it rust@sha256:a71cd88f9dd32fbdfa67c935f55165ddd89b7166e95de6c053c9bf33dd7381d

In that shell, I'm then using curl to fetch config.json which works fine:

root@6aa30c25e68e:/# curl -I https://index.crates.io/config.json
HTTP/2 200

What happens when you run curl in another container, e.g. ubuntu:latest?

jdno commented 4 months ago

As a side note, your issue is very difficult to read. It would be much easier to parse if the shell output was inside a code block.

MrMic commented 4 months ago

I cannot reproduce the issue locally using the same image. I'm using this command to pull the exact same image as you used above and open in interactive Bash shell:

$ docker run -it rust@sha256:a71cd88f9dd32fbdfa67c935f55165ddd89b7166e95de6c053c9bf33dd7381d

In that shell, I'm then using curl to fetch config.json which works fine:

root@6aa30c25e68e:/# curl -I https://index.crates.io/config.json
HTTP/2 200

What happens when you run curl in another container, e.g. ubuntu:latest?

Me too I can run:

$> docker run -it --rm --name test-rust rust:latest

But it is NOT what I do: I am trying to build a docker image with a Rust App inside. Try this: 1/ Create a project with:

$> cargo init test

2/ then, inside this project add a dependency, for example:

$> cargo add tokyo

3/ Create a Dockerfile, like this one:

FROM rust:latest

COPY . .

RUN cargo build --release

CMD [ "./target/release/test" ]

4/ And finally try to build the image with:

$> docker build .

As you can see in the following log, I can resolve docker.io to get the base image but the issue arise during the docker build ...

❯ docker build .                
[+] Building 13.7s (7/7) FINISHED                                                                                                                                             docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                    0.1s
 => => transferring dockerfile: 184B                                                                                                                                                    0.0s
 => [internal] load metadata for docker.io/library/rust:latest                                                                                                                          0.9s
 => [internal] load .dockerignore                                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                                         0.0s
 => [internal] load build context                                                                                                                                                       0.1s
 => => transferring context: 69.38kB                                                                                                                                                    0.0s
 => CACHED [1/3] FROM docker.io/library/rust:latest@sha256:a71cd88f9dd32fbdfa67c935f55165ddd89b7166e95de6c053c9bf33dd7381d5                                                             0.0s
 => => resolve docker.io/library/rust:latest@sha256:a71cd88f9dd32fbdfa67c935f55165ddd89b7166e95de6c053c9bf33dd7381d5                                                                    0.0s
 => [2/3] COPY . .                                                                                                                                                                      0.1s
 => ERROR [3/3] RUN cargo build --release                                                                                                                                              12.4s
------                                                                                                                                                                                       
 > [3/3] RUN cargo build --release:                                                                                                                                                          
1.073     Updating crates.io index                                                                                                                                                           
1.074 warning: spurious network error (3 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)                                                          
2.225 warning: spurious network error (2 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)                                                          
5.726 warning: spurious network error (1 tries remaining): [6] Couldn't resolve host name (Could not resolve host: index.crates.io)                                                          
12.23 error: failed to get `tokyo` as a dependency of package `tets v0.1.0 (/)`
12.23 
12.23 Caused by:
12.23   failed to query replaced source registry `crates-io`
12.23 
12.23 Caused by:
12.23   download of config.json failed
12.23 
12.23 Caused by:
12.23   failed to download from `https://index.crates.io/config.json`
12.23 
12.23 Caused by:
12.23   [6] Couldn't resolve host name (Could not resolve host: index.crates.io)
------
Dockerfile:8
--------------------
   6 |     # ENV CARGO_HTTP_MULTIPLEXING=false
   7 |     
   8 | >>> RUN cargo build --release
   9 |     
  10 |     CMD [ "./target/release/rust-web-dev" ]
--------------------
ERROR: failed to solve: process "/bin/sh -c cargo build --release" did not complete successfully: exit code: 101

This drives me crazy!!! I cannot release anything for more than 1 week ...

Any help is welcome. Rgds,

jdno commented 4 months ago

Hm. I've went through the steps you listed, but cannot reproduce the issue locally. I also cloned https://github.com/MrMic/Rust_Web_Development and built the Dockerfiles in there without hitting the error. And judging from this build, the issue also doesn't appear on GitHub Actions. Given all of this, I still think this is a problem with your local Docker installation.

Can you try to reproduce the issue in a different container, e.g. ubuntu:latest?

First, start the container:

docker run -it --rm --name docker-rust-176 ubuntu:latest

Inside the container, install curl:

apt update
apt install curl

Then try to fetch the configuration:

curl -I https://index.crates.io/config.json
MrMic commented 4 months ago

Everything is OK:

root@0317ba46a436:/# curl -I https://index.crates.io/config.json
HTTP/2 200 
content-type: application/json
content-length: 76
last-modified: Mon, 23 May 2022 19:23:53 GMT
x-amz-version-id: OLJqOQ3bTJ8rGepGZwmcEkt2gR_Fm1bY
accept-ranges: bytes
server: AmazonS3
date: Fri, 23 Feb 2024 21:41:22 GMT
etag: "7a39851bdb021fcef53172317860f970"
vary: Accept-Encoding
x-cache: Hit from cloudfront
via: 1.1 ac3060b504d45421db0adf8239900fec.cloudfront.net (CloudFront)
x-amz-cf-pop: MRS52-P3
x-amz-cf-id: 7SiHLYm1wEHV-fVd1Uck3lXMKDR-YIjanV2nc2veC14ZYTkdnrGZvQ==
age: 451
MrMic commented 4 months ago

What I cannot explain is that inside the Docker Image construction, I can download the base image (so resolve the hostname where it comes from) and failed to resolve crates.io to update the index , when cargo build is invoked inside the same Dockerfile.

MrMic commented 4 months ago

I finally found the issue! One has to add this key dns to /etc/docker/daemon.json on Ubuntu 20.04:

{
    "insecure-registries" : ["localhost:32000"],
    "dns": ["8.8.8.8"]
}

Thanks, @jdno for the hint about Docker installation. It helps me to search in another direction.

Rgds,

jdno commented 4 months ago

Great that you figured out the issue and that everything works now! 🥳