github.com/circular-dark/docker@v1.7.0/docs/examples/running_ssh_service.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Dockerizing an SSH service"
     4  description = "Installing and running an SSHd service on Docker"
     5  keywords = ["docker, example, package installation,  networking"]
     6  [menu.main]
     7  parent = "smn_apps_servs"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Dockerizing an SSH daemon service
    12  
    13  ## Build an `eg_sshd` image
    14  
    15  The following `Dockerfile` sets up an SSHd service in a container that you
    16  can use to connect to and inspect other container's volumes, or to get
    17  quick access to a test container.
    18  
    19      # sshd
    20      #
    21      # VERSION               0.0.2
    22  
    23      FROM ubuntu:14.04
    24      MAINTAINER Sven Dowideit <SvenDowideit@docker.com>
    25  
    26      RUN apt-get update && apt-get install -y openssh-server
    27      RUN mkdir /var/run/sshd
    28      RUN echo 'root:screencast' | chpasswd
    29      RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
    30  
    31      # SSH login fix. Otherwise user is kicked off after login
    32      RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
    33  
    34      ENV NOTVISIBLE "in users profile"
    35      RUN echo "export VISIBLE=now" >> /etc/profile
    36  
    37      EXPOSE 22
    38      CMD ["/usr/sbin/sshd", "-D"]
    39  
    40  Build the image using:
    41  
    42      $ docker build -t eg_sshd .
    43  
    44  ## Run a `test_sshd` container
    45  
    46  Then run it. You can then use `docker port` to find out what host port
    47  the container's port 22 is mapped to:
    48  
    49      $ docker run -d -P --name test_sshd eg_sshd
    50      $ docker port test_sshd 22
    51      0.0.0.0:49154
    52  
    53  And now you can ssh as `root` on the container's IP address (you can find it
    54  with `docker inspect`) or on port `49154` of the Docker daemon's host IP address
    55  (`ip address` or `ifconfig` can tell you that) or `localhost` if on the
    56  Docker daemon host:
    57  
    58      $ ssh root@192.168.1.2 -p 49154
    59      # The password is ``screencast``.
    60      $$
    61  
    62  ## Environment variables
    63  
    64  Using the `sshd` daemon to spawn shells makes it complicated to pass environment
    65  variables to the user's shell via the normal Docker mechanisms, as `sshd` scrubs
    66  the environment before it starts the shell.
    67  
    68  If you're setting values in the `Dockerfile` using `ENV`, you'll need to push them
    69  to a shell initialization file like the `/etc/profile` example in the `Dockerfile`
    70  above.
    71  
    72  If you need to pass`docker run -e ENV=value` values, you will need to write a
    73  short script to do the same before you start `sshd -D` and then replace the
    74  `CMD` with that script.
    75  
    76  ## Clean up
    77  
    78  Finally, clean up after your test by stopping and removing the
    79  container, and then removing the image.
    80  
    81      $ docker stop test_sshd
    82      $ docker rm test_sshd
    83      $ docker rmi eg_sshd
    84