How to Build, Run and Upload your Docker image on Docker Hub
Developing applications in our days requires much more than writing code. There are so many languages, frameworks, interfaces and tools to create one application which leads to large complexity. And how can someone deploy these environments in any machine? This is where Docker comes into play!
The approach of building an image is similar to building a virtual machine. First of all you need to decide upon the OS of the image. In windows, the user can select whether wanting to build a Linux or a Windows image. In this tutorial we will proceed with building a Linux image.
In order to explain further what an image/container is, we can imagine an image as a virtual box. The image contains every information and data that is needed for a specific application to run, regardless the OS that the container is operating.
‘’A Docker container is an open source software development platform. Its main benefit is to package applications in containers, allowing them to be portable to any system running a Linux or Windows operating system (OS). A Windows machine can run Linux containers by using a virtual machine (VM)’’.
Proof of Concept
In this scope we have implemented an application that has a trained Mask RCNN model in order to characterize metal material powders that are used for Additive Manufacturing. Our implementation uses the Matterport Mask RCNN implementation [https://github.com/matterport/Mask_RCNN/wiki] along with several changes to match our dataset and the scope of this problem.
Step 1: Structure
First of all, the user must create a fine structure for the project. The project skeleton should include the name of the project along with all the data, code, requirements that are going to be used to build the Docker file.
a) The requirements.txt is crucial that is in the correct format. In order to verify this, try and create a Python Virtual environment and run the following command.
(We use the anaconda prompt to test the following procedure).
$ (base) conda create --name test_environment python==3.7
$ (base) conda activate test_environment#Copy the requirements.txt in the directory you are currently working
$ (test_environment)pip install -r requirements.txt
Note: If you don’t have a requirements.txt you can type in the directory of your source code:
$ pip freeze > requirements.txt
This will write all the requirements that are installed in your environment in the requirements.txt in the directory you are located. Follow the above guidelines to install all dependencies.
If this step runs smoothly, we can move on to creating the Dockerfile. Docker will search for a file named Dockerfile within the directory you will build the image.
Step 2: Create the Dockerfile
Create a file named Dockerfile (txt but without .txt in the end or else it will not work!!!)
You can name your dockerfile at your own taste…
For more info follow the official Docker documentation here and search for the — file argument here
The following is the Dockerfile:
# FROM selects the image to find on docker hub and selects the tag specified. Here we select the python image and the 3.7 version (as tag)
FROM python:3.7 # or else the Python version you want# this is the work directoryWORKDIR /user/src/appRUN apt-get updateRUN apt install -y libgl1-mesa-glx# Copy the requirements.txtCOPY ./requirements.txt /user/src/app/requirements.txt# Run the pip - installRUN pip install -r requirements.txt# Expose the port that the container will communicateEXPOSE 5000# Copy source code and everythingCOPY . /user/src/appCMD [ "python", "main.py" ]
For the Dockerfile to build smoothly without errors, there are some rules that need to be followed. Dockerfile is in general expecting a command name and a set of arguments. These are reserved keywords (such as CMD, FROM).
Let’s go through the Dockefile arguments…
- The WORKDIR specifies the working directory within the container. This is the location where the source code, etc will be placed.
- The next step is to RUN the updates on your linux container. Also we run a command that installs some dependencies concerning the server that we will be using.
- COPY command will copy the requirements.txt from our current (windows) directory in the WORKDIR (usr/src/app/). The next step is to RUN the pip install command which will install all the dependencies found within the requirements.txt.
- EXPOSE is a very crucial command in the Docker file. As the discussion is based on containers, the question comes on how it is possible to communicate with this container. In order for the container to communicate with the “outer world” it is crucial that we define which PORT is left open. By exposing a port we will be able to interact with the running container.
- COPY will copy all files and directories from our local directory to the WORKDIR, or else our container will not include all the files we want.
- CMD (Command Line) will execute the command specified in the brackets (ex. here python main.py)
This is it! You have created your first Docker file.
Step 3: Build your image
Now let’s get into details on how to build the image!
Locate yourself within the directory you have created the docker file.
In powershell or CMD (Windows) or by typing the following on any command line (linux or MacOS):
docker build --tag <tag_you_want>:<tag> .
If <tag> is ommited, then latest will be applied as default.
NOTE: if you get the following error ‘failed to solve with frontend dockerfile.v0: failed to create LLB definition: no build stage in current context’, then set the following 2 variables to to disable docker buildkit and to use the Compose python client for building images instead of the native docker CLI (using your command line):
(IN LINUX use export instead of SET)
Also keep in mind that Docker keeps a local cache memory for image it is trying to build. By using the argument –no-cache while building, Docker will not track the existing build if it fails and therefore will start from scratch when rebuilding.
Docker will build the image with the name <tag_you_want> and the “.” Specifies that the Dockerfile is found within the same directory you are running this command.
As soon as the docker builds your image, you can verify the existence of your image by typing:
# Display all the images in your machine
Step 4: Run the container
To run the container there are two possibilities:
- Run the container using the Docker Desktop
a. Start Docker Desktop.
b. Find the Container/Image you want to start.
c. Make sure to select the Port you would like your local application to communicate with the exposed 5000 port via TCP (default) on the running Container.
d. Click Run aaaand magic! You app is working!
Verify that your container is running:
For our application, we run the app on the container localhost on port 8000 . So, if we type: http://localhost:8000/ on a web platform (Chrome etc) we can see this:
2. Run the container from the command line
If you are using linux or if you want to run your container while using the command line follow these instructions…
docker run -p 8000:5000 mask_rcnn_app_dfile
-p specifies the ports that the docker file will run. The 8000 in the 1st position specifies the local port while the 5000 in the 2nd position is the container port. In this way the container can communicate with the local machine.
Application name: mask_rcnn_app_dfile or the name you want to add.
If we type: http://localhost:8000/ on a web platform (Chrome etc), Once again the user will see the same results.
The user can also interact with the container using the CLI module found next to the running instance. By typing ls the user can see the files within the container or check out the running processes by typing ps -ef.
Push to the Docker Hub Repository
To push the image repo to Docker Hub in 2 different ways (using command line, using docker desktop):
- If you don’t have a Docker account, head to https://hub.docker.com to create one (your docker account will be the docker username you will select during the sign up).
- Note: You need to tag the image name as shown using the CMD:
$ docker image tag <your_image_name> <user_name>/<your_image_name>
Note: Only and only if the image name matches the following convention: <user_name>/<your_image_name> you will be able to push in the Docker Hub repository!
1. Using the command line:
$ docker login// fill in your username and password
// If done correctly will get message:
Login succeeded$ docker image tag <image_name> <new_image_name>The <new_image_name> must be the same with the repo you are going to upload.
For example:$ docker image tag my_app_test user_name/my_app_testThis will create a new image in your local docker repo that will be named accordingly or else docker will not upload the image!$ docker image push user_name/my_app_test
2. Using the Docker Desktop (Windows only)
Open the docker Desktop application
Create an account, if you are a new user here: https://hub.docker.com/signup
Go back to Docker Desktop and click Log in at the top right of your screen.
Push to Hub!