学习转载:https://www.jianshu.com/p/c87b9ba51c67

讲dockerfile很好的文章:https://blog.csdn.net/wo18237095579/article/details/80540571


新建gitserver镜像的dockerfile

FROM alpine:latest

MAINTAINER Ralph Wang

RUN apk add --no-cache openssh git

RUN ssh-keygen -A

RUN adduser -D -s /bin/sh git && passwd -d git

RUN mkdir -p /opt/git

WORKDIR /opt

RUN chown -R git:git ./git

USER git

WORKDIR /home/git

RUN mkdir .ssh && chmod 700 .ssh && touch .ssh/authorized_keys && touch .ssh/known_hosts && chmod 600 .ssh/authorized_keys

EXPOSE 22

USER root

CMD /usr/sbin/sshd -D


生成镜像

docker build -t gitserver:v1 .


编辑git-depoyment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: gitserver

  labels:

    app: gitserver

spec:

  replicas: 1 

  selector:

    matchLabels:

      app: gitserver

  template: # This is the pod template

    metadata:

      labels:

        app: gitserver

    spec:

      containers:

      - name: gitserver

        image: gitserver:v1 

        imagePullPolicy: Never # Force to use 本地镜像

        ports:

        - containerPort: 22

          name: ssh

          protocol: TCP

部署pods

kubectl create -f  git-deployment.ymal

kubectl get pods


编辑git-ssh-service.yaml

apiVersion: v1

kind: Service

metadata:

  labels:

    app: gitserver

  name: git-ssh # 服务的名称

spec:

  type: NodePort # 服务的 Type

  ports:

  - port: 22

    targetPort: 22

    name: ssh

  selector:

    app: gitserver

部署services

kubectl create -f git-ssh-service.yaml

kubectl get services


问题:

1、 [Warning] IPv4 forwarding is disabled. Networking will not work.

echo "net.ipv4.ip_forward = 1"  /usr/lib/sysctl.d/00-system.conf

service network restart && systemctl restart docker 


2、fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.g

WARNING: Ignoring http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz: temporary error (try again later)

ERROR: unsatisfiable constraints:

  git (missing):

    required by: world[git]

  openssh (missing):

    required by: world[openssh]

The command '/bin/sh -c apk add --no-cache openssh git' returned a non-zero code : 2

问题1解决了,问题2也就消失了后来又卡在15/15最后一步

修改安装源ok

RUN apk add --no-cache openssh git --repository=http://mirrors.aliyun.com/alpine/v3.8/community/


3、docker build -t gitserver:v1 .

Sending build context to Docker daemon  2.378GB(很大原来是把其它文件也传过去了)

需要单独建目录存放dockerfile并在里面运行就正常了