実際のところ
https://hub.docker.com/_/alpine/:tilte=alpine on Docker hub
Docker公式が推してる軽量Linux*1、alpineのイメージを導入
$ docker pull alpine
次にDockerイメージをビルドするための作業環境を用意
$ mkdir dockerbuild $ cd dockerbuild $ touch Dockerfile
とりあえず何かやらせる
最初のDockerfileという事で、何か端末に表示するだけの簡単なのを。
Dockerfileの中身はこんな風になる。
FROM alpine:3.14 CMD printf "%10d" 1
作業環境にいる状態で、これをfoobarというタグ名でビルド
$ docker.exe build -t foobar . [+] Building 1.6s (5/5) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 75B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/alpine:3.14 1.5s => CACHED [1/1] FROM docker.io/library/alpine:3.14@sha256:06b5d462c92fc39303e6363c65e074559f8d6b1363250027ed5053 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:ed515db3c32298c3908eb037fe034b8f80bbf3fba32665ec60f169ed561a2f91 0.0s => => naming to docker.io/library/foobar 0.0s Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
runでコンテナを作って実行。
"-it"はSTDINの有効化と、ttyの有効化。
$ docker run --name great_goat -it foobar 0000000001
対話型シェルを立ち上げる
先ほどの例だと実行するなり速攻でクローズするので、せめてシェルを立ち上げて何かできるようにする。
今度のDockerfileはENTRYPOINTというのを設定。
FROM alpine:3.14 ENTRYPOINT ["/bin/sh"]
$ docker.exe build -t fizzbuzz . [+] Building 0.9s (5/5) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 77B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/alpine:3.14 0.8s => CACHED [1/1] FROM docker.io/library/alpine:3.14@sha256:06b5d462c92fc39303e6363c65e074559f8d6b1363250027ed5053 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:cb0566ca332cb4b321c6df0d459758c515736e5212d73971ee7a54b37fc632e0 0.0s => => naming to docker.io/library/fizzbuzz 0.0s Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
対話式でrunすると……ちゃんとshが実行される
$ docker run --name poor_pig -it fizzbuzz / #
さらに、イメージが実行状態であれば、別の端末で以下の様にやってやる事で新しいshを実行可能。
$docker exec -it poor_pig sh
参考もと
bash - Dockerfile CMD instruction will exit the container just after running it - Stack Overflow
How To Use docker exec to Run Commands in a Docker Container | DigitalOcean
How to open multiple terminals in docker? - Stack Overflow
Dockerfile reference | Docker Documentation
自分でイメージを構築 — Docker-docs-ja 1.13.RC ドキュメント
*1:なんと5Mbyte