so the command option (CMD) runs once the images are built. Basically, it runs when the networks are all set up. But there is one issue, the postgres image is yet to run and setup the server yet.
So, the option of restart: on-failure will come to use here. The web (one with diesel) container keeps on restarting until db (postgres) container is setup properly. Once postgres runs properly, the diesel migration also runs properly.
here is the Dockerfile
FROM rust
RUN apt update
RUN apt install -y libpq-dev
RUN cargo install diesel_cli --no-default-features --features postgres
WORKDIR /usr/src/actix-tera-test
COPY . .
RUN cargo install --path .
CMD bash -c "diesel setup && actix-tera-test"
As you already noticed your database is not available at the time your are building your container. Another way to circumvent this issue is to include your migrations into the final binary by using the diesel_migrations::embed_migrations! macro. This allows you to just apply migrations as part of the application startup and does remove the need to install diesel_cli inside of your production container.