dockerコンテナ privateIPにssh

softlayerの仮想インスタンスにdockerコンテナを作成し、sshログインをしてみた
brg11271

まずは仮想インスタンスにdockerをインストールして、起動させます。今日も便利なsoftlayer:)(これしか使ったことがない)

1
2
3
4
5
# yum -y install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# wget -P /etc/yum.repos.d http://www.hop5.in/yum/el6/hop5.repo
# yum install xz docker-io -y
# chkconfig docker on
# service docker start

Dockerfileファイルを作成します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# mkdir /root/docker
# cd /root/docker
# touch Dockerfile
# vi Dockerfile
FROM centos:centos6
RUN yum install -y passwd
RUN yum install -y openssh
RUN yum install -y openssh-server
RUN yum install -y openssh-clients
RUN yum install -y sudo

RUN sed -ri /etc/ssh/sshd_config
RUN sed -ri /etc/ssh/sshd_config
RUN /etc/init.d/sshd start
RUN /etc/init.d/sshd stop
RUN echo |chpasswd

RUN useradd docker
RUN echo |chpasswd
RUN echo /etc/sudoers.d/docker

EXPOSE 22
CMD /usr/sbin/sshd -D

先程作ったDockerfileを元にイメージを作成します。

1
#docker build -t centos6/sshd .

dockerコンテナを作成

1
2
3
4
5
6
# docker run -p 80:80 -d centos6/sshd
07846fed6f8beffe379c51f1d138396838bca35b003b23ef53262aacc9d27c11
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07846fed6f8b centos6/sshd:latest "/bin/sh -c '/usr/sb 5 seconds ago Up 5 seconds 22/tcp, 0.0.0.0:80->80/tcp naughty_hoover
7ef17421b386 centos6/sshd:latest "/bin/sh -c '/usr/sb 4 minutes ago Up 3 minutes 0.0.0.0:49153->22/tcp suspicious_bell

コンテナのIPを確認

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
# docker inspect 07846fed6f8b
[{
"Args": [
"-c",
"/usr/sbin/sshd -D"
],
"Config": {
"AttachStderr": false,
"AttachStdin": false,
"AttachStdout": false,
"Cmd": [
"/bin/sh",
"-c",
"/usr/sbin/sshd -D"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"ExposedPorts": {
"22/tcp": {},
"80/tcp": {}
},
"Hostname": "07846fed6f8b",
"Image": "centos6/sshd",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": false,
"PortSpecs": null,
"SecurityOpt": null,
"StdinOnce": false,
"Tty": false,
"User": "",
"Volumes": null,
"WorkingDir": ""
},
"Created": "2014-11-26T09:44:02.591321036Z",
"Driver": "devicemapper",
"ExecDriver": "native-0.2",
"HostConfig": {
"Binds": null,
"CapAdd": null,
"CapDrop": null,
"ContainerIDFile": "",
"Devices": [],
"Dns": null,
"DnsSearch": null,
"ExtraHosts": null,
"Links": null,
"LxcConf": [],
"NetworkMode": "bridge",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
"Privileged": false,
"PublishAllPorts": false,
"RestartPolicy": {
"MaximumRetryCount": 0,
"Name": ""
},
"VolumesFrom": null
},
"HostnamePath": "/var/lib/docker/containers/07846fed6f8beffe379c51f1d138396838bca35b003b23ef53262aacc9d27c11/hostname",
"HostsPath": "/var/lib/docker/containers/07846fed6f8beffe379c51f1d138396838bca35b003b23ef53262aacc9d27c11/hosts",
"Id": "07846fed6f8beffe379c51f1d138396838bca35b003b23ef53262aacc9d27c11",
"Image": "457cf9b2a2dd5bf339e56498bde6ce67a28188abf7c2c323a1596f32a2110099",
"MountLabel": "",
"Name": "/naughty_hoover",
"NetworkSettings": {
"Bridge": "docker0",
"Gateway": "172.17.42.1",
★"IPAddress": "172.17.0.26",★
"IPPrefixLen": 16,
"MacAddress": "02:42:ac:11:00:1a",
"PortMapping": null,
"Ports": {
"22/tcp": null,
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
}
},
"Path": "/bin/sh",
"ProcessLabel": "",
"ResolvConfPath": "/var/lib/docker/containers/07846fed6f8beffe379c51f1d138396838bca35b003b23ef53262aacc9d27c11/resolv.conf",
"State": {
"ExitCode": 0,
"FinishedAt": "0001-01-01T00:00:00Z",
"Paused": false,
"Pid": 12828,
"Restarting": false,
"Running": true,
"StartedAt": "2014-11-26T09:44:02.844181094Z"
},
"Volumes": {},
"VolumesRW": {}
}
]

IPAddressが172.17.0.26だと分かったので、さっそくsshをしてみます。パスワードはDockerfileで書いています。dockerでしたね、確か。

1
2
3
# ssh 172.17.0.26 -l root
root@172.17.0.26's password:
[root@07846fed6f8b ~]#

はいれたはいれた!:)

ここでdockerコンテナの中身を設定します。goを動かしたいのでgo環境を整えます。

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
#yum install python-setuptools
#yum install mercurial
[root@07846fed6f8b ~]# cd;mkdir bin
[root@07846fed6f8b ~]# export GOROOT=$HOME/go
[root@07846fed6f8b ~]# export GOOS=linux
[root@07846fed6f8b ~]# export GOARCH=amd64
[root@07846fed6f8b ~]# export PATH=$PATH:$HOME/bin
[root@07846fed6f8b ~]# source .bashrc
[root@07846fed6f8b ~]# hg clone -r release https://go.googlecode.com/hg/ $GOROOT
requesting all changes
adding changesets
adding manifests
adding file changes
added 19559 changesets with 68116 changes to 8835 files
updating to branch release-branch.go1.3
4167 files updated, 0 files merged, 0 files removed, 0 files unresolved
[root@07846fed6f8b ~]# cd /root/go/src
[root@07846fed6f8b src]# yum intall gcc
[root@07846fed6f8b src]# ./all.bash
--最後に以下のコメント--
ALL TESTS PASSED

---
Installed Go for linux/amd64 in /root/go
Installed commands in /root/go/bin
*** You need to add /root/go/bin to your PATH.
[root@07846fed6f8b src]# export PATH=$PATH:/root/go/bin

↑のようにパスを通しました。

goが使えるようになったのでコンテナを動かせてみます。viの中身はいつもhello worldなので今日はタイマーにしてみました。

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
[root@07846fed6f8b src]# vi time.go
package main

import "time"
import "fmt"

func main() {

timer1 := time.NewTimer(time.Second * 2)

<-timer1.C
fmt.Println("Timer 1 expired")

timer2 := time.NewTimer(time.Second)
go func() {
<-timer2.C
fmt.Println("Timer 2 expired")
}()
stop2 := timer2.Stop()
if stop2 {
fmt.Println("timer 2 stopped")
}
}
[root@07846fed6f8b src]# go run time.go
Timer 1 expired
timer 2 stopped

きちんと2秒カウントしました。

うむ、コンテナが仮想インスタンスのように使えて感激!
どうやらコンテナにはグローバルIPをふれるそうなのでそちらも試してみようっと!それがうまくできたら、webから見られるようになりますよね。(別途設定が必要)

あと未だに謎なのがdockerfileとimageの違いと、dockerをインストールするとき時に出来る5つのコンテナの働き。
クライアント・サーバ型であるのでクライアントと何かなのでしょうがよく分からない・・・
中身を見たり調べたりしないとですね。分からないときちんと使えない。

【参考】
http://qiita.com/comutt/items/1251cc19885947cd6d3d
https://github.com/CentOS/CentOS-Dockerfiles
http://qiita.com/zembutsu/items/3a396dd1e9d16193b0ba

https://github.com/jpetazzo/pipework#docker_integration
http://inokara.hateblo.jp/entry/2013/09/29/090500
http://docs.docker.com/examples/running_ssh_service/

This entry was posted in docker.

コメントを残す