Skip to content

The GPL Docker

Openweb edited this page Dec 6, 2020 · 3 revisions

Building Docker Images

This projects builds docker images on alpine OS. The images are on gcr.io/the-gpl in Google Container Registries. The Alpine from Google it is lightest and fastest golang docker environment.

These are build it two ways:

  1. Using GCP cloud build as part of CICD pipeline.
  2. From Mac command line

Docker: GCP Cloud Build

The git repo is configured to trigger Cloud Build when git push origin master is executed. The build steps are outlines in cloudbuild.yaml. It works along with Dockerfile.

The cloudbuild.yaml and Dockerfile is used by Google Cloud Build to create container image tag and stores it to gcr.io/the-gpl/book.

Docker: Local Command Line

The Mac commands to create docker image on local laptop are:

  1. GCloud container images
  2. Docker Hub repository

GCloud container images

From your laptop is gcloud command line to build the image.

# Build GCP  container image using Cloud Build
gcloud builds submit --tag gcr.io/the-gpl/gpl:v1 
gcloud container images list --repository=gcr.io/the-gpl # Show your container registries

Docker Hub repository

From your laptop is docker command line to build the image.

# Build on local docker 
docker build -t the-gpl:v1 . # Build version v1
docker image ls  # See docker images
docker history the-gpl:v1  # See image layers
docker image inspect the-gpl:v1

Deploying Docker Images

The images are deployed on:

  1. GCR.io i.e. Google Cloud Registry, and
  2. Docker hub.

GCR Google Cloud Repository

The cloudbuild.yaml contains deployment instructions to GCP cloud. To deploy as Cloud Run service use the GCP console to create service for docker image. You can use these gcloud commands.

gcloud auth login # Ensure you are logged to your GCP cloud project account
gcloud config set project the-gpl # Make sure you are in right project 
gcloud run deploy --image gcr.io/the-gpl/gpl:v4 --platform managed  --allow-unauthenticated # Deploy

Docker repository

The image is also on docker repository. In order to push images to docker repo commit it, add tag and push. e.g:

docker tag the-gpl:v1 uopendocker/the-gpl:v1
docker push uopendocker/the-gpl:v1

Your local docker

You can build a image with specific tag and run it in local docker container.

docker build -t the-gpl:v1 . # Build version v1
docker image ls  # See docker images

Handy Docker Commands

These commands will come handy to use docker in your environment.

Docker Run

Use the --rm to run docker images.

# Sample cloud run examples, remove container after run
docker run --rm the-gpl:v1 ./the-gpl mas -fn=array
docker run --rm the-gpl:v1 ./the-gpl mas -fn=slice
docker run --rm the-gpl:v1 ./the-gpl mas -fn=comp -n1=123 -n2=-1234
docker run --rm the-gpl:v1 ./the-gpl fetch -site=http://www.google.com -site=http=//www.facebook.com
docker run -d -p 8080:8080 the-gpl:v1 ./the-gpl server -port=8080

Docker Container

Here are docker container commands.

docker container ls  # See all your containers
docker container ps -a # Your docker process
docker container prune # Remove stopped containers
docker stop 58a639eecb9a # '58a639eecb9a' is the container id of image "the-gpl"
dcoker rm 58a639eecb9a # Remove container with id '58a639eecb9a'

Docker images

Make sure to prune your containers and images:

docker image rm image:version # Remove unused images
docker container prune
docker image prune

Mounting Docker Volumes

You can create your own local log file by mounting it to docker container.

# If you like to log to a file.
export DEV_LOGS="~/Logs/DevLogs/"
docker run -d -p 8080:8080 -v $DEV_LOGS:/app/logs the-gpl