github.com/feiyang21687/docker@v1.5.0/docs/sources/examples/running_ssh_service.md (about)

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