docker Method for Constructing ssh Service Based on golang Image

  • 2021-11-01 05:45:02
  • OfStack

The following is an introduction to the code for docker to build ssh services based on golang images, as follows:


# golang:latest Mirror image 
FROM ee23292e2826
#  Author 
MAINTAINER dechao@qq.com
#  Add Golang Environment variable 
ENV GOPROXY https://goproxy.cn,direct
ENV GO111MODULE on
#  Configure apt-get Source 
ADD sources.list /etc/apt/
#  Update apt-get Source   Installation ssh Services   Modify root Password   Configure ssh Service Allow root Remote login   Write " Open ssh Services   Write address information to /root/ip.txt  And tail -f" To /root/ip.sh  Endowed with ip.sh Execution authority 
RUN apt-get update \
&& apt-get -y install ssh \
&& echo "root:1" | chpasswd  \
&& echo "PermitRootLogin yes" >> /etc/ssh/sshd_config \
&& echo "service ssh start && ip addr | grep global > /root/ip.txt && tail -f /root/ip.txt" > /root/ip.sh \
&& chmod +x /root/ip.sh
#  Execute on startup 
ENTRYPOINT ["sh","-l"]
CMD ["/root/ip.sh"]

-p Host Address: Host Port: Container Port
-v Host Volume: Container Volume
docker run -itd -p 2222:22 -v /root/fserver/:/go/src/fserver 4618

PS: Docker based on sshd and golang environment under CentOS

1. Dockerfile file


# Inheritance centos7 Mirror image 
FROM        centos:centos7
MAINTAINER  tpythoner tpythoner@gmail.com"
 
#yum Installation sshd Services 
#RUN         yum install -y openssh openssh-server openssh-clients
RUN         yum install -y openssh-server
 
# Create sshd
RUN         mkdir /var/run/sshd
RUN         ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN         ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
 
# Settings root Password and add tpythoner Users 
RUN         /bin/echo 'root:mypwd' |chpasswd
RUN         useradd tpythoner
RUN         /bin/echo 'tpythoner:mypwd' |chpasswd
 
# Cancel pam Limit 
RUN         /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd
RUN         /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
 
# Installation golang
#RUN                    yum install -y wget
#RUN                    wget http://golangtc.com/static/go/go1.4.2.linux-amd64.tar.gz
#RUN                    tar zxvf go1.4.2.linux-amd64.tar.gz -C /usr/local/
ADD                     go1.4.2.linux-amd64.tar.gz /root
ADD                     golang.conf /root/golang.conf
RUN                     mv /root/go /usr/local/
#RUN                    echo "export GOROOT=/usr/local/go" >> /etc/profile
#RUN                    echo "export GOBIN=$GOROOT/bin" >> /etc/profile
#RUN                    echo "export PATH=$PATH:$GOBIN" >> /etc/profile
#RUN                    echo "export GOPATH=/home/golang" >> /etc/profile
RUN                     cat /root/golang.conf >> /etc/profile
RUN                     echo "source /etc/profile" >> /root/.bashrc
RUN                     mkdir -p /home/golang
# Development port 
EXPOSE      22
EXPOSE      80
# Start sshd Services 
CMD                     /usr/sbin/sshd -D

2. golang. conf


export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN
export GOPATH=/home/golang

3. Download go1.4. 2. linux-amd64.tar. gz

wget http://golangtc.com/static/go/go1.4.2.linux-amd64.tar.gz

4. Create a new docker images

docker build -rm -t centos:go_sshd .

5. Run the Mirror Generation Container


docker run -d -p 2222:22 -p 80:80 centos:go_sshd
# If encountered WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
echo '' >> ~/.ssh/known_hosts

6. Connect the go_sshd container

ES61en ES62en @ 192.168. 59.103-ES63en 2222 # ES64en is container ES65en password is ES67en in ES66en


Related articles: