Is it possible to SSH into FARGATE managed container instances?

后端 未结 3 1003
孤街浪徒
孤街浪徒 2020-12-17 07:47

I use to connect to EC2 container instances following this steps, https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance-connect.html wondering how I can conne

相关标签:
3条回答
  • 2020-12-17 08:05

    Looking on that issue on github https://github.com/aws/amazon-ecs-cli/issues/143 I think it's not possible to make docker exec from remote host into container on ECS Fargate. You can try to run ssh daemon and your main process in one container using e.g. systemd (https://docs.docker.com/config/containers/multi-service_container/) and connect to your container using SSH but generally it's not good idea in containers world.

    0 讨论(0)
  • 2020-12-17 08:06

    It is possible, but not easy.straight forward. Shortly: install SSH, don't expose ssh port out from VPC, add bastion host, SSH through bastion.

    A little bit more details:

    • spin up SSHD with password-less authentication. Docker instructions
    • Fargate Task: Expose port 22
    • Configure your VPC, instructions
    • create EC2 bastion host
    • From there SSH into your Task's IP address
    0 讨论(0)
  • 2020-12-17 08:24

    Here is an example of adding SSH/sshd to your container to gain direct access:

    # Dockerfile
    FROM alpine:latest
    
    RUN apk update && apk add --virtual --no-cache \
      openssh
    
    COPY sshd_config /etc/ssh/sshd_config
    
    RUN mkdir -p /root/.ssh/
    COPY authorized-keys/*.pub /root/.ssh/authorized_keys
    RUN cat /root/.ssh/authorized-keys/*.pub > /root/.ssh/authorized_keys
    RUN chown -R root:root /root/.ssh && chmod -R 600 /root/.ssh
    
    COPY docker-entrypoint.sh /usr/local/bin/
    RUN chmod +x /usr/local/bin/docker-entrypoint.sh
    RUN ln -s /usr/local/bin/docker-entrypoint.sh /
    
    # We have to set a password to be let in for root - MAKE THIS STRONG.
    RUN echo 'root:THEPASSWORDYOUCREATED' | chpasswd
    
    EXPOSE 22
    ENTRYPOINT ["docker-entrypoint.sh"]
    
    # docker-entrypoint.sh
    #!/bin/sh
    
    if [ "$SSH_ENABLED" = true ]; then
      if [ ! -f "/etc/ssh/ssh_host_rsa_key" ]; then
        # generate fresh rsa key
        ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
      fi
      if [ ! -f "/etc/ssh/ssh_host_dsa_key" ]; then
        # generate fresh dsa key
        ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
      fi
    
      #prepare run dir
      if [ ! -d "/var/run/sshd" ]; then
        mkdir -p /var/run/sshd
      fi
    
      /usr/sbin/sshd
    
      env | grep '_\|PATH' | awk '{print "export " $0}' >> /root/.profile
    fi
    
    exec "$@"
    

    More details here: https://github.com/jenfi-eng/sshd-docker

    0 讨论(0)
提交回复
热议问题