Skip to content

Instantly share code, notes, and snippets.

@masanobuimai
Last active October 15, 2021 00:12
Show Gist options
  • Save masanobuimai/3825d5ef441a843387647d9bba8f0e4b to your computer and use it in GitHub Desktop.
Save masanobuimai/3825d5ef441a843387647d9bba8f0e4b to your computer and use it in GitHub Desktop.
ホストからsshで接続できるDockerコンテナの例

Dockerイメージのビルド

docker build -t sshd .

Dockerコンテナを起動する

docker run -itd -p 22:22 sshd

上記コンテナにsshで接続する(rootのパスワードは password )

> ssh root@localhost
root@localhost's password: password
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.
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@873717cbf7f8:~#
FROM ubuntu:18.04
USER root
RUN apt-get update
RUN apt-get -y install locales && \
localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
ENV TZ JST-9
ENV TERM xterm
RUN apt-get -y install openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:password' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment