[PYTHON] [Docker context] ~ How to access docker in remote environment from VScode ~

Introduction

Access the docker environment in the remote environment using VScode. (The image of the connection is as shown in the figure.) I have been developing with jupyter-lab without knowing this method, but it was very difficult to modify the py file and edit the web code. .. (It can be easily realized just by using docker context ...) It's a very useful method, so please give it a try!

If you find it helpful, I would appreciate it if you could use LGTM m (_ _) m

image.png

table of contents

  1. ssh connection settings
  2. docker context settings
  3. Connect to remote docker
  4. Return to local docker

1. ssh connection settings

On the client side, enter the ssh connection conditions in the configuration file

~/.ssh/config


Host home-ubuntu
  HostName home-ubuntu.mydns.jp
  IdentityFile ~/.ssh/home_ubuntu
  User shota
  Port 5504
  TCPKeepAlive yes
  IdentitiesOnly yes

2. docker context settings

What is docker context? (Quoted from the document below)

A single Docker CLI can have multiple contexts. Each context contains endpoint and security information needed to manage different clusters and nodes. You can easily set and switch between those contexts with the docker context command. Once you've set the context, you can easily switch between contexts by simply running the top-level command docker context use .

Create and switch the context from the following command

# 1.Add context
$ docker context create home-ubuntu --docker "host=ssh://shota@home-ubuntu"
# home-ubuntu
# Successfully created context "home-ubuntu"

# 2.Switching context
$ docker context use home-ubuntu
# home-ubuntu

# 3.Confirm context
$ docker context ls
# NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT   ORCHESTRATOR
# default             moby                Current DOCKER_HOST based configuration   # unix:///var/run/docker.sock                         swarm
# home-ubuntu *       moby

If you check the image and container with the docker command, you can see that it is switched to docker on the host side

$ docker ps
# CONTAINER ID   IMAGE                     COMMAND       CREATED       STATUS       PORTS     NAMES
# 451ead9000bf   tensorflow-from-scratch   "/bin/bash"   2 hours ago   Up 2 hours             tensorflow-from-scratch

3. Connect to remote docker

Install the following two extensions in VScode image.png image.png

If you click the extended icon of remote connection and click the window mark or folder mark, you can see the docker conteainer on the host side that you switched earlier. image.png Successful access! image.png

4. Return to local docker

When returning the target to the docker to be used locally

$ docker ps
# CONTAINER ID   IMAGE                     COMMAND       CREATED       STATUS       PORTS     NAMES
# 451ead9000bf   tensorflow-from-scratch   "/bin/bash"   2 hours ago   Up 2 hours             tensorflow-from-scratch

#When returning the target
$docker context use default

$ docker ps -a
#CONTAINER ID   IMAGE            COMMAND   CREATED       STATUS                        PORTS     NAMES
#0b0edc2e9df2   conda-init-env   "bash"    2 hours ago   Exited (255) 44 minutes ago             conda-init-env
#↑ Different container is displayed

that's all! If you find it helpful, I would appreciate it if you could use LGTM m (_ _) m

Recommended Posts

[Docker context] ~ How to access docker in remote environment from VScode ~
How to develop from VScode in a remote destination environment or a remote destination container environment
How to use Docker in VSCode DevContainer
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to reflect seeds.rb in production environment
How to use environment variables in RubyOnRails
Understand in 5 minutes !! How to use Docker
How to build Rails 6 environment with Docker
How to execute with commands of normal development language in Docker development environment
How to check the logs in the Docker container
How to link Rails6 Vue (from environment construction)
How to get a heapdump from a Docker container
Setting to exit from Docker container with VScode
How to get Class from Element in Java
How to update pre-built files in docker container
[Rails] How to build an environment with Docker
[Rails] How to use PostgreSQL in Vagrant environment
Temporarily move Docker environment from Mac to AWS
How to create a Java environment in just 3 seconds
How to deploy to Heroku from a local docker image
How to build docker environment with Gradle for intelliJ
List how to learn from Docker to AKS on AWS
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
Command memo to install xeyes in ubuntu docker environment
How to delete untagged images in bulk with Docker
How to launch Swagger UI and Swagger Editor in Docker
Ssh connect from container to git with VSCode Remote Container
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
Let's write how to make API with SpringBoot + Docker from 0
WSL2 + VSCode + Docker development environment
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
How to develop in a container with --privileged and / sbin / init passed in VSCode Remote Containers
How to set Docker nginx
How to store a string from ArrayList to String in Java (Personal)
What happened in "Java 8 to Java 11" and how to build an environment
How to build Rails, Postgres, ElasticSearch development environment with Docker
I tried to build the environment of WSL2 + Docker + VSCode
How to migrate from JUnit4 to JUnit5
Connect to AWS RDS from your local PC Docker environment
How to prevent past dates from being entered in Rails forms
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
[Rails] How to execute "rails db: create" etc. in production environment EC2
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
From building to deploying Ruby on Jets in docker-compose environment <Part 2>
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
From building to deploying Ruby on Jets in docker-compose environment <Part 1>
How to install Pry after building Rails development environment with Docker
Install lsb_release from the command line when lsb_release fails in docker environment
How to install the language used in Ubuntu and how to build the environment
From "Create mvn project" to "Create remote repository in GitLab" to "git init"
How to get and add data from Firebase Firestore in Ruby
How to build an Apache Flink application from scratch in 5 minutes
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
[Rails] How to reset the database in production environment (Capistrano version)
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
How to use Lombok in Spring
How to push from Tarminal to GitHub
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle