This post shows how to build a Ubuntu 18.04 Docker image with installed Rust. We can use the image to build Rust applications, but the codes are in the host Windows system. When we compile the codes, we need to specify the local Windows directory as a volume in Docker.
Requirements For Docker Image
We used the following items.
- Ubuntu 18.04 Docker image as the base image
- Rust 1.42.0, which is the latest version as of this writing
- Windows 10, which is our host system
- Docker 2.2.0.4 Community for Windows
- Enable File Sharing and choose the local drives where the codes are
- Cargo project with Cargo.toml
- Windows 10
Build Docker Image With Dockerfile
Okay, we start with a Dockerfile.
1 2 3 4 5 6 7 8 9 10 | FROM ubuntu:18.04 RUN apt-get update && apt-get install -y curl RUN apt-get install build-essential -y RUN mkdir -p /user/turreta-rust-builder/src WORKDIR /user/turreta-rust-builder/src RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y ENV PATH="/root/.cargo/bin:${PATH}" |
Line 1 specifies that we use Ubuntu 18.04 as the base of our Docker image. Next, we update the operating system and install the curl with -y option. Then, we create and directory for our WORKDIR.
Finally, we install Rust. We also need to set the environment PATH to Cargo’s bin directory.
We need to build our Docker image. Go to the Windows Commandline, the run the following command.
1 | docker build -t turreta-rust-builder:1.0 . |
We’ll get something like the following output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | C:\Users\sang018\Desktop\ksg_prj\_m\blog\rust>docker build -t turreta-rust-builder:1.0 . Sending build context to Docker daemon 1.958GB Step 1/6 : FROM ubuntu:18.04 18.04: Pulling from library/ubuntu 5bed26d33875: Pull complete f11b29a9c730: Pull complete 930bda195c84: Pull complete 78bf9a5ad49e: Pull complete Digest: sha256:bec5a2727be7fff3d308193cfde3491f8fba1a2ba392b7546b43a051853a341d Status: Downloaded newer image for ubuntu:18.04 ---> 4e5021d210f6 Step 2/6 : RUN apt-get update && apt-get install -y curl ---> Running in f7a7ab80c514 ... Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1361 kB] Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [11.9 kB] Get:17 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [4245 B] Get:18 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [2496 B] Fetched 17.7 MB in 18s (989 kB/s) Reading package lists... Reading package lists... Building dependency tree... Reading state information... The following additional packages will be installed: ca-certificates krb5-locales libasn1-8-heimdal libcurl4 libgssapi-krb5-2 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libldap-common libnghttp2-14 libpsl5 libroken18-heimdal librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libsqlite3-0 libssl1.1 libwind0-heimdal openssl publicsuffix Suggested packages: krb5-doc krb5-user libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal libsasl2-modules-ldap libsasl2-modules-otp libsasl2-modules-sql The following NEW packages will be installed: ca-certificates curl krb5-locales libasn1-8-heimdal libcurl4 libgssapi-krb5-2 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libldap-common libnghttp2-14 libpsl5 libroken18-heimdal librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libsqlite3-0 libssl1.1 libwind0-heimdal openssl publicsuffix 0 upgraded, 30 newly installed, 0 to remove and 0 not upgraded. Need to get 4835 kB of archives. After this operation, 14.8 MB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libssl1.1 amd64 1.1.1-1ubuntu2.1~18.04.5 [1300 kB] Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 openssl amd64 1.1.1-1ubuntu2.1~18.04.5 [613 kB] ... Get:28 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libcurl4 amd64 7.58.0-2ubuntu3.8 [214 kB] Get:29 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 curl amd64 7.58.0-2ubuntu3.8 [159 kB] Get:30 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libsasl2-modules amd64 2.1.27~101-g0780600+dfsg-3ubuntu2.1 [48.8 kB] debconf: delaying package configuration, since apt-utils is not installed Fetched 4835 kB in 11s (451 kB/s) Selecting previously unselected package libssl1.1:amd64. (Reading database ... 4046 files and directories currently installed.) Preparing to unpack .../00-libssl1.1_1.1.1-1ubuntu2.1~18.04.5_amd64.deb ... Unpacking libssl1.1:amd64 (1.1.1-1ubuntu2.1~18.04.5) ... Selecting previously unselected package openssl. Preparing to unpack .../01-openssl_1.1.1-1ubuntu2.1~18.04.5_amd64.deb ... Unpacking openssl (1.1.1-1ubuntu2.1~18.04.5) ... ... debconf: unable to initialize frontend: Readline debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.) debconf: falling back to frontend: Teletype Setting up libheimbase1-heimdal:amd64 (7.5.0+dfsg-1) ... Setting up openssl (1.1.1-1ubuntu2.1~18.04.5) ... Setting up libsqlite3-0:amd64 (3.22.0-1ubuntu0.3) ... Setting up libkeyutils1:amd64 (1.5.9-9.2ubuntu2) ... ... Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done. Running hooks in /etc/ca-certificates/update.d... done. Removing intermediate container f7a7ab80c514 ---> 11915719f6b4 Step 3/6 : RUN mkdir -p /user/turreta-rust-builder/src ---> Running in 419f324141fa Removing intermediate container 419f324141fa ---> e3e0279fe848 Step 4/6 : WORKDIR /user/turreta-rust-builder/src ---> Running in f0d586db801c Removing intermediate container f0d586db801c ---> 42ca608da96b Step 5/6 : RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y ---> Running in 97531dd8c3d5 info: downloading installer info: profile set to 'default' info: default host triple is x86_64-unknown-linux-gnu info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: latest update on 2020-03-12, rust version 1.42.0 (b8cedc004 2020-03-09) info: downloading component 'cargo' info: downloading component 'clippy' info: downloading component 'rust-docs' info: downloading component 'rust-std' info: downloading component 'rustc' info: downloading component 'rustfmt' info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs' info: installing component 'rust-std' info: installing component 'rustc' info: installing component 'rustfmt' info: default toolchain set to 'stable' stable installed - rustc 1.42.0 (b8cedc004 2020-03-09) Rust is installed now. Great! To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH environment variable. Next time you log in this will be done automatically. To configure your current shell run source $HOME/.cargo/env Removing intermediate container 97531dd8c3d5 ---> 1a106d92350e Step 6/6 : ENV PATH="/root/.cargo/bin:${PATH}" ---> Running in 7df0ac0d7361 Removing intermediate container 7df0ac0d7361 ---> 82a20687cf96 Successfully built 82a20687cf96 Successfully tagged turreta-rust-builder:1.0 SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. |
Verify Rust Installation in Docker
To verify the Rust installation in Ubuntu 18.04, we can check out Cargo’s version and make it compile code. Note – we don’t have any codes when we start up a Docker container.
All commands are on the Windows command line prompt.
First, we run Cargo with –version option.
1 | docker run --rm --name rustc turreta-rust-builder:1.0 cargo --version |
The command produces the following output.
1 | cargo 1.42.0 (86334295e 2020-01-31) |
Next, we make Cargo build codes using the following command.
1 | docker run --rm --name rustc turreta-rust-builder:1.0 cargo build |
The command produces the following output.
1 | error: could not find `Cargo.toml` in `/user/turreta-rust-builder/src` or any parent directory |
The error indicates that the directory is empty. It is okay for now because we don’t have Rust codes yet to compile.
If we have the codes in a local directory, we use this command. It will compile the Rust codes in the Windows host.
1 | docker run --rm --name rustc -v c:\path\in\windows:/user/turreta-rust-builder/src turreta-rust-builder:1.0 cargo build |