DEV Community

Cover image for Okteto: work with simple Dockerfile
Ashok Nagaraj
Ashok Nagaraj

Posted on

Okteto: work with simple Dockerfile

Developer Environments
Traditional development involves committing code, pushing to repository, kicking start a CI pipeline, passing and then pushing it to a cluster through CD practices. The task grows burdonsome as you need to do it multiple times a day and as more and more develipers get added to the mix.
Dev Environments attempt to solve this by allowing you to Live preview your changes as you save the code (er changes) in your editor. While this can be done with scripts and wrappers it comes with a lot of overhead of configuration and up-keep. Okteto cli intends to provide you a cli set to ease this burden with well-defined flows.

Install cli
❯ brew install okteto
...
❯ okteto version
okteto version 1.14.9
Enter fullscreen mode Exit fullscreen mode
Configure okteto cli to your kubernetes context
❯ kubectl ctx # from krew ctx plugin
default

❯ okteto context use default
 ✓  Using context default @ default
 i  Run 'okteto context update-kubeconfig' to update your kubectl credentials

❯ okteto context update-kubeconfig
 i  Updated kubernetes context 'default/default' in '[/Users/nashok/k3s.conf]'

❯ okteto context list
Name       Namespace  Builder  Registry
default *  default    docker   -
Enter fullscreen mode Exit fullscreen mode
Developer workflow
  • Sample python application
cat app.py
import datetime
from flask import Flask, jsonify

app = Flask('example')
now = datetime.datetime.now()

@app.route('/', methods=['GET'])
def root():
  return jsonify({'message': 'hello world'})

@app.route('/healthz', methods=['GET'])
def health():
  return jsonify({'message': f'up since {now}'})

if __name__ == '__main__':
  app.run(host='0.0.0.0', port=8080, debug=True)cat Dockerfile
FROM python:3.8-slim-buster

WORKDIR /python-docker

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

EXPOSE 8080

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

❯ tree -L 1
.
├── Dockerfile 
├── app.py
├── requirements.txt
Enter fullscreen mode Exit fullscreen mode
  • Run okteto init
❯ okteto init
 i  Using default @ default as context
 ✓  Okteto manifest (okteto.yml) deploy and build configured successfully
 ?  Do you want to launch your development environment? [y/n]: n

❯ tree -L 1
.
├── Dockerfile
├── app.py
├── okteto.yml # gets created
├── requirements.txt

Enter fullscreen mode Exit fullscreen mode
  • Build and push the images
grep image: okteto.yml
    image: ashoka007/okteto-test:0.0.1

❯ okteto build
 i  Building image for service 'python'
 i  Building the image 'ashoka007/okteto-test:0.0.1' using your local docker daemon
[+] Building 4.2s (11/11) FINISHED
 => [internal] load build definition from Dockerfile                                                                             => => transferring dockerfile: 255B
 ...
0.0.1: digest: sha256:dfd2fa6109e7d6cfade3c6eda90319b0f59816877faf4a23e18bb6cf6a50bc2f size: 2207
 ✓  Image 'ashoka007/okteto-test:0.0.1' successfully pushed
Enter fullscreen mode Exit fullscreen mode
  • Deploy images
❯ okteto deploy
 i  Using default @ default as context
 i  Images were already built. To rebuild your images run 'okteto build' or 'okteto deploy --build'
 i  Running kubectl apply -f kubernetes.yaml
deployment.apps/okteto-test-dep created
service/okteto-test-svc created
There are no available endpoints for 'python'
 ✓  Development environment 'python' successfully deployed
 i  Run 'okteto up' to activate your development container
Enter fullscreen mode Exit fullscreen mode
  • Start synchronization
❯ okteto up
 i  Using default @ kind-macbook as context
 i  Development environment 'docker' already deployed.
 i  To redeploy your development environment run 'okteto deploy' or 'okteto up [devName] --deploy'
 i  Images were already built. To rebuild your images run 'okteto build' or 'okteto deploy --build'
 ✓  Images successfully pulled
 ✓  Files synchronized
    Context:   kind-macbook
    Namespace: default
    Name:      test-app
    Forward:   8080 -> 8080
               9229 -> 9229
root@test-app-okteto-6bb5c444cf-txvxt:/python-docker# 
Enter fullscreen mode Exit fullscreen mode
  • Start the service in DEV mode and test working
root@test-app-okteto-6bb5c444cf-txvxt:/python-docker# python app.py
 * Serving Flask app 'example' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on all addresses (0.0.0.0)
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://127.0.0.1:8080
 * Running on http://10.244.1.16:8080 (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 111-486-135
127.0.0.1 - - [03/Aug/2022 05:26:16] "GET / HTTP/1.1" 200 -

# test from localhost
❯ curl localhost:8080
{
  "message": "hello from app again @ 2022-08-03 05:26:02.759327"
}
Enter fullscreen mode Exit fullscreen mode
  • Check the bi-directional sync between the DEV pod and localhost
# localhosttouch test.txt
❯ ls
Dockerfile       app.py           okteto.yaml      requirements.txt test.txt

# inside the pod
root@test-app-okteto-6bb5c444cf-txvxt:/python-docker# ls
Dockerfile  app.py  okteto.yaml  requirements.txt  test.txt
Enter fullscreen mode Exit fullscreen mode
  • Clean up
root@test-app-okteto-6bb5c444cf-txvxt:/python-docker# exit
exit

# localhost
❯ okteto down
 i  Using default @ kind-macbook as context
 ✓  Development container 'test-app' deactivated

Enter fullscreen mode Exit fullscreen mode

Top comments (0)