How to build ssh service based on golang image in docker

How to build ssh service based on golang image in docker

The following is the code for building an ssh service based on the golang image in docker. The specific content is as follows:

# golang:latest image FROM ee23292e2826
# Author MAINTAINER [email protected]
# 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 Install ssh service Modify root password Configure ssh service Allow root remote login Write "Enable ssh service Write address information to /root/ip.txt and tail -f" to /root/ip.sh Give ip.sh execution permission 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 ENTRYPOINT ["sh","-l"] at startup
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

#Inherit centos7 image FROM centos:centos7
MAINTAINER tpythoner [email protected]"
 
#yum install sshd service #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
 
#Set the root password and add the tpythoner user RUN /bin/echo 'root:mypwd' |chpasswd
RUN useradd tpythoner
RUN /bin/echo 'tpythoner:mypwd' |chpasswd
 
#Cancel pam restriction 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
 
#Install 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 service 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 new docker images

docker build -rm -t centos:go_sshd .

5. Run the image to generate the container

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

6. Connect to the go_sshd container

ssh [email protected] -p 2222 #ip is the container ip password is in Dockerfile: mypwd

This is the end of this article about building ssh service with docker based on golang image. For more information about building ssh service with docker, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Example of Form action and onSubmit

>>:  Detailed explanation of the sticky position attribute in CSS

Recommend

How to install SVN server under Linux

1. Yum installation yum install subversion 2. Con...

Let's talk about the LIMIT statement in MySQL in detail

Table of contents question Server layer and stora...

Solve the abnormal error when building vue environment with webpack

Table of contents First, configure package.json T...

mysql subquery and join table details

Table of contents 1. What is a subquery? 2. Self-...

How MySQL handles implicit default values

Some students said that they encountered the prob...

JS implements user registration interface function

This article example shares the specific code of ...

HTML realizes real-time monitoring function of Hikvision camera

Recently the company has arranged to do some CCFA...

MySQL SQL statement performance tuning simple example

MySQL SQL statement performance tuning simple exa...

Summary of various methods for JS data type detection

Table of contents background What are the methods...

Summary of the use of Vue computed properties and listeners

1. Computed properties and listeners 1.1 Computed...

CSS form validation function implementation code

Rendering principle In the form element, there is...

An article to help you learn CSS3 picture borders

Using the CSS3 border-image property, you can set...

Summary of some tips for bypassing nodejs code execution

Table of contents 1. child_process 2. Command exe...