我想使用
创建一个docker镜像docker build --tag docker-rust-image-test .
但它不起作用。错误是:
0.628 error: proc macro panicked
0.628 --> src/config/db.rs:6:44
0.628 |
0.628 6 | pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
0.628 | ^^^^^^^^^^^^^^^^^^^
0.628 |
0.628 = help: message: Failed to receive migrations dir from None
这是我的
db.rs
文件:
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
pub type DbPool = Pool<ConnectionManager<PgConnection>>;
pub fn establish_connection_pool(database_url: &str) -> DbPool {
let manager = ConnectionManager::<PgConnection>::new(database_url);
Pool::builder()
.build(manager)
.expect("Failed to create database pool")
}
pub fn run_migration(connection: &mut impl MigrationHarness<diesel::pg::Pg>) {
match connection.run_pending_migrations(MIGRATIONS) {
Ok(_) => {
println!("Migrations successfully completed");
}
Err(e) => {
panic!("error running pending migrations {}", e)
}
};
}
这是我的 Dockerfile:
# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
ARG RUST_VERSION=1.75.0
ARG APP_NAME=hoa-backend
################################################################################
# xx is a helper for cross-compilation.
# See https://github.com/tonistiigi/xx/ for more information.
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.3.0 AS xx
FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-alpine AS build
ARG APP_NAME
WORKDIR /app
# Copy cross-compilation utilities from the xx stage.
COPY --from=xx / /
# Install host build dependencies.
RUN apk add --no-cache clang lld musl-dev git file
# This is the architecture you’re building for, which is passed in by the builder.
# Placing it here allows the previous steps to be cached across architectures.
ARG TARGETPLATFORM
# Install cross-compilation build dependencies.
RUN xx-apk add --no-cache musl-dev gcc
RUN --mount=type=bind,source=src,target=src \
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
--mount=type=cache,target=/app/target/,id=rust-cache-${APP_NAME}-${TARGETPLATFORM} \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/ \
xx-cargo build --locked --release --target-dir ./target && \
cp ./target/$(xx-cargo --print-target-triple)/release/$APP_NAME /bin/server && \
xx-verify /bin/server
FROM alpine:3.18 AS final
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser
COPY --from=build /bin/server /bin/
EXPOSE 8080
CMD ["/bin/server"]
这个文件是从 docker init 命令生成的
chatgpt
表示要在Cargo.toml
文件中添加“embedded_migration”,如下所示:diesel = { version = "2.1.4", features = ["postgres", "embedded_migration", "r2d2", "chrono"] }
diesel_migrations = "2.1.0"
但是柴油没有这个功能
cargo run
和cargo build
在提供的
Dockerfile
中,migrations
文件夹未映射或复制到构建软件的容器中。
您还必须为迁移文件夹添加一行:
--mount=type=bind,source=migrations,target=migrations \