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

Explanation of building graph database neo4j in Linux environment

Neo4j (one of the Nosql) is a high-performance gr...

Brief analysis of mysql scheduled backup tasks

Introduction In a production environment, in orde...

CSS sets the box container (div) height to always be 100%

Preface Sometimes you need to keep the height of ...

How to Find the Execution Time of a Command or Process in Linux

On Unix-like systems, you may know when a command...

Mac VMware Fusion CentOS7 configuration static IP tutorial diagram

Table of contents Install CentOS7 Configuring Sta...

GDB debugging MySQL actual combat source code compilation and installation

Download source code git clone https://github.com...

How to use DPlayer.js video playback plug-in

DPlayer.js video player plug-in is easy to use Ma...

How to delete node_modules and reinstall

Table of contents Step 1: Install node_modules in...

CSS achieves the effect of two elements blending (sticky effect)

I remember that a few years ago, there was an int...

Example of cross-database query in MySQL

Preface In MySQL, cross-database queries are main...

Linux Dig command usage

Dig Introduction: Dig is a tool that queries DNS ...