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

In-depth study of MySQL multi-version concurrency control MVCC

MVCC MVCC (Multi-Version Concurrency Control) is ...

Teach you how to build a react+antd project from scratch

The previous articles were all my own learning lo...

Will CSS3 really replace SCSS?

When it comes to styling our web pages, we have t...

How to change the root password of Mysql5.7.10 on MAC

First, start MySQL in skip-grant-tables mode: mys...

Detailed explanation of Angular component life cycle (I)

Table of contents Overview 1. Hook calling order ...

Detailed explanation of 4 common data sources in Spark SQL

Generic load/write methods Manually specify optio...

Details of watch monitoring properties in Vue

Table of contents 1.watch monitors changes in gen...

About the garbled problem caused by HTML encoding

Today a junior student asked a question. The HTML...

How to use docker to deploy dubbo project

1. First, use springboot to build a simple dubbo ...

How to add double quotes in HTML title

<a href="https://www.jb51.net/" titl...

How to detect whether a file is damaged using Apache Tika

Apache Tika is a library for file type detection ...

Use CSS content attr to achieve mouse hover prompt (tooltip) effect

Why do we achieve this effect? ​​In fact, this ef...