Skip to main content

what is a Dockerfile

DockerFile

This is the instructions on what the image should do when it's executed in a container, it contains instructions on how to run the app, commands to install dependencies, ETC.

Here's a generic example

\# syntax=docker/dockerfile:1

FROM BASEIMAGE

# install app dependencies

RUN TheseAreShellCommands\_WhateverYouTypeInTerminalCanBePutHere

RUN EG\_NPM\_INSTALL

# Copy app source code into

COPY LocationOfMySourceCode WhereIWantItInTheImage

# Environment variables

ENV FLASK\_APP=hello

#Our app will run on Port

EXPOSE PORT

#Our run command, EG: flask run --host 0.0.0.0 --port 8000

CMD \["flask", "run", "--host", "0.0.0.0", "--port", "8000"\]

Here's on for a simple flask App in python

First here's the python code:

from flask import Flask

app = Flask(\_\_name\_\_)

@app.route("/")

def hello():

return "Hello World!"

Here's it's corresponding Dockerfile

\# syntax=docker/dockerfile:1

FROM ubuntu:22.04 # This is the base image

# install app dependencies

RUN apt-get update && apt-get install -y python3 python3-pip

RUN pip install flask==3.0.\*

# install app

COPY hello.py /

# final configuration

ENV FLASK\_APP=hello

EXPOSE 8000

CMD \["flask", "run", "--host", "0.0.0.0", "--port", "8000"\]

Overall the project structure should look like this:

|_ hello.py

|_ dockerfile

NOTE: Dockerfile file names include (there is no extension): Dockerfile, dockerfile