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

Use of Linux dynamic link library

Compared with ordinary programs, dynamic link lib...

How to quickly build an FTP file service using FileZilla

In order to facilitate the storage and access of ...

Solve the problem that Navicat cannot connect to MySQL on the Linux server

At the beginning, I felt sad. The screenshots are...

Several situations that cause MySQL to perform a full table scan

Table of contents Case 1: Case 2: Case 3: To summ...

Detailed explanation of how to find the location of the nginx configuration file

How can you find the location of the configuratio...

Let's talk in detail about the direction of slow SQL optimization in MySQL

Table of contents Preface SQL statement optimizat...

jQuery plugin to implement stacked menu

A jQuery plugin every day - stacked menu, for you...

mysql workbench installation and configuration tutorial under centOS

This article shares the MySQL Workbench installat...

Docker-compose creates a bridge, adds a subnet, and deletes a network card

1. Create a docker network card [root@i ~]# brctl...

Things to note when designing web pages for small-screen mobile devices

The reason is that this type of web page originate...

Linux file/directory permissions and ownership management

1. Overview of file permissions and ownership 1. ...

Eight hook functions in the Vue life cycle camera

Table of contents 1. beforeCreate and created fun...

MySQL master-slave replication configuration process

Main library configuration 1. Configure mysql vim...

Vue imports Echarts to realize line scatter chart

This article shares the specific code of Vue impo...