This is a tutorial on using VScode and Docker over SSH. If you would like to use VScode and Docker on a local computer, please refer to this tutorial.
We assume that you have successfully installed Docker engine in the remote machine. You need to know the basic knowledge about docker.
In gerenal, we first configure SSH service in docker container and then use the VScode Remote-SSH Extension to connect to the docker container.
- First connect to the remote machine. Refer to the Dockerfile of this repo, and create your own Dockerfile in a folder, for example
/docker
.
The provided Dockerfile will configure the SSH service in docker container automatically.
In Dockerfile, we install theopenssh-server
with the below command:
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server
Then, we modify /etc/ssh/sshd_config
by setting UsePAM no
and PermitRootLogin yes
:
RUN \
sed -i "s/UsePAM yes/#UsePAM yes/" /etc/ssh/sshd_config && \
sed -i "s/PermitRootLogin/#PermitRootLogin/" /etc/ssh/sshd_config && \
echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config && \
echo 'UsePAM no' >> /etc/ssh/sshd_config
We echo a command to /etc/bash.bashrc
to start the SSH service when we open a bash:
RUN echo 'service ssh start' >> /etc/bash.bashrc
Finally, we can echo a password for the root of docker container in Dockerfile:
RUN echo 'root:your_password' | chpasswd
- Enter in the folder that includes the Dockerfile and build a docker image in a termintal as follow:
$ cd ~/docker
$ docker build -t docker_image .
- Then create a docker container with
docker_image
and map the port 22 of this container to the other port of the remote machine with-p xxxx:22
:
$ docker run -it --shm-size 8G -p 1234:6006 -p 1022:22 --ipc host --name docker_container --gpus all -v ~/your_project:/workspace docker_image /bin/bash
$ (currently, you should be in the docker container as root)
- If you did not set your password in Dockerfile, then set a password for SSH connection manually, type
passwd
in the terminal and enter your password twice. (Skip this step, if your Dockerfile containsRUN echo 'root:your_password' | chpasswd
)
Now we have finished the configuration of the SSH service in container.
-
Open VScode as a
New Window
. Then, click the Extensions. Search and install extenion Remote Development. This extension set consists of the Remote-SSH and other remote extensions:
-
Choose the the
Remote-SSH: Open configuration file
and open the/xxx/.ssh/config
file as the following figure:
-
Click the left bottom icon again and select
Remote-SSH:Connect Current Window to Host
. Then choose the Hostname that you wrote in the config file:
-
Now you have connected to the remote docker successfully. Install extensions in SSH:docker and enjoy your coding in remote docker.
Note: Remember that the container should be always started first when we connect it with VScode.