Proceed with the official Rust documentation on a Docker container (2. Program a number guessing game)

Introduction

As the title says. Last time: Proceed with Rust official document on Docker container (1. Getting started)

Referenced

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

2. Program the number guessing game

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!

in conclusion

If you just want to create a learning environment for Rust, you can use mount.

Recommended Posts

Proceed with the official Rust documentation on a Docker container (2. Program a number guessing game)
Proceed with Rust official documentation on Docker container (1. Getting started)
A quick note on using jshell with the official Docker image of the JDK
[Docker] Delete only the volume associated with a specific container
Time is wrong with the application launched on the Docker container
Run React on a Docker container
Run PureScript on a Docker container
I tried deploying a Docker container on Lambda with Serverless Framework
Access MySQL on a Docker container from a local (host OS) Java program
Starting with installing Docker on EC2 and running Yellowfin in a container
Monitor the Docker container and SystemD process on the same host with Zabbix on Ubuntu.
Come out with a suffix on the method
Come out with a suffix on the method 2
Update container image with KUSANAGI Runs on Docker
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC
I tried running Ansible on a Docker container
Create a Java (Gradle) project with VS Code and develop it on a Docker container
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
[Docker] How to see the contents of Volumes. Start a container with root privileges.
Create a Java (Maven) project with VS Code and develop it on a Docker container
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
How to reduce the load on the program even a little when combining characters with JAVA
Workspace setting location when connecting remotely with VS Code and working on a Docker container