2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dockerイメージ導入(ubuntu)〜ssh接続(自動サービス起動)まで

Last updated at Posted at 2022-07-23

Dockerイメージ(ubuntu)の導入

$ sudo docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
405f018f9d1d: Pull complete 
Digest: sha256:b6b83d3c331794420340093eb706a6f152d9c1fa51b262d9bf34594887c2c7ac
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

$ sudo docker image ls
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
httpd          latest    444f7df01ce9   10 days ago     145MB
ubuntu         latest    27941809078c   6 weeks ago     77.8MB

ubuntuコンテナのssh接続セットアップ

sshdサービス導入

#ubu1コンテナを作成して、シェルに入る
$ sudo docker container run -it --name "ubu1" ubuntu /bin/bash
root@91e0faf978c2:/#
root@91e0faf978c2:/# apt-get update
(cut)
Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [5805 B]
Fetched 22.0 MB in 5s (4489 kB/s)                          
Reading package lists... Done

#sshを受けるopenssh-serverサービスをインストール
$ apt-get install -y openssh-server
(cut)
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.

#ssh接続用ユーザ作成
root@91e0faf978c2:/# useradd sshcon
root@91e0faf978c2:/# passwd sshcon
New password: 
Retype new password: 
passwd: password updated successfully

#sshサービス起動(systemcltはサポートされていないらしい2022/7/23)
root@91e0faf978c2:/# /etc/init.d/ssh start 
 * Starting OpenBSD Secure Shell server sshd                                                                                    [ OK ] 
root@91e0faf978c2:/# /etc/init.d/ssh status
 * sshd is running

#ssh接続ユーザー用のホームディレクトリの追加
root@91e0faf978c2:/# id -a
uid=0(root) gid=0(root) groups=0(root)
root@91e0faf978c2:/# mkdir /home/sshcon
root@91e0faf978c2:/# 
root@91e0faf978c2:/# ll /home/sshcon/
total 8
drwxr-xr-x 2 root root 4096 Jul 23 00:57 ./
drwxr-xr-x 1 root root 4096 Jul 23 00:57 ../
root@91e0faf978c2:/# chown sshcon /home/sshcon/
root@91e0faf978c2:/# ll /home/sshcon/
total 8
drwxr-xr-x 2 sshcon root 4096 Jul 23 00:57 ./
drwxr-xr-x 1 root   root 4096 Jul 23 00:57 ../

sshdサービス自動起動設定

#サービス起動用のシェルを作成
root@91e0faf978c2:~# cat /root/init.sh 
#!/bin/bash
/etc/init.d/ssh start

#実行権限の付与
root@91e0faf978c2:~# chmod 777 init.sh 
root@91e0faf978c2:~# ll
total 32
drwx------ 1 root root 4096 Jul 23 01:36 ./
drwxr-xr-x 1 root root 4096 Jul 23 01:31 ../
-rw------- 1 root root  899 Jul 23 01:40 .bash_history
-rw-r--r-- 1 root root 3106 Oct 15  2021 .bashrc
-rw-r--r-- 1 root root  161 Jul  9  2019 .profile
-rw------- 1 root root 5857 Jul 23 01:36 .viminfo
-rwxrwxrwx 1 root root   34 Jul 23 01:36 init.sh*
root@91e0faf978c2:~# exit
exit

#コンテナの起動
$ docker container start ubu1
ubu1

#コンテナ内でサービスを起動させるシェルを実行
$ docker container exec -it ubu1 /root/init.sh
 * Starting OpenBSD Secure Shell server sshd                                                                                    [ OK ] 

#実行中のコンテナ確認
$ docker container ls
CONTAINER ID   IMAGE     COMMAND       CREATED        STATUS          PORTS     NAMES
91e0faf978c2   ubuntu    "/bin/bash"   13 hours ago   Up 16 seconds             ubu1

ssh接続確認

$ ssh sshcon@172.17.0.2    ←設定したユーザを指定
sshcon@172.17.0.2's password:    ←接続ユーザのパスワードを入力
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-41-generic x86_64)
(cut)
To restore this content, you can run the 'unminimize' command.
Last login: Sat Jul 23 00:58:55 2022 from 172.17.0.1
$ 

おまけ1.IPコマンドがなかったのでインストール

root@91e0faf978c2:/# apt-get install -y iproute2
(cut)
debconf: falling back to frontend: Teletype
Processing triggers for libc-bin (2.35-0ubuntu3) ...

root@91e0faf978c2:/# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

おまけ2.viコマンドがなかったのでインストール

root@91e0faf978c2:/# apt-get install -y vim
(cut)
Processing triggers for libc-bin (2.35-0ubuntu3) ...

root@91e0faf978c2:/# vi --version
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Apr 18 2022 19:26:30)
(cut)

おまけ3.Dockerコマンド実行時の sudo 付与からの脱却

$ sudo ls -al /var/run/docker.sock 
srw-rw---- 1 root root 0  7月 23 08:59 /var/run/docker.sock

#アクセス権の付与
$ sudo chmod 666 /var/run/docker.sock

$ docker container ls  ←sudoがなくても実行可能になる
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?