Home > other >  dockerfile and kubernetes jobs ( assistance needed)
dockerfile and kubernetes jobs ( assistance needed)

Time:12-03

I have my dockerfile in which i have used postgres:12 image and i modified it using some ddl scripts and then i build this image and i can run the container through docker run command but how i can use Kubernetes jobs to run build image , as I dont have good exp on k8s.

This is my dockerfile here you can see it.

docker build . -t dockerdb

FROM postgres:12 

ENV POSTGRES_PASSWORD xyz@123123!233 

ENV POSTGRES_DB test 

ENV POSTGRES_USER test 

COPY ./Scripts /docker-entrypoint-initdb.d/

How i can customize the below code using the below requirement

apiVersion: batch/v1
      kind: Job
      metadata:
        name: job-1
      spec:
        template:
          metadata:
            name: job-1
          spec:
            containers:
            - name: postgres
              image: gcr.io/project/pg_12:dev
              command:
              - /bin/sh
              - -c
              - "not sure what command should i give in last line" 

CodePudding user response:

Not sure how you are running the docker image

if you are running your docker image without passing any command you can directly run the image in Job.

docker run <imagename>

once your Dockerimage is ready and build you can run it directly

You job will get executed without passing any command

apiVersion: batch/v1
      kind: Job
      metadata:
        name: job-1
      spec:
        template:
          metadata:
            name: job-1
          spec:
            containers:
            - name: postgres
              image: gcr.io/project/pg_12:dev

if you want to pass any argument or command that you can pass further

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: <CHANGE IMAGE URL>
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

Just to update above template is for Cronjob, Cronjob run on specific time.

  • Related