サンプル集  >  other  >  Dockerfile
Dockerfile
2025/05/24

Dockerfileでイメージを作成します。

◆環境
OS Windows 10 Home 22H2 64bit
Docker 27.4.0 build bde2b89

Dockerfileにrustの環境を作成するときに使用したコマンドを記載します。

Dockerfile
1: 
2: 
3: 
4: 
5: 

6: 
7: 
FROM ubuntu:20.04

RUN apt update
RUN apt install -y curl
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | 
sh
RUN . "$HOME/.cargo/env"
RUN source "$HOME/.cargo/env"

Dockerfileのあるフォルダに移動しdocker buildを実行します。

> cd .\P304\src
docker build -t my-rust:Dockerfile .
[+] Building 35.5s (7/9)                                             
                              docker:desktop-linux
 => [internal] load build definition from Dockerfile                 
                                              0.1s
 => => transferring dockerfile: 226B                                 
                                              0.0s
 => [internal] load metadata for docker.io/library/ubuntu:20.04      
                                              0.0s
 => [internal] load .dockerignore                                    
                                              0.0s
 => => transferring context: 2B                                      
                                              0.0s
 => [1/6] FROM docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4e
ad070431d29b576a530d3166df73ec44affc1cd27555  1.6s
 => => resolve docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4e
ad070431d29b576a530d3166df73ec44affc1cd27555  1.5s
 => [2/6] RUN apt update                                             
                                             18.9s
 => [3/6] RUN apt install -y curl                                    
                                              9.9s
 => ERROR [4/6] RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.r
ustup.rs | sh                                 4.8s
------
 > [4/6] RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.r
s | sh:
0.826 info: downloading installer
4.733 error: Unable to run interactively. Run with -y to accept defau
lts, --help for additional options
------
Dockerfile:5
--------------------
   3 |     RUN apt update
   4 |     RUN apt install -y curl
   5 | >>> RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup
.rs | sh
   6 |     RUN . "$HOME/.cargo/env"
   7 |     RUN source "$HOME/.cargo/env"
--------------------
ERROR: failed to solve: process "/bin/sh -c curl --proto '=https' --t
lsv1.2 -sSf https://sh.rustup.rs | sh" did not complete successf
ully: exit code: 1

View build details: docker-desktop://dashboard/build/desktop-linux/de
sktop-linux/v5kj9vc3ps7gejc1b3isotna6

Dockerfileの5行目でエラーになったようです。

5行目を修正します。

Dockerfile
1: 
2: 
3: 
4: 
5: 
6: 
FROM ubuntu:20.04

RUN apt update
RUN apt install -y curl
RUN curl https://sh.rustup.rs- sSf | sh
ENV PATH="/root/.cargo/bin:$PATH"

Docker buildを実行します。

> docker build -t my-rust:Dockerfile .
[+] Building 0.2s (8/8) FINISHED                                     
                              docker:desktop-linux
 => [internal] load build definition from Dockerfile                 
                                              0.0s
 => => transferring dockerfile: 177B                                 
                                              0.0s
 => [internal] load metadata for docker.io/library/ubuntu:20.04      
                                              0.0s
 => [internal] load .dockerignore                                    
                                              0.0s
 => => transferring context: 2B                                      
                                              0.0s
 => [1/4] FROM docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4e
ad070431d29b576a530d3166df73ec44affc1cd27555  0.0s
 => => resolve docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4e
ad070431d29b576a530d3166df73ec44affc1cd27555  0.0s
 => CACHED [2/4] RUN apt update                                      
                                              0.0s
 => CACHED [3/4] RUN apt install -y curl                             
                                              0.0s
 => CACHED [4/4] RUN curl https://sh.rustup.rs- sSf | sh             
                                              0.0s
 => exporting to image                                               
                                              0.1s
 => => exporting layers                                              
                                              0.0s
 => => exporting manifest sha256:a32d7a379ab98cacdf8c808ed2fda326f027
e30017cf6866e09fa2e98abd7da3                  0.0s
 => => exporting config sha256:911cc53fd500bedf1b51f74b192aeb8badadc3
fb37a34d480cd3b01f43fec9e2                    0.0s
 => => exporting attestation manifest sha256:9a2bcd05122909769ecfca16
a31784306fad1cd6b55affccb5f3b3716129172a      0.0s
 => => exporting manifest list sha256:2e76d59c25aa4262f4b0b9c71ab3de5
3e0d55482c8c17e1548151dbeea69e405             0.0s
 => => naming to docker.io/library/my-rust:Dockerfile                
                                              0.0s
 => => unpacking to docker.io/library/my-rust:Dockerfile             
                                              0.0s

View build details: docker-desktop://dashboard/build/desktop-linux/de
sktop-linux/kkmr88idcnq18jbpzfj426k7q

エラーは出ていないようです。

コンテナに接続してみます。

docker run -it my-rust:Dockerfile bash

rustのバージョンを確認してみます。

root@31d2e5f13c17:/# cargo --version
bash: cargo: command not found

コマンドがエラーになりました。$HOME配下を確認します。

root@31d2e5f13c17:/# cd $HOME
root@31d2e5f13c17:~# ls

$HOME配下には何もありませんでした。$PATHを確認します。

root@31d2e5f13c17:~# echo $PATH
/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/s
bin:/bin

$PATHには/root/.cargo/binが追加されていました。

Dockerfileを確認したところ5行目の-sSfの-の一がおかしかったので修正します。

Dockerfile
1: 
2: 
3: 
4: 
5: 
6: 
FROM ubuntu:20.04

RUN apt update
RUN apt install -y curl
RUN curl https://sh.rustup.rs -sSf | sh
ENV PATH="/root/.cargo/bin:$PATH"

Docker buildを実行します。

docker build -t my-rust:Dockerfile .
[+] Building 6.2s (7/7) FINISHED                                     
                              docker:desktop-linux
 => [internal] load build definition from Dockerfile                 
                                              0.0s
 => => transferring dockerfile: 177B                                 
                                              0.0s
 => [internal] load metadata for docker.io/library/ubuntu:20.04      
                                              0.0s
 => [internal] load .dockerignore                                    
                                              0.0s
 => => transferring context: 2B                                      
                                              0.0s
 => [1/4] FROM docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4e
ad070431d29b576a530d3166df73ec44affc1cd27555  0.0s
 => => resolve docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4e
ad070431d29b576a530d3166df73ec44affc1cd27555  0.0s
 => CACHED [2/4] RUN apt update                                      
                                              0.0s
 => CACHED [3/4] RUN apt install -y curl                             
                                              0.0s
 => ERROR [4/4] RUN curl https://sh.rustup.rs -sSf | sh              
                                              6.3s
------
 > [4/4] RUN curl https://sh.rustup.rs -sSf | sh:
1.232 info: downloading installer
6.294 error: Unable to run interactively. Run with -y to accept defau
lts, --help for additional options
------
Dockerfile:5
--------------------
   3 |     RUN apt update
   4 |     RUN apt install -y curl
   5 | >>> RUN curl https://sh.rustup.rs -sSf | sh
   6 |     ENV PATH="/root/.cargo/bin:$PATH"
   7 |
--------------------
ERROR: failed to solve: process "/bin/sh -c curl https://sh.rustup.rs
 -sSf | sh" did not complete successfully: exit code: 1

View build details: docker-desktop://dashboard/build/desktop-linux/de
sktop-linux/jvoe3eqe3a84v4bp4goasm3wm

Dockerfileを修正します。

Dockerfile
1: 
2: 
3: 
4: 
5: 
6: 
FROM ubuntu:20.04

RUN apt update
RUN apt install -y curl
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
ENV PATH="/root/.cargo/bin:$PATH"

Docker buildを実行します。

docker build -t my-rust:Dockerfile .
[+] Building 63.7s (8/8) FINISHED                                    
                              docker:desktop-linux
 => [internal] load build definition from Dockerfile                 
                                              0.0s
 => => transferring dockerfile: 186B                                 
                                              0.0s
 => [internal] load metadata for docker.io/library/ubuntu:20.04      
                                              0.0s
 => [internal] load .dockerignore                                    
                                              0.0s
 => => transferring context: 2B                                      
                                              0.0s
 => [1/4] FROM docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4e
ad070431d29b576a530d3166df73ec44affc1cd27555  0.0s
 => => resolve docker.io/library/ubuntu:20.04@sha256:8e5c4f0285ecbb4e
ad070431d29b576a530d3166df73ec44affc1cd27555  0.0s
 => CACHED [2/4] RUN apt update                                      
                                              0.0s
 => CACHED [3/4] RUN apt install -y curl                             
                                              0.0s
 => [4/4] RUN curl https://sh.rustup.rs -sSf | sh -s -- -y           
                                             27.5s
 => exporting to image                                               
                                             36.0s
 => => exporting layers                                              
                                             27.6s
 => => exporting manifest sha256:d21c6c193a9771f668fd25ce6f0ca38b3128
f02731bf65699cee1b17eaa0ca02                  0.0s
 => => exporting config sha256:a77f8e8bb04e06412d66553cd345d09f8ceee5
3cfb69ef5e1b59f6a035315694                    0.0s
 => => exporting attestation manifest sha256:d877e14452c41ade860d25c0
3568904a48d700e6d8852e307d121113446c150d      0.0s
 => => exporting manifest list sha256:5a4b47d430bb9a5136e34032f8567ea
65d5b4ae30d68b9940a7a975c42892ac8             0.0s
 => => naming to docker.io/library/my-rust:Dockerfile                
                                              0.0s
 => => unpacking to docker.io/library/my-rust:Dockerfile             
                                              8.3s

View build details: docker-desktop://dashboard/build/desktop-linux/de
sktop-linux/w7pnx0wby6873b6i0gxkqxt4g

エラーは出ていないようです。イメージを確認します。

docker images
REPOSITORY   TAG          IMAGE ID       CREATED          SIZE
my-rust      Dockerfile   5a4b47d430bb   43 minutes ago   1.91GB

コンテナに接続しrustのバージョンを確認してみます。

docker run -it my-rust:Dockerfile bash
root@c94b6d812ad7:/# cargo --version
cargo 1.87.0 (99624be96 2025-05-06)

うまく構築できたようです。

▲ PageTop  ■ Home


Copyright (C) 2025 ymlib.com