目录

在Docker中启动一个centos系统+代理+python

在Docker中启动一个centos系统+代理+python

过程记录

  1. 通过compose文件启动容器 运行位于附件中的linux-compose.yml文件 注意其中的卷映射规则,根据自己实际情况进行修改

    1
    
    docker-compose -f linux-compose.yml up -d
    
  2. 进入容器运行install.sh脚本,该脚本会为你自动更新软件包并设置root账户的密码为123456

    1
    2
    3
    
    docker exec -it <容器名> /bin/bash
    cd /
    ./install.sh 
    
  3. 设置代理

    如果有需要,可以为系统设置代理

    1
    2
    3
    4
    
    # 设置http代理
    export http_proxy=http://<ip>:<port>
    # 设置https代理
    export https_proxy=http://<ip>:<port> 
    

    取消代理

    1
    2
    3
    4
    
    unset http_proxy
    unset https_proxy
    # 可进行查看是否取消成功
    echo $http_proxy
    

附件

三个文件

install.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 对系统进行升级
yum update -y
# 添加 End Point 到 CentOS 7 仓库
yum install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm -y
# 安装openssh-server
yum install openssh-server -y
# 安装需要用到的软件vim用于编辑文件、passwd用于设置root或其他用户的密码、openssh-clients用于让此容器可以使用ssh命令、net-tools我暂时只是用来查看ip
yum install vim passwd openssh-clients net-tools -y
# 启动ssh服务
systemctl start sshd
# 将ssh服务加入到自启项
systemctl enable sshd
# 修改root账户密码为123456
echo 'root:123456' | chpasswd

install-Python310.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 安装编译所需依赖
yum -y update
yum -y install openssl-devel libffi-devel bzip2-devel
yum -y install git gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl openssl-devel tk-devel libffi-devel xz-devel tar wget make patch
yum -y groupinstall "Development Tools"
# 安装 Python 3.10 必需的 openssl >= 1.1.1
wget https://www.openssl.org/source/openssl-1.1.1q.tar.gz --no-check-certificate
tar zxf openssl-1.1.1q.tar.gz
cd openssl-1.1.1q
./config --prefix=/usr/local/openssl-1.1.1
make && make install
# 安装Python 3.10
wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz
tar zxf Python-3.10.8.tgz
cd Python-3.10.8
./configure --prefix=/opt/python/python3108 --enable-optimizations --with-openssl=/usr/local/openssl-1.1.1 --with-openssl-rpath=auto
make altinstall

sshd_config

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#       $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22
#AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server

linux-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
version: '3'
services:
    devLinux:
        image: centos:7
        # network_mode: host
        # deploy:
        #   resources:
        #     limits:
        #       cpus: '5'
        #       memory: 2G
        #     reservations:
        #       cpus: '3'
        #       memory: 1G
        container_name: devLinux
        restart: no
        privileged: true
        volumes:
        - D:\DockerVolumes\linux\scripts\install.sh:/opt/scripts/install.sh
        - D:\DockerVolumes\linux\scripts\install-Python310.sh:/opt/scripts/install-Python310.sh
        - D:\DockerVolumes\linux\sshd_config:/etc/ssh/sshd_config
        ports:
        - "2222:22"
        stdin_open: true
        command: /usr/sbin/init
        tty: true