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

Detailed Introduction to MySQL Innodb Index Mechanism

1. What is an index? An index is a data structure...

Learn v-model and its modifiers in one article

Table of contents Preface Modifiers of v-model: l...

MySQL replication detailed explanation and simple example

MySQL replication detailed explanation and simple...

What is web design

<br />Original article: http://www.alistapar...

How to add vector icons to web font files in web page production

As we all know, there are two types of images in c...

Detailed explanation of CSS elastic box flex-grow, flex-shrink, flex-basis

The functions of the three attributes flex-grow, ...

The latest 36 high-quality free English fonts shared

01. Infinity Font Download 02. Banda Font Download...

Special commands in MySql database query

First: Installation of MySQL Download the MySQL s...

How to implement the strategy pattern in Javascript

Table of contents Overview Code Implementation Su...

vitrualBox+ubuntu16.04 install python3.6 latest tutorial and detailed steps

Because I need to use Ubuntu+Python 3.6 version t...

Docker modifies the configuration information of an unstarted container

When I first used docker, I didn't use docker...

How to uninstall MySQL cleanly (tested and effective)

How to uninstall Mysql perfectly? Follow the step...