All-Things-Docker-and-Kubernetes

Lab 004: Serving a Golang Application through Docker

Pre-requisites

Introduction

In this lab, we’ll be deploying a website written in Go(golang) on a container.

Create the Files

Install Git.

$ sudo yum -y install git 

Clone the repository to your machine.

$ git clone https://github.com/cloudacademy/flask-content-advisor.git
$ cd flask-content-advisor 

For our website to work, it needs some packages installed. These packages are defined in the requirements.txt.

flask == 1.1.4
markupsafe==2.0.1 

Let’s now create the dockerfile.

$ vim Dockerfile 
# Python v3 base layer
FROM python:3

# Set the working directory in the image's file system
WORKDIR /usr/src/app

# Copy everything in the host working directory to the container's directory
COPY . .

# Install code dependencies in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Indicate that the server will be listening on port 5000
EXPOSE 5000

# Set the default command to run the app
CMD [ "python", "src/app.py" ] 

Build the Image

Build the image from the Dockerfile.

$ docker build -t flask-content-advisor:latest . 

Get the IP of you machine. We will need this later.

$ curl ipecho.net/plain; echo 

Run the Container

Finally, run the container from the image.

$ docker run --name advisor -p 80:5000 flask-content-advisor 

Open an internet browser and navigate to the IP that you just saved.

Cleanup

When you’re done with the lab, you can stop all running containers by running the command below.

$ docker stop $(docker ps) 

Once all containers have “Exited” status, remove them.

$ docker ps  -a 
$ docker container prune -f 

Finally, remove all images.

$ docker image prune -af