Proceed with Rust official documentation on Docker container (1. Getting started)

Introduction

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

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

1.1 Installation

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!

bonus

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

in conclusion

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

Proceed with Rust official documentation on Docker container (1. Getting started)
Proceed with the official Rust documentation on a Docker container (2. Program a number guessing game)
Getting Started with Docker
[Google Cloud] Getting Started with Docker
Getting Started with Docker with VS Code
Getting Started with GitHub Container Registry instead of Docker Hub
Getting Started with Docker for Mac (Installation)
Update container image with KUSANAGI Runs on Docker
I tried Getting Started with Gradle on Heroku
Getting Started with DBUnit
Getting Started with Ruby
Getting Started with Swift
Getting Started with Doma-Transactions
Getting Started with Doma-Annotation Processing
Getting Started with Java Collection
Get started with DynamoDB with docker
Getting Started with JSP & Servlet
Getting Started with Java Basics
Time is wrong with the application launched on the Docker container
Getting Started with Spring Boot
Getting Started with Ruby Modules
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
I tried deploying a Docker container on Lambda with Serverless Framework
Getting Started with Java_Chapter 5_Practice Exercises 5_4
Run React on a Docker container
Run GUI application on Docker container
WordPress with Docker Compose on CentOS 8
Run PureScript on a Docker container
Getting started with Java lambda expressions
Use Docker CE (official) on CentOS 8
[Linux] Start Apache container with Docker
Build WebRTC Janus with Docker container
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
Starting with installing Docker on EC2 and running Yellowfin in a container
Getting Started with Doma-Criteria API Cheat Sheet
Getting Started with Ruby for Java Engineers
Launch docker container on EC2 (personal memorandum)
Run NordVPN on Docker (Windows) Ubuntu container
Scraping with puppeteer in Nuxt on Docker.
Getting Started with Parameterization Testing in JUnit
Getting Started with Java Starting from 0 Part 1
Getting Started with Ratpack (4)-Routing & Static Content
Getting started with the JVM's GC mechanism
Getting Started with Language Server Protocol with LSP4J
Build an environment with Docker on AWS
Launched Redmine with Docker on Raspberry Pi 3
Run Ubuntu + ROS with Docker on Mac
Getting Started with Creating Resource Bundles with ListResoueceBundle
Monitor the Docker container and SystemD process on the same host with Zabbix on Ubuntu.
A quick note on using jshell with the official Docker image of the JDK
Part2 Part II. How to proceed with Getting Started Spring Boot Reference Guide Note ①