As the title says. Last time: Proceed with Rust official document on Docker container (1. Getting started)
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/ch02-00-guessing-game-tutorial.html
First of all, check the whole thing. Since it seems that the following files need to be modified, we will take the method of copying the locally created one.
Create main.rc and Cargo.toml to copy to the container. In addition, the folder structure of what is created locally.
guessing_game
- src/main.rc
- Cargo.toml
guessing_game/src/main.rc
extern crate rand;
use std::io;
use std::cmp::Ordering;
use rand::Rng;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1, 101);
loop {
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
guessing_game/Cargo.toml
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["root"]
edition = "2018"
[dependencies]
rand = "0.3.14"
Create a Dockerfile. Some are matched to the sample on Docker Hub. I'm using cargo init because I can't use cargo new in an existing directory.
Dockerfile
FROM rust:latest
WORKDIR /usr/src/projects
COPY . .
# 2.Programming a number guessing game
RUN cargo init guessing_game --bin && \
cd /usr/src/projects/guessing_game && \
cargo build
WORKDIR /usr/src/projects/guessing_game
CMD ["cargo", "run"]
When I built it, I got a cargo error
$ docker build -t my-rust-app .
(Omitted)
---> Running in 13aa45df5d36
error: `cargo init` cannot be run on existing Cargo packages
The command '/bin/sh -c cargo init guessing_game --bin && cd /usr/src/projects/guessing_game && cargo build' returned a non-zero code: 101
Apparently cargo init can use existing directories and sources, but Cargo.toml doesn't seem to work. Be quiet, carry new, and then overwrite with COPY.
Modified Dockerfile
FROM rust:latest
WORKDIR /usr/src/projects
# 2.Programming a number guessing game
RUN cargo new guessing_game --bin
COPY . .
RUN cd /usr/src/projects/guessing_game && \
cargo build
WORKDIR /usr/src/projects/guessing_game
CMD ["cargo", "run"]
Build and confirm that the program works without problems
$ docker build -t my-rust-app .
$ docker run -it --rm --name my-running-app my-rust-app
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/guessing_game`
Guess the number!
Please input your guess.
0
You guessed: 0
Too small!
Please input your guess.
100
You guessed: 100
Too big!
Please input your guess.
97
You guessed: 97
You win!
If you just want to create a learning environment for Rust, you can use mount.
Recommended Posts