<p>I am just wondering to know how should I create a <code>docker</code> file  for a Flutter app then deploy it on a <code>Kubernetes</code> cluster?</p>
<p>I found the following <code>Dockerfile</code> and <code>server.sh</code> script from <a href="https://blog.logrocket.com/containerizing-flutter-web-apps-with-docker/" rel="nofollow noreferrer">this</a> website but I am not sure if this a correct way of doing it?</p>
<pre><code># Install Operating system and dependencies
FROM ubuntu:22.04

RUN apt-get update 
RUN apt-get install -y curl git wget unzip libgconf-2-4 gdb libstdc++6 libglu1-mesa fonts-droid-fallback lib32stdc++6 python3
RUN apt-get clean

# download Flutter SDK from Flutter Github repo
RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter

# Set flutter environment path
ENV PATH=&quot;/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}&quot;

# Run flutter doctor
RUN flutter doctor

# Enable flutter web
RUN flutter channel master
RUN flutter upgrade
RUN flutter config --enable-web

# Copy files to container and build
RUN mkdir /app/
COPY . /app/
WORKDIR /app/
RUN flutter build web

# Record the exposed port
EXPOSE 5000

# make server startup script executable and start the web server
RUN [&quot;chmod&quot;, &quot;+x&quot;, &quot;/app/server/server.sh&quot;]

ENTRYPOINT [ &quot;/app/server/server.sh&quot;]
</code></pre>
<p>And:</p>
<pre><code>#!/bin/bash

# Set the port
PORT=5000

# Stop any program currently running on the set port
echo 'preparing port' $PORT '...'
fuser -k 5000/tcp

# switch directories
cd build/web/

# Start the server
echo 'Server starting on port' $PORT '...'
python3 -m http.server $PORT
</code></pre>
<p>I did all the steps and it seems it works fine but as long as I use <code>skaffold</code> I don't know how/where to put the following command to automate this step as well (I have already ran this command manually):</p>
<pre><code>docker run -i -p 8080:5000 -td flutter_docker
</code></pre>
<p><em><strong>I still like to know was the above files, proper/official way to doing that or there is a better way of it?</strong></em></p>
<p><code>EDIT</code>: I created the following <code>deployment &amp; service</code> file to put  the deploy the created image on <code>Kubernetes</code> local <code>Kind</code> cluster but when I run <code>kubectl get pods</code> I can not find this image but I find it by doing <code>docker images</code>. Why this happens and how can I put in on a <code>Kubernetes</code> pod instead of <code>docker</code> images?</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: client-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: client
  template:
    metadata:
      labels:
        app: client
    spec:
      containers:
        - name: client
          image: front
---
apiVersion: v1
kind: Service
metadata:
  name: client-srv
spec:
  selector:
    app: client
  ports:
    - name: client
      protocol: TCP
      port: 3000
      targetPort: 3000
</code></pre>

best_of_man
<p>The question (title) is misleading.
There are 2 parts.</p>
<ol>
<li>How to containerize the app (in this case flutter app).</li>
<li>How to deploy the app on the k8s cluster.</li>
</ol>
<p>To deal with the first part, You have Dockerfile. There is room for improvement but I think this Dockerfile should work. Then you need to build a container image. Please refer to the <a href="https://docs.docker.com/engine/reference/commandline/build/" rel="nofollow noreferrer">official documentation</a>. Finally, you need to push this created container image to some repository. (We may skip this pushing stage but to make things simple I am suggesting pushing the image)</p>
<p>For the second part, you should be familiar with basic Kubernetes concepts. You can run the container from a previously built container image with the help of the <a href="https://kubernetes.io/docs/concepts/workloads/pods/" rel="nofollow noreferrer">k8s Pod object</a>. To access the application, you need one more k8s object and that is the <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Service</a> (Load balancer or Node port type).</p>
<p>I know things are a bit complex (at initial levels) but please follow a good course/book I have gone through the blog post you shared, and this talks only about the first part and not the second part. You will have a container image at the end of this blog post.</p>
<p>I suggest going through the free playground offered by <a href="https://killercoda.com/killer-shell-ckad/" rel="nofollow noreferrer">killer shell</a>, if you don't want to set up a k8s cluster on your own, that is again another learning curve.  Skip the first tile on this page this is just a playground, but from the second tile, they have enough material.</p>
<p><strong>Improvements for Edited Question:</strong></p>
<ul>
<li>server.sh: maintaining a startup script is quite standard practice if you have complex logic to start the process. We can skip this file but in that case, a few steps will be added to Dockerfile.</li>
<li><code>kubectl get pods</code> does not show you images but it will show you running pods in the cluster (in default namespace). Not sure how you ran and connected to the cluster. But try to add output of the command.</li>
<li>few pointers to impve dockerfile:
<ul>
<li>Use a small base image footprint. Ubuntu: xx has many packages pre-installed, maybe you don't need all of them. Ubuntu has slim images also or try to find a flutter image.</li>
<li>Try to reduce Run statements. you can club 2-3 commands in one. this will reduce layers in the image.</li>
<li>instead of <code>RUN git clone</code>, you should clone code before docker build and copy/add code in the container image. In this way, you can control which files you need to add to the image. You also don't require to have a git tool installed in the container image.</li>
<li><code>RUN [&quot;chmod&quot;, &quot;+x&quot;, &quot;/app/server/server.sh&quot;]</code> and <code>RUN mkdir </code> both statements are not needed at all if you write Dockerfile smartly.</li>
<li>Dockerfiles should be clean, crisp, and precise.</li>
</ul>
</li>
</ul>
<p>PS: Sorry but this is not a classroom section. I know this is a bit complex thing for beginners. But please try to learn from some good sources/books.</p>
