Docker
Flowchart

Step 1. Development
  • Download the sample code
    1. git clone https://github.com/lin-chen-Langley/count
  • Fetch Redis image to local docker daemon
    1. # docker login
    2. docker image pull redis
  • Run Redis image as a container
    1. docker run -d -p 6379:6379 redis

  • Run Count in local
    1. # install needed dependencies
    2.  
    3. # run app
    4. python app.py
    5.  
    6. # access app
    7. http://localhost:5000/
    Step 2. Image Creation
  • Create an Docker Hub account if you do not have one
    1. https://hub.docker.com/

  • Create a Dockerfile in your code folder
    1. # Dockerfile
    2. FROM python:3.7-alpine # build image from python:3.7
    3. WORKDIR /code # set up default directory
    4. ENV FLASK_APP=app.py
    5. ENV FLASK_RUN_HOST=0.0.0.0
    6. RUN apk add --no-cache gcc musl-dev linux-headers # install needed Linux tools
    7. COPY requirements.txt requirements.txt # copy a file from local to image
    8. RUN pip install -r requirements.txt # install dependencies of the app
    9. EXPOSE 5000
    10. COPY . . # copy all files in the current folder to image
    11. CMD ["python", "app.py"] # run the command when run the image as container

  • Create an image for Count
    1. docker build -t lchenlangley/count .
  • Push the image to a registry
    1. # docker login
    2. docker push lchenlangley/count
    Step 3. Orchestration

  • Create a compose file
    1. # compose.yaml
    2. version: "3.9" # version of Docker Compose
    3. services:
    4. webapp: # create a container for Count
    5. image: lchenlangley/count # load image from DockerHub
    6. ports:
    7. - 8000:5000 # port mapping
    8. environment:
    9. - MYREDIS_HOST=redisserver # pass ip of redisserver to webapp
    10. - FLASK_ENV=development
    11. redisserver: # create a container for redis
    12. image: "redis:alpine" # load image from DockerHub
  • Run the app
    1. # compose containers
    2. docker-compose up
    3.  
    4. # access app
    5. 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