CIS 527



Lab 5.A - Developing with Containers

Developing with Containers

  • Dockerfile
  • Docker Compose
  • Develop in Docker
  • CI/CD Pipelines

Dockerfile

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim
EXPOSE 5000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, 
# please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "wsgi:app"]

Docker Compose

services:
  mysql:
    image: mysql:5.7
    container_name: mysql
    restart: unless-stopped
    ports:
      - '3306:3306'
    environment:
      MYSQL_DATABASE: officehours
      MYSQL_USER: officehours
      MYSQL_PASSWORD: password
      MYSQL_RANDOM_ROOT_PASSWORD: fact
  phpmyadmin:
    image: phpmyadmin:latest
    container_name: phpmyadmin
    restart: unless-stopped
    ports:
      - '8080:80'
    environment:
      PMA_ARBITRARY: 1
	

Develop in Docker

Image Source: Visual Studio Code

CI/CD Pipelines - GitHub

name: Docker Image CI
jobs: build: runs-on: ubuntu-latest
steps: - uses: actions/checkout@v3
- name: Login to GitHub Container Registry uses: docker/login-action@v1 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push uses: docker/build-push-action@v2 with: push: true tags: | ghcr.io/${{ github.repository_owner }}/ksucs-hugo:latest

CI/CD Pipelines - GitLab

image: docker:20.10.11
variables: DOCKER_HOST: tcp://docker:2375 DOCKER_TLS_CERTDIR: "" GIT_SUBMODULE_STRATEGY: recursive
services: - docker:20.10.11-dind
before_script: - docker info - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build-latest: stage: build only: - master - main script: - docker pull $CI_REGISTRY_IMAGE:latest || true - > docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest . - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA - docker push $CI_REGISTRY_IMAGE:latest