Docker
Flowchart
Step 1. Development
Download the sample code
git clone https://github.com/lin-chen-Langley/count
Fetch Redis image to local docker daemon
# docker login
docker image pull redis
Run Redis image as a container
docker run -d -p 6379:6379 redis
Run Count in local
# install needed dependencies
# run app
python app.py
# access app
http://localhost:5000/
Step 2. Image Creation
Create an Docker Hub account if you do not have one
https://hub.docker.com/
Create a Dockerfile in your code folder
# Dockerfile
FROM python:3.7-alpine # build image from python:3.7
WORKDIR /code # set up default directory
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers # install needed Linux tools
COPY requirements.txt requirements.txt # copy a file from local to image
RUN pip install -r requirements.txt # install dependencies of the app
EXPOSE 5000
COPY . . # copy all files in the current folder to image
CMD ["python", "app.py"] # run the command when run the image as container
Create an image for Count
docker build -t lchenlangley/count .
Push the image to a registry
# docker login
docker push lchenlangley/count
Step 3. Orchestration
Create a compose file
# compose.yaml
version: "3.9" # version of Docker Compose
services:
webapp: # create a container for Count
image: lchenlangley/count # load image from DockerHub
ports:
- 8000:5000 # port mapping
environment:
- MYREDIS_HOST=redisserver # pass ip of redisserver to webapp
- FLASK_ENV=development
redisserver: # create a container for redis
image: "redis:alpine" # load image from DockerHub
Run the app
# compose containers
docker-compose up
# access app
http://localhost:8000/
Exercise
1. Run the app with docker-compose up
2. Update the app, change Hello World from Docker to Hello [Your Name]
3. Create an image for the updated code, the image name should be [your_dockerhub_username]/count
4. Push the built image to Docker Hub
5. Rerun the app