Skip to content

snadi-teaching/docker-exercise-no-db

Repository files navigation

Hello World Flask Application

This repo contains a simple hello world endpoint. You will practice basic Docker usage.

Learning Objectives

  • Practice deploying containerized applications
  • Understand how to map ports from the Docker container to the host machine
  • Use Docker Compose to simplify management of containers

Prior Knowledge:

  • Basic knowledge of Docker usage and containerization

Time Estimate: 8-10 minutes

Technical Prerequisites

  • python 3.10 or higher
  • Docker is already installed.

Exercise 1 -- Build with docker files and commands

  1. Let's first try building the image docker build -t flaskdocker . -- this builds an image called flaskdocker

  2. Run docker images. You should see the flaskdocker image you just built listed

  3. Let's now run the container docker run -d --name flaskdocker-container flaskdocker This runs the image we created in step 1 (called flaskdocker) in a container called flaskdocker-container. Note that the -d flag runs the container in the background.

  4. Run docker container ls to see the running image.

  5. Go to http://127.0.0.1:6969. You will find that the application is still not running. Why is that? The application is running on port 6969 on the docker container, not on the host machine. We want to map the port in the docker container to some port on the host machine so let's do the following:

    a. Stop the container `docker stop flaskdocker-container`
    
    b. Remove the container `docker rm flaskdocker-container`
    
    c. re-run the container but while mapping the ports  `docker run -d --name flaskdocker-container -p 6969:6969 flaskdocker`
    

Now go to http://127.0.0.1:6969 and you should see the application!

The format in step c indicates that we want to map port 6969 inside the docker container to port 6969 on the host machine.

Exercise 2 -- Build with docker compose

Great, but the previous series of commands is a bit tedious. Instead, we can create a single docker-compose.yml file that describes all the containers we need to build. Running docker compose up will trigger building the necessary images and containers and you can even do the port mapping inside the same file. Check the provided docker-compose.yml file to understand what is in it.

Let's first stop and remove the container we already have running:

`docker stop flaskdocker-container`

`docker rm flaskdocker-container`

And then we can simply call docker compose up -d (the -d runs it in the background)

Go to http://127.0.0.1:6969 and verify that the application is running.

You can stop the container through running docker compose down

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors