./
└── myfolder
├── newmodel_prediction.py
├── requirements.txt
├── Dockerfile
├── .gitignore └── .dockerignore
How to build 🐳 Docker Image & run Container
docker
Step by step guide on how to build docker image on VSCode and run docker container on Docker Desktop App
It is seldom that I needed to use docker to validate pre-production code, but when I do, I find myself always having to retrace my steps trying to learn how to use docker again. So here is step by step guide to remind myself:
1. Build docker image
- Open VSCode window, choose “Open Folder”.
- Choose the directory where we store the pre-production code we want to test. Eg “myfolder” here.
- If not already exist, add .dockerignore. If the file we want to ignore is not in ‘./myfolder’ directory, we need to include full filepath.
# .dockerignore
Dockerfilebin
include
lib
venv .env
- If not already exist, create .gitignore. Fill in any filenames that you want to exclude. If the file we want to ignore is not in ‘./myfolder’ directory, we need to include full filepath.
# .gitignore
.envbin
lib
include
share .ipynb_checkpoints
- If not already exist, create requirements.txt. We can automatically create one using terminal. In terminal:
# make sure we have pipreqs installed, if not install using:
!pip install pipreqs
# create requirements.txt in the parent directory
!pipreqs .
- Create Dockerfile. Currently, below is the minimal working dockerfile we can use to test any pre-production code. The last line
CMD ["python3.8", "./newmodel_prediction.py"]
means as soon as the container is running, runpython 3.8 ./newmodel_prediction.py
.
# Dockerfile
# enter what specific python image we want to work with
3.8.16
FROM python:
-get install libgomp1
RUN apt
/usr/local/bin
WORKDIR /
ADD . .
-m pip install -r requirements.txt
RUN python
# what command we want to run as soon as the container is running
"python3.8", "./newmodel_prediction.py"] CMD [
- Open up docker desktop apps.
- Build docker image. The easiest way to create an image in VSCode:
- Open up Dockerfile
- In Dockerfile, right-click and select “Build Image…”
- Look notice the “myfolder:1” in the command pallete. “myfolder” is there because it is the name of our current directory, this will become the default image name. “1” here is the default image tag. We can change both the image name and the tag if we wish, or just press enter to use the default.
- If there is no error, your terminal should look similar like so.
- Observe that we have newly created image in our docker desktop app.
2. Create docker container & run
- Click on the newly created image.
- Set container name (optional). Add any environment variables if needed. The environment variables you included here will be used to run the container. (See docker documentation for environment variables precedence)
- You will be directed to container tab. Notice the container name “testrun1”, image name and tag “myfolder:1”.You can confirm that the container is currently running when the play button ▶️ on the right hand corner is grayed out. You can stop the container by clicking on the stop button ⏹. Since we created our ended our Dockerfile with
CMD ["python3.8", "./newmodel_prediction.py"]
, this means it will directly run the specified file. However, we could also manually run anything through the terminal should we wish, like so.
Note: Docker containers and images take up space. You might want to delete them if unused.