5
6

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 3 years have passed since last update.

Dockerにsshdをインストールしてsshログインする

Last updated at Posted at 2020-05-16

目的

Dockerにsshdをインストールする方法に関する備忘録です

少し探したところ、公式サイトに書いていた、、

Dockerize an SSH service

Dockerfileを作成して起動

Dockerfileを作成する

Dockerfile
FROM ubuntu:16.04

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:THEPASSWORDYOUCREATED' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

Dockerfileと同じディレクトリでdocker buildする

$ docker build -t eg_sshd .
...
Successfully built a25d2079f336
Successfully tagged eg_sshd:latest
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
eg_sshd             latest              a25d2079f336        2 minutes ago       205MB

buildに成功したのでdocker runする

$ docker run -d -P --name test_sshd eg_sshd
2c72816dcfccbe6ed82e15635d4a43d77180b564a95de32c553bfe8c5e49fb16
$ docker ps
CONTAINER ID        IMAGE                    COMMAND               CREATED             STATUS              PORTS                   NAMES
2c72816dcfcc        eg_sshd                  "/usr/sbin/sshd -D"   21 seconds ago      Up 20 seconds       0.0.0.0:32768->22/tcp   test_sshd

dockerコンテナの22番ポートがどこにマッピングされているか確認する(今回は32768のようだった)

$ docker port test_sshd 22
0.0.0.0:32768

IPアドレスか、localhostを指定してsshでログインする

$ ssh root@localhost -p 32768
root@localhost's password:(Dockerfileでchpasswdした値、例:"THEPASSWORDYOUCREATED")
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.9.125-linuxkit x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@2c72816dcfcc:~# 

sshでdockerコンテナ内にログインできました。

参考

Dockerize an SSH service
DockerでポータブルなLinux開発環境(GUI付き)を構築する

5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?