This repo contains a simple hello world endpoint. You will practice basic Docker usage.
- 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
- Basic knowledge of Docker usage and containerization
- python 3.10 or higher
- Docker is already installed.
-
Let's first try building the image
docker build -t flaskdocker .-- this builds an image called flaskdocker -
Run
docker images. You should see the flaskdocker image you just built listed -
Let's now run the container
docker run -d --name flaskdocker-container flaskdockerThis 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. -
Run
docker container lsto see the running image. -
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.
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