Step 2 Docker for Application Packaging - truongnhatbui/techtrends GitHub Wiki
Package a TechTrends web application using Docker capabilities. This section will involve the creation of a Docker image and pushing it to a public image registry, such as DockerHub.
Note: You will require a valid DockerHub account.
Set up your environment to create a Docker image for an application:
Step 1 - Clone the TechTrends repository using git
HTTPS
https://github.com/truongnhatbui/techtrends.git
SSH
[email protected]:truongnhatbui/techtrends.git
GITHUB CLI
gh repo clone truongnhatbui/techtrends
To run this application there are 2 steps required:
- Initialize the database by using the
python init_db.py
command. This will create or overwrite thedatabase.db
file that is used by the web application. - Run the TechTrends application by using the
python app.py
command. The application is running on port3111
and you can access it by querying thehttp://127.0.0.1:3111/
endpoint.
Once you can access the application through the local browser, the next steps are to package the application using Docker. Create the Docker image for the TechTrends web application and push it to DockerHub, considering the following requirements:
Dockerfile
- use the python base image
- set the working directory to /app
- make sure to copy all the files from the current directory to the container working directory (e.g. /app)
- to build the application, use go build -o techtrends command, where -o techtrends will create the binary of the application with the name techtrends
- the application should be accessible on port 6111
- and lastly, the command to start the container is to invoke the binary created earlier, which is ./techtrends
The following snippet showcases the Dockerfile for the application:
FROM python:3.8
LABEL maintainer="Truong Bui"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
# command to run on container start
CMD [ "python", "app.py" ]
Docker Image
- should have the name techtrends
- should have a valid tag, and a version with a major, minor, and patch included
- should be available in DockerHub, under your username e.g. buinhattruong/techtrends
# build the image
docker build -t techtrends .
Docker Container should be running on your local machine, by referencing the image from the DockerHub with a valid tag e.g. buinhattruong/techtrends:v1.0.0
To build tag and push the image to DockerHub, use the following commands:
# tag the image
docker tag go-helloworld pixelpotato/go-helloworld:v1.0.0
Note: You will need to use docker login to login into Docker before pushing images to DockerHub.
Login to DockerHub - To login into DockerHub, use the following command:
docker login
# push the image
docker push pixelpotato/go-helloworld:v1.0.0