LoginSignup
10
15

More than 5 years have passed since last update.

docker imageに一時的に修正を入れて使う

Posted at

このtipについて

既存のdocker imageをベースにdocker -i -t [image] docker commitして、ワンステップずつ確認しながら作業をしていく方法です。

「docker imageを他のプログラムから呼び出す」などの使い方をするときに、docker imageのlatestを振ってあげればプログラムは書きなおさなくていいよね!というお手軽な考え方に基づいています。

Dockerを使っている/使いこなしている人には当たり前のtipかもしれません。

しかし、構築されているDockerのimageを更新せずにその上でアプリケーションを使っている人も多いのではないでしょうか?あと、Dockerfile構築するときに毎回docker buildするのめんどうだなー、と思っている人にも(まあそれはないか…)。とりあえずDockerを使ってみる、みたいな人にもいいかもしれません。

このtipの基本的な考え方

ベースとなるdocker imageをpullしてきたあとの使い方には差があると思います。ただ、どんな開発・構築でも1回めのtrialは必ずあります。そのときに、いかにコマンドラインやプログラム中の定数を変えないか!を意識しています。

流れとしては、以下の図のようになります。

docker_—_bash_—_120×53.png

docker imageに一時的に修正を入れて使う

ここがこのドキュメントのメインの部分です。

もととなるdocker imageは予めdocker pullしておいて下さい。今回は自分で作ったrvmが入っているものを使っていますが、基本的にはdocker pull ubuntu:14.04とかするといいと思います。

imageにタグをつける

docker imageを呼び出すときに、外から持ってきたままの名前だと、最新に更新したりいざというときにpushできなかったりといろんな障害があります。気にしない、というのであれば、そのままでもいいのですが、なんとなく気持ちもよくないので、別名を付けて、これをずっと今後使いまわします。

例えば、シェルスクリプトで

echo.sh
#!/bin/sh

docker run nayuta/working-image echo "What's up?"

というスクリプトを作った時に、このイメージ名を変えるのがめんどい、という前提とすると、まずはベースとなるnayuta/working-imageをtagによって作って、ずっと使いまわす、ことにします。

まずはイメージ確認からタグ付けまで。

Last login: Mon Aug 18 14:36:33 on ttys000
nayuta-mbp:docker nayuta$ docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu                               14.04               c4ff7513909d        6 days ago          225.4 MB
nayuta/ubuntu14.04-rvm-full-locale   latest              0da8ad572f94        12 days ago         1.931 GB
<none>                               <none>              9781fdfaa4d2        12 days ago         225.7 MB
<none>                               <none>              141d9dfe7d8d        12 days ago         225.7 MB
<none>                               <none>              d843a905b3fc        12 days ago         225.7 MB
nayuta-mbp:docker nayuta$ docker tag nayuta/ubuntu14.04-rvm-full-locale nayuta/working-image:1437
nayuta-mbp:docker nayuta$ docker tag nayuta/working-image:1437 nayuta/working-image:latest

人によって好みは分かれると思いますし、一手間増えるので、一度1437というtagを作らないで、直接latestを作ってしまってもいいでしょう。その場合は docker tag nayuta/ubuntu14.04-rvm-full-locale nayuta/working:latestになります。

ちなみに1437は14:37で作成した時間をタグにしています。通常運用だと日時(例えば20140108134500とか)をタグにすることが多いですが、今回は一時作業なので簡単に打てることを重視しています。

一時変更を入れる

普通にdocker runコマンドで変更をします。今回はapt-getでwgetを入れています。

nayuta-mbp:docker nayuta$ docker run nayuta/working-image apt-get install -y wget
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  wget
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 270 kB of archives.
After this operation, 651 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main wget amd64 1.15-1ubuntu1 [270 kB]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin: 
Fetched 270 kB in 2s (133 kB/s)
Selecting previously unselected package wget.
(Reading database ... 20780 files and directories currently installed.)
Preparing to unpack .../wget_1.15-1ubuntu1_amd64.deb ...
Unpacking wget (1.15-1ubuntu1) ...
Setting up wget (1.15-1ubuntu1) ...

変更されたコンテナのidを確認する

変更されたコンテナのidを確認するためには、docker ps -lしてください。オプションなしのdocker psでは最後に実行されたコンテナであっても終了したコンテナのため表示されません。

nayuta-mbp:docker nayuta$ docker ps -l
CONTAINER ID        IMAGE                                       COMMAND                CREATED             STATUS                     PORTS               NAMES
f75e04d7dbdb        nayuta/ubuntu14.04-rvm-full-locale:latest   apt-get install -y w   10 days ago         Exited (0) 4 seconds ago                       jolly_newton        

imageとしてcommitする

ここが今回の山場です。tagをつけるイメージでcontainerのimageをcommitします。containerのストレージをimageに書き出す、という感じでしょうか?

nayuta-mbp:docker nayuta$ docker commit f75e04d7dbdb nayuta/working-image:1438
dff1a15db2036c715019ba672c9f50c489da909718c7c1eb5f37fd86714a7564
nayuta-mbp:docker nayuta$ docker tag nayuta/working-image:1438 nayuta/working-image:latest

イメージを確認

一応docker imagesしてちゃんとできているかどうかを確認します。

nayuta-mbp:docker nayuta$ docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu                               14.04               c4ff7513909d        6 days ago          225.4 MB
nayuta/working-image                 1438                dff1a15db203        10 days ago         1.933 GB
nayuta/working-image                 latest              dff1a15db203        10 days ago         1.933 GB
nayuta/ubuntu14.04-rvm-full-locale   latest              0da8ad572f94        12 days ago         1.931 GB
nayuta/working-image                 1437                0da8ad572f94        12 days ago         1.931 GB
<none>                               <none>              9781fdfaa4d2        12 days ago         225.7 MB
<none>                               <none>              141d9dfe7d8d        12 days ago         225.7 MB
<none>                               <none>              d843a905b3fc        12 days ago         225.7 MB
nayuta-mbp:docker nayuta$ 

終わりに

今回は一歩一歩回り道をしないでdocker imageに一時的な修正を入れて、ということをやってみました。この状態でdocker pushすることもできますし、修正の手順確認として使って、あとはDockerfileに書き出す、ということもできます。楽しみ方は人それぞれ、といったところですね。

おまけ

今回は一歩一歩、ということで、回り道をしてサクッとやることもできます。commandだけ書いておきますね。多分こちらのほうが一般的かも。

nayuta@localhost ~$ docker run ubuntu:14.04 apt-get install -y wget
nayuta@localhost ~$ docker commit $(docker ps -l -q) nayuta/working-image

こんだけ長々書いといてたった2行かよ!という人もいるかもしれませんが、まあ、経過を知るのも大事ということで…。

10
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
15