How to set up vscode remote connection to server docker container

How to set up vscode remote connection to server docker container

Pull the image

docker pull [options] name [:tag] means pulling the image from the repository. options is a parameter. tag is a version.

Run the image (generate a container)

docker run [options] image [:tag] [command] [arg…]

Run a container to create a container using the image as a template options operation tag version command command to execute when running arg parameter

Option Option Abbreviation Description –detach -d Run the container in the background and print the container id.
–interactive -i Keep standard input open even if there is no connection. Usually used with -t.
–tty -t Allocate a pseudo-tty, usually used with -i.

After the docker container completes the task, it will be in the exited state. If you want to put the container in the up state, you can use the following command, such as:
Use the image nginx:latest to start a container in interactive mode and execute the /bin/bash command in the container.

docker run -dit nginx:latest /bin/bash

Start the container

docker start container ID

Entering the container

docker attach container id
docker exec -it container ID /bin/bash
docker exec -it container name bash

View All Mirrors

List images: docker images [OPTIONS] [REPOSITORY[:TAG]]

Exit the container

If you want to exit normally without closing the container, press (Ctrl+P+Q) to exit the container. If you use exit to exit, the container will be closed after exiting.

Restarting the container

Restart the container using the (docker restart container id) command

View All Containers

docker container ls
docker ps (view running containers)
docker ps -a (view all containers)

Deleting a container

We can also use the docker container rm command to delete a specified container, or simply write the docker rm command to delete the container. However, it is not allowed to delete a running container, so if you want to delete it, you must stop the container first.

docker rm container_id

When we need to delete all containers in batches, we can use the following command:

docker rm $(docker ps -q)

Batch delete stopped containers in Docker

Method 1:

#Show all containers, filter out containers in the Exited state, and retrieve the IDs of these containers.

sudo docker ps -a|grep Exited|awk '{print $1}'

#Query all containers, filter out containers in Exited state, list container IDs, and delete these containers sudo docker rm `docker ps -a|grep Exited|awk '{print $1}'`

Method 2:

#Delete all non-running containers (the running ones cannot be deleted, and the non-running ones will be deleted together)

sudo docker rm $(sudo docker ps -a -q)

Method 3:

#According to the status of the container, delete the container in the Exited state sudo docker rm $(sudo docker ps -qf status=exited)

Method 4:

#After Docker version 1.13, you can use the docker containers prune command to delete isolated containers.

vscode remotely connects to the container in the server via ssh

1. Run the ubuntu image to create a container:

docker run -it ubuntu

2. Enter the container and set the container root password

Modify the container's root password: passwd
Password is set to: 123456

3. Install ssh service

apt-get update
apt-get install openssh-server

4. Modify the ssh configuration to allow root login. Generally, the root account is used to enter the container, but ssh prohibits the root account from using a password to log in remotely by default, so you need to modify the ssh configuration file to allow it:

vim /etc/ssh/sshd_config
Change the value of PermitRootLogin from withoutPassword to yes (remove the leading #)
If you don't have vim, you can install it:
apt-get install vim

5. Save container modifications and generate a new image

docker commit <container_id> <new_image_name>
For example, docker commit <container_id> ubuntu-ssh

6. Exit the current container and run the new image just saved (this time you need to map the port and run it in the background)

exit (the container will be closed after exit)
docker run -dit -p 8008:22 ubuntu-ssh (8008 is the port number, which is used when connecting via ssh)

7. Enter the container running in the background through the exec command

docker exec -it container_id /bin/bash
#exec is to enter an existing container, run is to create a new container

8. Start ssh service

sudo service ssh start

In addition (stop restart is shutdown and restart respectively)

9. Determine whether the startup is successful

Enter in the terminal: ps -e|grep ssh to check whether it is started successfully. If there is sshd, it means that it is started successfully.

The output is as follows:

$ sudo ps -e | grep ssh

 4031 ? 00:00:00 sshd------corresponding to the server-side sshd, indicating that the ssh-server is started

10. Exit the container but don’t shut it down

exit (because we enter the container through the exec command, exit does not exit the container, the container will run in the background)

11 Remote Connection

ssh root@host_id -p 8008

This is the end of this article about how to set up vscode remote connection to server docker container. For more relevant vscode remote connection to docker content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker enables secure TLS remote connection access
  • Docker deploys mysql remote connection to solve 2003 problems
  • Detailed example of remotely connecting to Docker using TLS encrypted communication
  • Tutorial on installing MySQL with Docker and implementing remote connection
  • Docker deploys mysql to achieve remote connection sample code
  • Detailed explanation of docker daemon remote connection settings
  • Implementation example of Docker remote connection settings

<<:  HTML discount price calculation implementation principle and script code

>>:  Responsive Web Design Learning (2) — Can videos be made responsive?

Recommend

W3C Tutorial (5): W3C XML Activities

XML is designed to describe, store, transmit and ...

Implementation of webpack code fragmentation

Table of contents background CommonsChunkPlugin s...

Sample code for testing technology application based on Docker+Selenium Grid

Introduction to Selenium Grid Although some new f...

Detailed tutorial on installing mysql 8.0.13 (rpm) on Centos7

yum or rpm? The yum installation method is very c...

Detailed steps to build an independent mail server on Centos7.9

Table of contents Preface 1. Configure intranet D...

Introduction to nesting rules of html tags

There are many XHTML tags: div, ul, li, dl, dt, d...

A simple example of using Vue3 routing VueRouter4

routing vue-router4 keeps most of the API unchang...

How to query duplicate data in mysql table

INSERT INTO hk_test(username, passwd) VALUES (...

Use docker to build kong cluster operation

It is very simple to build a kong cluster under t...

Detailed tutorial on installing Docker on CentOS 8

1. Previous versions yum remove docker docker-cli...

Example code for implementing complex table headers in html table

Use HTML to create complex tables. Complex tables...

MySQL 8.0.22 winx64 installation and configuration graphic tutorial

mysql 8.0.22 winx64 installation and configuratio...

A brief analysis of event bubbling and event capture in js

Table of contents 01-Event Bubbling 1.1- Introduc...

Summary of common sql statements in Mysql

1. mysql export file: SELECT `pe2e_user_to_compan...

Basic usage details of Vue componentization

Table of contents 1. What is componentization? 2....