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

Implementation code for adding links to FLASH through HTML (div layer)

Today a client wants to run an advertisement, and ...

How to dynamically modify the replication filter in mysql

MySQL dynamically modify replication filters Let ...

MySQL compression usage scenarios and solutions

Introduction Describes the use cases and solution...

CSS tips for controlling animation playback and pause (very practical)

Today I will introduce a very simple trick to con...

HTML web page creation tutorial Use iframe tags carefully

Using iframes can easily call pages from other we...

Detailed explanation of SQL injection - security (Part 2)

If there are any errors in this article or you ha...

Seven different color schemes for website design experience

The color matching in website construction is ver...

Simple summary of tomcat performance optimization methods

Tomcat itself optimization Tomcat Memory Optimiza...

How to enable JMX monitoring through Tomcat

Build a simulation environment: Operating system:...

Docker builds cluster MongoDB implementation steps

Preface Due to the needs of the company's bus...

Install nvidia graphics driver under Ubuntu (simple installation method)

Install the nvidia graphics card driver under Ubu...

Detailed explanation of the use of Vue image drag and drop zoom component

The specific usage of the Vue image drag and drop...