Building an image

We have prepared the Dockerfile to build an image that first builds all the dependencies for our microservice and then builds all the source code. To start this process, you have to use the Docker build command:

docker build -t users-microservice:latest 

When you run this command, you will see how Docker prepares files to build an image and builds all the dependencies, but only for the empty crate without the sources of a microservice:

Sending build context to Docker daemon  13.82kB
Step 1/12 : FROM rust:nightly
---> 91e52fb2cea5
Step 2/12 : RUN USER=root cargo new --bin users-microservice
---> Running in 3ff6b18a9c72
Created binary (application) `users-microservice` package
Removing intermediate container 3ff6b18a9c72
---> 85f700c4a567
Step 3/12 : WORKDIR /users-microservice
---> Running in eff894de0a40
Removing intermediate container eff894de0a40
---> 66366486b1e2
Step 4/12 : COPY ./Cargo.toml ./Cargo.toml
---> 8864ae055d16
Step 5/12 : RUN cargo build
---> Running in 1f1150ae4661
Updating crates.io index
Downloading crates ...
Compiling crates ...
Compiling users-microservice v0.1.0 (/users-microservice)
Finished dev [unoptimized + debuginfo] target(s) in 2m 37s
Removing intermediate container 1f1150ae4661
---> 7868ea6bf9b3

Our image needs 12 steps in total to build the microservice. As you can see, the building of dependencies takes two and a half minutes. It's not fast. But we don't need to repeat this step till Cargo.toml has changed. The next steps copy the source code of the microservices into a container and build them with prebuit dependencies:

Step 6/12 : RUN rm src/*.rs
---> Running in 5b7d9a1f96cf
Removing intermediate container 5b7d9a1f96cf
---> b03e7d0b23cc
Step 7/12 : COPY ./src ./src
---> 2212e3db5223
Step 8/12 : COPY ./diesel.toml ./diesel.toml
---> 5d4c59d31614
Step 9/12 : RUN rm ./target/debug/deps/users_microservice*
---> Running in 6bc9df93ebc1
Removing intermediate container 6bc9df93ebc1
---> c2e3d67d3bf8
Step 10/12 : RUN cargo build
---> Running in b985b6c793d1
Compiling users-microservice v0.1.0 (/users-microservice)
Finished dev [unoptimized + debuginfo] target(s) in 4.98s
Removing intermediate container b985b6c793d1
---> 553156f97943
Step 11/12 : CMD ["./target/debug/users-microservice"]
---> Running in c36ff8e44db3
Removing intermediate container c36ff8e44db3
---> 56e7eb1144aa
Step 12/12 : EXPOSE 8000
---> Running in 5e76a47a0ded
Removing intermediate container 5e76a47a0ded
---> 4b6fc8aa6f1b
Successfully built 4b6fc8aa6f1b
Successfully tagged users-microservice:latest

As you can see in the output, building the microservice takes just 5 seconds. It's fast enough and you can rebuild it as many times as you want. Since the image has been built, we can start a container with our microservice.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.34.197