As the title says. Rust is my first time. I thought about how to use the container as follows, but since I will not use this container for anything other than learning Rust, I decided to go with the former.
--FROM: rust --How to install rust in FROM: ubuntu etc.
In the former case, the os used by the Rust image is used. Check the os you are using in the Dockerfile of rust. https://github.com/rust-lang/docker-rust
Rust Official Document (The Rust Programming Language Japanese Version): https://doc.rust-jp.rs/book-ja/title-page.html DokerHub(Rust):https://hub.docker.com/_/rust
Official document: https://doc.rust-jp.rs/book-ja/ch01-01-installation.html I will use the Rust image this time, so I skipped this part in this study. Instead, make sure you have the Rust image on docker Hub and make sure the container works as originally planned.
DokerHub(Rust):https://hub.docker.com/_/rust
Scrolling down a bit, I found a sample of Dockerfile and build commands.
Rust Dockerfile sample on Docker Hub
FROM rust:1.31
WORKDIR /usr/src/myapp
COPY . .
RUN cargo install --path .
CMD ["myapp"]
If there is a sample, I changed only the version and wrote it as it is, but I got an error in build, so I decided not to see it.
Cargo at this stage.I don't know what toml is
$ docker build -t my-rust-app .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM rust:latest
---> 8d0d57072949
Step 2/5 : WORKDIR /usr/src/myapp
---> Using cache
---> 6015e37ea342
Step 3/5 : COPY . .
---> 66589dbef629
Step 4/5 : RUN cargo install --path .
---> Running in b3cdc5e8eece
error: `/usr/src/myapp` does not contain a Cargo.toml file. --path must point to a directory containing a Cargo.toml file.
1.2 Hello, World! Official document: https://doc.rust-jp.rs/book-ja/ch01-02-hello-world.html Create a Dockerfile. Some are matched to the sample on Docker Hub.
Dockerfile
FROM rust:latest
WORKDIR /usr/src/projects/hello_world
COPY . .
RUN cd /usr/src/projects/hello_world && \
rustc main.rs
CMD ["./main"]
Create main.rc to copy to the container.
main.rc
fn main() {
//Hello World
println!("Hello, world!");
}
Build and run
$ docker build -t my-rust-app .
$ docker run -it --rm --name my-running-app my-rust-app
Hello, world!
1.3 Hello, Cargo! Official document: https://doc.rust-jp.rs/book-ja/ch01-03-hello-cargo.html Edit the Dockerfile.
Dockerfile
FROM rust:latest
WORKDIR /usr/src/projects
RUN cd /usr/src/projects && \
cargo new hello_cargo --bin && \
cd /usr/src/projects/hello_cargo && \
cargo build
CMD ["/usr/src/projects/hello_cargo/target/debug/hello_cargo"]
When I built it, I was told to specify USER.
$ docker build -t my-rust-app .
(Omitted)
Step 5/7 : RUN cd /usr/src/projects && cargo new hello_cargo --bin
---> Running in 1f2404887764
error: Failed to create package `hello_cargo` at `/usr/src/projects/hello_cargo`
Caused by:
could not determine the current user, please set $USER
The same is true even if you comment out RUN and CMD, enter the container, and execute it. Docker will log in as root if you don't specify a user, but echo $ USER is empty.
$ docker run -it --rm --name my-running-app my-rust-app
root@126e1954fec0:/usr/src/projects# cargo new hello_cargo --bin
error: Failed to create package `hello_cargo` at `/usr/src/projects/hello_cargo`
Caused by:
could not determine the current user, please set $USER
root@126e1954fec0:/usr/src/projects# echo $USER
Modify the Dockerfile to define the environment variable USER.
Dockerfile
FROM rust:latest
WORKDIR /usr/src/projects
ENV USER=root
RUN cd /usr/src/projects && \
cargo new hello_cargo --bin && \
cd /usr/src/projects/hello_cargo && \
cargo build
CMD ["/usr/src/projects/hello_cargo/target/debug/hello_cargo"]
Build and run
$ docker build -t my-rust-app .
$ docker run -it --user root --rm --name my-running-app my-rust-app
Hello, world!
1.1 If you want to eliminate the following error that occurred when building the Docker Hub rust template that appeared in the installation
FROM rust:latest
WORKDIR /usr/src/myapp
COPY . .
##add to###############
ENV USER=root
RUN cargo init .
####################
RUN cargo install --path .
CMD ["myapp"]
Reference: https://kajirikajiri.gitbook.io/gitbook/untitled
I'm studying languages on Docker, but it doesn't have to pollute the environment of my local computer, so it's really nice to be able to recreate it over and over again. In addition, the Rust specifications, which are difficult to notice just by tracing the document as it is, such as USER, are also being advanced on Docker, which has the effect of deepening understanding in unexpected places such as errors.
Actually, I create a container with only the minimum Dockerfile such as the definition of FROM and WORKDIR, enter the container, execute the command according to the document, and proceed while looking at the contents of the created one. If that looks okay, I'm taking steps to update the Dockerfile to make sure I get the same result in CMD. If you just study Rust, you don't need to update the Dockerfile, but I will continue for a while as it will also study the Docker specifications.
Recommended Posts