Edit Docker Container in VSCode multi-stage SSH Vagrant

Create sshconfig

Create a config file in C: \ Users \ username \ .ssh and write the following

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Execute the following with powershell

$ scp .vagrant/machines/default/virtualbox/private_key default:/home/vagrant/.ssh/

After logging in to vagrant, execute the following

$ vagrant ssh
# private_Change the execute permission of key
$ chmod 600 ~/.ssh/private_key

Transfer of private key

#Note`vagrant up`Run in the directory
> scp .vagrant/machines/default/virtualbox/private_key vagrant-os:/home/vagrant/.ssh/

#SSH connection to Guest OS
> vagrant ssh

# private_Change the execution authority of key (in Guest OS)
$ chmod 600 ~/.ssh/private_key

Launch Docker Container

FROM ruby:2.5.1

#Update repository and install dependent modules
RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       nodejs

#Create a working directory with the name webapp directly under the root (application directory in the container)
RUN mkdir /webapp
WORKDIR /webapp

#Host Gemfile and Gemfile.Copy lock to container
ADD Gemfile /webapp/Gemfile
ADD Gemfile.lock /webapp/Gemfile.lock

#Run bundle install
RUN bundle install

#Copy everything in the host's application directory to a container
ADD . /webapp

# puma.Create a directory to place sock
RUN mkdir -p tmp/sockets


#From here to the end is the openssh setting
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd

# /etc/ssh/sshd_Rewrite config settings
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

#Copy public key
COPY authorized_keys /root/authorized_keys

EXPOSE 22

# Activate authorized_keys & Boot sshd
CMD mkdir ~/.ssh && \
    mv ~/authorized_keys ~/.ssh/authorized_keys && \
    chmod 0600 ~/.ssh/authorized_keys &&  \
    #Finally start ssh
    /usr/sbin/sshd -D

#Copy public key
$ cp ~/.ssh/authorized_keys .

# build
$ docker build ./ -t example

#Launch Docker Container
# Port:10000 Port:Transfer to 22
$ docker run -d -p 10000:22 example

#SSH connection to Docker Container
$ ssh -o 'StrictHostKeyChecking no' [email protected] -p 10000 -i ~/.ssh/private_key

Check with powershell

$ ssh -o 'StrictHostKeyChecking no' docker-os

Edit .ssh/config file

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

#Added below
Host docker-os
Hostname 127.0.0.1
User root
Port 10000
ProxyCommand C:\Windows\System32\OpenSSH\ssh.exe -W %h:%p default
IdentityFile D:/vagrant/centos7/.vagrant/machines/default/virtualbox/private_key

After launching VScode Remote Development installation from extension Edit from sshTargets in Remote Explorer

Recommended Posts

Edit Docker Container in VSCode multi-stage SSH Vagrant
Kind @ Mac in Docker and vctl container
Edit Mysql with commands in Docker environment
MySQL container does not start in Docker
[Docker] Start container, start bash in container, delete image
Japanese setting of mysql in Docker container
How to check the logs in the Docker container
Setting to exit from Docker container with VScode
How to update pre-built files in docker container
WSL, Zsh, VSCode, Docker, NodeJS setup summary in 2020
How Docker works ~ Implement the container in 60 lines
Small Docker container
Docker in LXD
Access and debug Circle CI Docker container with ssh
Ssh connect from container to git with VSCode Remote Container