技術をかじる猫

適当に気になった技術や言語、思ったこと考えた事など。

Docker(17.12.0) 汚せる nodejs 実行環境を作る

そして、自分の用途を考えると、何より色々試せるサンドボックス環境が欲しかったのだ。
ならば作ろうじゃないか Docker で!

自分がよく作るだろう環境を image 化する

という事で、おもむろに Ubuntu イメージからコンテナを作成する。

XXXXXXX:~ azalea$ docker run --name node-ubuntu -it ubuntu /bin/bash
root@7404156c11f4:/# apt-get update -y
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]                               
Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [58.7 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]                      
   中略                                                                                                              
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [7172 B]                                                                                                                  
Fetched 24.8 MB in 12s (2009 kB/s)                                                                                                                                                                         
Reading package lists... Done

初っ端は更新から。

そしたら、デフォルトパッケージで nodejsnpm をインストールする。
ただしこの時点では node コマンドは使えなくて nodejs コマンドというクソっぷり。

root@7404156c11f4:/# apt-get install nodejs npm -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  binutils build-essential bzip2 ca-certificates cpp cpp-5 dpkg-dev fakeroot file g++ g++-5 gcc gcc-5 gcc-5-base gyp ifupdown iproute2 isc-dhcp-client isc-dhcp-common javascript-common
  libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan2 libatm1 libatomic1 libc-dev-bin libc6-dev libcc1-0 libcilkrts5 libdns-export162 libdpkg-perl libexpat1 libfakeroot
  libffi6 libfile-fcntllock-perl libgcc-5-dev libgdbm3 libgmp10 libgomp1 libicu55 libisc-export160 libisl15 libitm1 libjs-inherits libjs-jquery libjs-node-uuid libjs-underscore liblsan0 libmagic1
  libmnl0 libmpc3 libmpfr4 libmpx0 libperl5.22 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libquadmath0 libsqlite3-0 libssl-dev libssl-doc libssl1.0.0 libstdc++-5-dev libstdc++6 libtsan0
  libubsan0 libuv1 libuv1-dev libxtables11 linux-libc-dev make manpages manpages-dev mime-support netbase node-abbrev node-ansi node-ansi-color-table node-archy node-async node-block-stream
  node-combined-stream node-cookie-jar node-delayed-stream node-forever-agent node-form-data node-fstream node-fstream-ignore node-github-url-from-git node-glob node-graceful-fs node-gyp node-inherits
  node-ini node-json-stringify-safe node-lockfile node-lru-cache node-mime node-minimatch node-mkdirp node-mute-stream node-node-uuid node-nopt node-normalize-package-data node-npmlog node-once
  node-osenv node-qs node-read node-read-package-json node-request node-retry node-rimraf node-semver node-sha node-sigmund node-slide node-tar node-tunnel-agent node-underscore node-which nodejs-dev
  openssl patch perl perl-modules-5.22 python python-minimal python-pkg-resources python2.7 python2.7-minimal rename xz-utils zlib1g-dev
  中略
Setting up npm (3.5.2-0ubuntu4) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21) ...
Processing triggers for ca-certificates (20170717~16.04.1) ...
Updating certificates in /etc/ssl/certs...
148 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.

ここで、npm を使って n コマンドをインストールする。

root@7404156c11f4:/# npm cache clean
root@7404156c11f4:/# npm install n -g
/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
/usr/local/lib
`-- n@2.1.8 

あとは wget なり curl なりをインストールして、n stable で最新安定板の node を入れてしまう。

root@7404156c11f4:/# apt-get install wget -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
  中略
Processing triggers for libc-bin (2.23-0ubuntu10) ...
root@7404156c11f4:/# n stable

     install : node-v9.4.0
       mkdir : /usr/local/n/versions/node/9.4.0
       fetch : https://nodejs.org/dist/v9.4.0/node-v9.4.0-linux-x64.tar.gz
   installed : v9.4.0

root@7404156c11f4:/# ln -sf /usr/local/bin/node /usr/bin/node
root@7404156c11f4:/# node -v
v9.4.0

Ubuntu デフォルトの nodejs とかいらないので削除

root@7404156c11f4:/# apt-get purge -y nodejs npm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  binutils build-essential bzip2 cpp cpp-5 dpkg-dev fakeroot file g++ g++-5 gcc gcc-5 gyp ifupdown iproute2 isc-dhcp-client isc-dhcp-common javascript-common libalgorithm-diff-perl
  中略
Removing nodejs (4.2.6~dfsg-1ubuntu4.1) ...

しっかしデフォルトだと nodejs バージョンが古いものである。
まずはこれで、nodejs 用のコンテナができたわけだ。

後はイメージ化しておくと、コンテナ作成が楽でいい

XXXXXXX:~ azalea$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
7404156c11f4        ubuntu              "/bin/bash"              7 hours ago         Exited (0) 20 seconds ago                       node-ubuntu
d44b8f1d3bc2        wordpress           "docker-entrypoint.s…"   20 hours ago        Exited (137) 20 hours ago                       web
d551c8998080        mysql               "docker-entrypoint.s…"   20 hours ago        Exited (137) 20 hours ago                       db
ac386b7fd1df        hello-world         "/hello"                 3 days ago          Exited (0) 3 days ago                           clever_aryabhata
XXXXXXX:~ azalea$ docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)
XXXXXXX:~ azalea$ docker commit 7404156c11f4 nodejs9-sandbox
sha256:a93c21a71dade1f0579fa9e9d2ef29cf9f02a79599c740d77bd0e143914954cc
XXXXXXX:~ azalea$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nodejs9-sandbox     latest              a93c21a71dad        5 seconds ago       529MB
wordpress           latest              ef4787e1820e        8 days ago          410MB
ubuntu              latest              0458a4468cbc        3 weeks ago         112MB
mysql               latest              f008d8ff927d        4 weeks ago         409MB
nginx               latest              3f8a4339aadd        7 weeks ago         108MB
hello-world         latest              f2a91732366c        2 months ago        1.85kB
XXXXXXX:~ azalea$ 

後は使いたいときに使えるはずだ。
まずはその検証のために元を削除してみる。

XXXXXXX:~ azalea$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
7404156c11f4        ubuntu              "/bin/bash"              7 hours ago         Exited (0) 8 minutes ago                        node-ubuntu
d44b8f1d3bc2        wordpress           "docker-entrypoint.s…"   20 hours ago        Exited (137) 20 hours ago                       web
d551c8998080        mysql               "docker-entrypoint.s…"   21 hours ago        Exited (137) 20 hours ago                       db
ac386b7fd1df        hello-world         "/hello"                 3 days ago          Exited (0) 3 days ago                           clever_aryabhata
XXXXXXX:~ azalea$ docker rm node-ubuntu
node-ubuntu
XXXXXXX:~ azalea$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
d44b8f1d3bc2        wordpress           "docker-entrypoint.s…"   20 hours ago        Exited (137) 20 hours ago                       web
d551c8998080        mysql               "docker-entrypoint.s…"   21 hours ago        Exited (137) 20 hours ago                       db
ac386b7fd1df        hello-world         "/hello"                 3 days ago          Exited (0) 3 days ago                           clever_aryabhata
XXXXXXX:~ azalea$ 

そしたらイメージから起動する。

XXXXXXXX:~ azalea$ docker run -it nodejs9-sandbox
root@6cfb7120e148:/# 

ひゃっはー。
そして関連コンテナを全て docker rm で削除した後

XXXXXXX:~ azalea$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nodejs9-sandbox     latest              a93c21a71dad        About an hour ago   529MB
wordpress           latest              ef4787e1820e        8 days ago          410MB
ubuntu              latest              0458a4468cbc        3 weeks ago         112MB
mysql               latest              f008d8ff927d        4 weeks ago         409MB
nginx               latest              3f8a4339aadd        7 weeks ago         108MB
hello-world         latest              f2a91732366c        2 months ago        1.85kB
XXXXXXX:~ azalea$ docker image rm a93c21a71dad 
Untagged: nodejs9-sandbox:latest
Deleted: sha256:a93c21a71dade1f0579fa9e9d2ef29cf9f02a79599c740d77bd0e143914954cc
Deleted: sha256:ccfc46bb38a596f9d7965ce6aa2f15b905a44227b10c7e6405cde577b24ac586
XXXXXXX:~ azalea$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wordpress           latest              ef4787e1820e        8 days ago          410MB
ubuntu              latest              0458a4468cbc        3 weeks ago         112MB
mysql               latest              f008d8ff927d        4 weeks ago         409MB
nginx               latest              3f8a4339aadd        7 weeks ago         108MB
hello-world         latest              f2a91732366c        2 months ago        1.85kB

docker image rm イメージ名 でイメージ削除までいける事がわかった。