In the , a Dockerfile is a sort of recipe: it contains all the instructions to collect the ingredients (the image) that will make the cake (the container).
But what exactly can a Dockerfile contain? We will see, in our example (that you can find , a popular framework to build elegant and beautiful frontend for AI/ML python apps.
Our folder will look like this:
build_an_image_1/
|__ app.py
|__ Dockerfile
To fill up app.py, we will use a template that , we need to expose port 7860:
EXPOSE 7860
Now we can make our application run:
ENTRYPOINT ["python3"]
CMD ["/app/app.py"]
We will not be able to change the base executable (python3) but we will be able to override the CMD instance specifying another path at runtime (for example if we mount a volume while running the container).
4. Full Dockerfile
Our full Dockerfile will look like this:
ARG PYTHON_VERSION="3.11.9"
FROM python:${PYTHON_VERSION}
WORKDIR /app/
COPY ./app.py /app/
ARG GRADIO_V="5.4.0"
ENV GRADIO_VERSION=${GRADIO_V}
RUN python3 -m pip cache purge
RUN python3 -m pip install gradio==${GRADIO_VERSION}
EXPOSE 7860
ENTRYPOINT ["python3"]
CMD ["/app/app.py"]
Now we just need to build the image!
Build and push the image
When we build the image, we need to specify the context, meaning the directory in which our Dockerfile is. For starters, we will also use the -t flag, which specifies the name and tag of our image:
docker build . -t YOUR-USERNAME/gradio-echo-bot:0.0.0 -t YOUR-USERNAME/gradio-echo-bot:latest
As you can see, you can specify multiple tags.
This build, once launched, will take some minutes to complete, and then you will have your images locally!
If you want to make this images available to everyone, you need to login to your Docker account:
docker login -u YOUR-USERNAME --password-stdin
You will be prompted to input the password from your console.
You won't put your Docker password, but an access token (follow the link for a guide on how to obtain it).
Now let's push our image to the Docker Hub registry:
docker push YOUR-USERNAME/gradio-echo-bot:0.0.0
docker push YOUR-USERNAME/gradio-echo-bot:latest
The push generally takes some time, but after that our image will be live on Docker Hub: we published our first Docker image!🎉
We will stop here for this article, but in the next one we will dive into more advanced build concepts🥰
SOCIAL SHARE CARD GENERATOR