Not able to connect to docker containers using SSH [closed]

I am new to Docker, I built the container from a Dockerfile, and I can see the container creating and running, but I am not able to connect to that container using ssh.

Here is my Dockerfile:

  • Docker port binding error
  • Using docker-compose with CI - how to deal with exit codes and daemonized linked containers?
  • mounting docker-machine VM folders in container with docker-compose
  • Vagrant and Docker not playing nice
  • How to stop the client IP address getting lost in a Cloudflare/Docker/NGINX/LAMP site?
  • Install redmine plug-in using docker container
  • FROM ubuntu:12.04
    
    RUN apt-get update
    RUN apt-get install -y apache2 & openssh-server
    
    ENV APACHE_RUN_USER www-data
    ENV APACHE_RUN_GROUP www-data
    ENV APACHE_LOG_DIR /var/log/apache2
    
    EXPOSE 80
    EXPOSE 22
    
    ENTRYPOINT ["/usr/sbin/apache2"]
    CMD ["-D", "FOREGROUND"]
    

    Then I ran this cmd:

    docker build -t="apache2" .
    $ docker run -d apache2
    

    When I do docker ps, I can see the port is assigned, but I am not able to connect to that container using ssh.

    $ docker ps

    ID                  IMAGE               COMMAND                CREATED              STATUS              PORTS
    5765535796f8        apache2:latest      /usr/sbin/apache2 -D   About a minute ago   Up About a minute   49154->80,49155->22
    

  • etcd api on CoreOS - setting ip address configurations remotely
  • Docker. No such file or directory
  • Docker Error while fetching server API version
  • how does an app within a docker container access database hosted on another server which is in the same LAN
  • Docker-Compose won't volume my php.ini file
  • Synchronizing numeric user id's between Dockerfiles and docker-compose.yml?
  • 3 Solutions collect form web for “Not able to connect to docker containers using SSH [closed]”

    It looks like there’s a typo in your docker file. Line:

    RUN apt-get install -y apache2 & openssh-server

    should be

    RUN apt-get install -y apache2 openssh-server

    If you change this as well as add the steps to setting the root password as shown in the “running an ssh service guide” are you able to ssh into your running container?

    https://docs.docker.com/engine/examples/running_ssh_service/

    in addition to installing ssh, you need to run the sshd service (you are only running apache)

    This can be accomplished by using something like supervisord or targeting a startup.sh script instead of apache in the ENTRYPOINT

    Dockerfile

    FROM ubuntu:12.04
    
    RUN apt-get update
    RUN apt-get install -y apache2 openssh-server
    
    ENV APACHE_RUN_USER www-data
    ENV APACHE_RUN_GROUP www-data
    ENV APACHE_LOG_DIR /var/log/apache2
    
    EXPOSE 80
    EXPOSE 22
    ADD ./startup.sh /opt/startup.sh
    
    ENTRYPOINT ["/opt/startup.sh"]
    

    startup.sh

    #!/bin/bash
    
    sshd
    
    apache2 -D FOREGROUND
    

    IMHO your looking for

    docker attach 5765535796f8

    Docker Commands

    edit:

    docker attach 57 should work as well.

    edit2:

    When creating your container make sure to name it, restart, set environment variables and path to persistent data, expose the ports needed:

    docker run -d --restart=always --name=<my_container> -e TZ=<timezone> -e TERM=xterm -p <some_unused_port>:22 -v -p 80:80 -p 443:443 -v /path/to/persistent/data:/data maintainer/image_name
    

    afterwards you can access the container like this:

    docker exec -it <my_container> /bin/bash
    

    re: running ssh in a container

    Security wise that is not such a good idea.

    Also letsencrypt is very handy – port 443 😉

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.