LukeMathWalker / zero-to-production

Code for "Zero To Production In Rust", a book on API development using Rust.
https://www.zero2prod.com
Apache License 2.0
5.86k stars 519 forks source link

Considerations for Chapter 5.3.1 #238

Closed chdalski closed 11 months ago

chdalski commented 11 months ago

First of all thank you for the great book :1st_place_medal:

I encountered some problems in Chapter 5.3.1...

The first problem:

The Chapter includes a the Dockerfile, with is all well and good until it comes to COPY . . The issue with that command is that it copies everything. This includes unwanted files and folders like .env, .git, target etc. This folders shouldn't be copied - especially target can get pretty big.

As a solution one can either use a .dockerignore file or use dedicated COPY statements.

Opt-in .dockerignore example:

/**
!./.sqlx
!./migrations
!./src
!Cargo.lock
!Cargo.toml
!configuration.yaml

Note: And Opt-out approach might be better for the book, because it's not easy for beginners to track errors when docker build... fails because of missing files.

The second problem:

In Chapter 3.8.5.4 we add our .env file to git. Quote: Commit the .env file to version control - we will need it in CI soon enough!

In Chapter 5.3.1 is a list of the files in our git root. This list doesn't include the .env file. Furthermore, if the .env file is copied to docker sqlx will ignore the sqlx-data.json.

Btw.: Did you try shuttle.rs as an alternative to docker deployments?

LukeMathWalker commented 11 months ago

If you keep reading, you will indeed find that a .dockerignore file gets added! You can see it on this repository as well: https://github.com/LukeMathWalker/zero-to-production/blob/main/.dockerignore It takes care to ignore .env as well, as you correctly noted.

In Chapter 5.3.1 is a list of the files in our git root. This list doesn't include the .env file.

Good catch, thanks!

Did you try shuttle.rs as an alternative to docker deployments?

I've collaborated with the shuttle team on a few things, but it's not a good fit for a book like this: the service is too new and I prefer to build up skills that transfer to the vast majority of cloud providers in the market. For better or worse, Docker-based deployments are the current industry standard.

chdalski commented 11 months ago

Guess I should have read further first, but I was eager to try things out and stumbled across these points.

Maybe it would be a good idea to introduce the .dockerignore a bit earlier? Especially the target folder seems problematic - mine had a size of 16GB and with the .env in place the build fails no matter what.

However, that's up to you - I considers this issues as discussed. So please feel free to close it whenever you see fit.

LukeMathWalker commented 11 months ago

The book is built around the "fail first, fix afterwards" philosophy to cement the importance of each solution/pattern by clearly motivating why it's needed. The same applies here for .dockerignore!

chdalski commented 11 months ago

I'm not arguing with that and I really like that approach. I'm just saying that while I found some hints that errors are expected or a solution to a problem was provided on previous chapters the same didn't apply here.

Take 4.5.13 where you introduce secrecy as an example. There is an expected compiler error and some background to it. The same goes for the previous chapters and for 5.3.4 where running the docker image for the first time comes with some errors.

This flow, for the lack of a better word, didn't apply to 5.3.1 where I couldn't go on because the docker build failed and I was left stuck.

LukeMathWalker commented 11 months ago

I see what you mean—thanks for clarifying!