After understanding the concept of Docker, let’s now get into implementation. There are several steps we will do:
Install Docker
- Create a file called Dockerfile
- Build
- Run the image Install
the image
Docker
The first step we can do is install the Docker
application. You can download Docker here and make sure you choose the right one based on your operating system.
After installing Docker, let’s now test if Docker is already installed on your computer. You can run the ‘docker’ command like this preview below:
If your computer
shows a message like above, we can assure you that Docker is already installed on your computer. Now let’s move on to the next step.
Create
Dockerfile
After installing Docker, the next step is to create a file named Dockerfile. Dockerfile is a script for creating a Docker image. It consists of instructions to configure the image ranging from the installation of libraries to the execution of our project.
In this case, I created an API based on the Flask library. This API will be used as a connector between the user and the machine learning model to return a prediction result. Here’s the API code:
To create the Dockerfile, make sure the file is in the same location as the machine learning model app and folder. In my case, the folder location looks like this:
| app.py| Dockerfile| Requirements.txt|+-resnet50_food_model| | saved_model.pb| || +-assets| +-variables| variables.data-00000-of-00001| variables.index Inside the folder,
we have app.py to run my application, requirements.txt which contains the list of packages we will use, the model folder consisting of the previously trained model, and the Dockerfile to create a Docker image
.
Remember that when you create the Dockerfile, make sure you don’t give any extensions after the file name. Just type ‘Dockerfile’.
Now let’s write our Dockerfile script. Here is the script inside the Dockerfile
:
Let me explain each line of the script:
The ‘FROM’ instruction will take a docker image of a docker record. The image will contain the operating system and also the Python.
The ‘WORKDIR’ statement will set the default working directory that we will use to run the application.
The ‘COPY’ statement will copy each file within the current directory to the new working directory.
The ‘RUN’ statement will execute a command. In this case, the command ‘pip install’.
The ‘EXPOSE’ instruction will expose port 5000 as a connector to the docker image.
Finally, the ‘CMD’ statement will execute a command. In this case, the command ‘flask run’.
Side note: The ‘CMD’ statement has the same function as the ‘RUN’ statement,
but they are different depending on their purpose. The ‘CMD’ statement
is used to execute a command when the docker image execution starts.
Meanwhile, the ‘RUN’ instruction is used to execute a command while you are building the Docker image.
Create
the Docker image After creating the
Docker image, the next step is to build the image. To build the image, it’s really simple. All you need to do is run
the ‘docker build’ command like this: docker build -t my-image.
As you can see above, I give the ‘-t’ label next to the ‘docker build’ command. The ‘-t’ tag takes two arguments. They are the name of the image and also the dot character, which represents all the files within the folder.
Here’s the preview when we’re building the docker image (it will take a lot of time, so be patient): Let’s
run the ‘docker images’ command to display existing images that are already built. Here is the preview of the result:
Running the
Docker
image
As you can see from above, the image already exists. Now the next step is to run the docker image on our computer. To run the image, you can use the ‘docker run’ command like this, docker run
-dp 5000:5000 my-image
Let’s wait a moment of 3 to 5 minutes. To know if our image is already running, we can use the command ‘docker ps-a’ to check all running images. Here’s the preview:
As you can see from above, the image is already in about a minute. It means that the image is already running on port 5000. Now let’s access the image using localhost:5000 or 127.0.0.1:5000. Here is the preview:
As you can see from above, our container has successfully run on our computer. Because you set the parameter ‘-d’, you cannot display the container log.
To check activities in a container, you can use ‘docker logs’ to do so. Also, set the container ID next to it. You can view the container ID from the command ‘docker ps -a’. The
command looks like this:docker logs bff6d3c8e2da Here’s
what the log preview looks like:
If you want to stop the container, you can use the ‘docker kill’ command and set the container ID next to it. Here
is the command to stop the docker command: docker kill bff6d3c8e2da
After stopping the container, it will look like this
,
Yes, our container is already finished.