Docker Desktop installs Ubuntu and allows remote access

Table of contents

1. Problem background

2. Operation steps

1. Open the command line (Windows key + R, then enter cmd, and press Enter)

2. Check all the local mirrors first

3. View the image version (label) to be pulled

4. Pull the corresponding image

5. Run a container

6. Use the terminal to enter Ubuntu

7. Update

8. Install ssh service

9. Install vim editor

10. Edit the ssh configuration file

11. Start the ssh service and view the service status

12. Set a password for root for easy login

3. Test


1. Problem background

        My computer is Windows11 operating system, I hope to be able to use Linux operating system on this computer, and be able to remotely access this Linux operating system through the network, using Docker is a solution to this problem. Docker has a Windows version called Docker Desktop. Official website link:

        Docker: Accelerated, Containerized Application Development

        After installing Docker Desktop, you can pull (pull) an image (Image) and run a container (Container). If you don't know what images and containers are, you can simply understand images as classes and containers as objects. Based on an image, a container can be built and run.

        The process of the following process mainly includes:

                1. Pull the image.

                2. Run the container.

                3. Install the service and run it (because it is found that the pulled ubuntu image is a minimal installation, and many important services are not available, such as the ssh service for remote login).

        

2. Operation steps

1. Open the command line (Windows key + R, then enter cmd, and press Enter)

2. Check all the local mirrors first

Enter the following command at the command line:

docker images

3. View the image version (label) to be pulled

You can search for ubuntu in the search bar to find the corresponding version. (You can also directly visit the official website:  Ubuntu  )

 I chose the Ubuntu 22.04 version.

4. Pull the corresponding image

Enter the following command at the command line:

docker pull ubuntu:22.04

5. Run a container

Enter the following command at the command line:

docker run -itd -p IP地址:外部端口:内部端口 --name 容器名字 镜像:标签 /bin/bash

注:
    外部端口是指Windows操作系统中的端口
    内部端口是指容器中的Ubuntu操作系统的端口
    这里是一个例子:
    docker run -itd -p 0.0.0.0:10000:22 --name myubuntu ubuntu:22.04 /bin/bash
    其中:
        0.0.0.0表示本地所有的Ip
        这里把windows系统中的10000端口映射到了ubuntu的22端口(ssh需要使用22端口)

You can view running container information with the following command:

You can also see relevant information on the Docker Desktop interface:

6. Use the terminal to enter Ubuntu

 Click "Open in Terminal":

 After opening the terminal it looks like this:

7. Update

apt update

8. Install ssh service

apt install openssh-server

9. Install vim editor

 The ssh configuration file needs to be edited, so the vim editor needs to be installed:

 10. Edit the ssh configuration file

The location of the configuration file is located in "/etc/ssh", you need to edit the sshd_config file in this directory

The following four configurations are required:

PermitRootLogin yes #允许root使用ssh登录

PubkeyAuthentication yes #启用公钥私钥配对认证方式

AuthorizedKeysFile .ssh/authorized_keys # AuthorizedKeysFile项已包含在配置文件,这里只需要取消注释即可,此处的路径“.ssh/authorized_keys”以本地文件为准

UsePAM no #不适用PAM

 The configuration results are shown in the following two figures:

11. Start the ssh service and view the service status

service ssh start

service ssh status

12. Set a password for root for easy login

Use the "passwd" command to set a password (Note: In order to ensure security, when setting a password, the input content will not be output to the screen, so when entering characters, the input content cannot be seen, just input normally)

3. Test

On another computer, use ssh to log in to the root of Ubuntu in the container, and the configuration is successful:

Parts of this article refer to:

Docker Docs: How to build, share, and run applications

docker ssh connection_docker container ssh connection_Dennis-Chen's Blog-CSDN Blog

If there is any inappropriate or wrong, please correct me, thank you! ! !

Guess you like

Origin blog.csdn.net/qq_44667259/article/details/129867255