Docker Lifecycle Exercise

Explore published docker images at https:///hub.docker.com, pull and modify upstream Dockerfiles, build, run and push local images to a private repository.

  • Explore the differences between instructions, images and containers.

  • Visit https://hub.docker.com/_/nginx in your browser for official Nginx docker images

  • Select mainline to view latest release of Dockerfile in main branch

  • Pull upstream Git repository and navigate to mainline Dockerfile

  • git clone https://github.com/nginxinc/docker-nginx.git
  • cd docker-nginx/mainline/debian/

  • Open and examin Dockerfile

  • vim Dockerfile

  • Copy upstream docker image from Dockerfile

  • FROM debian:bookworm-slim

  • Familiarize yourself with Dockerfile best practices

  • Make a new branch of this repository

  • git checkout -b $YOUR_NAME

  • In a new directory create a fresh Dockerfile using base image from upstream Dockerfile.

  • mkdir docker
  • cd ./docker
  • echo 'FROM debian:bookworm-slim' > Dockerfile

  • Build docker image from new Dockerfile

  • docker build -t debian:local .

  • Create docker container from local image and enter container.

  • docker run -it debian:local bash

  • Install nginx and dependencies

  • apt update && apt upgrade -y
  • apt install -y nginx

  • Start Nginx service

  • nginx

  • Test Nginx from your workstation

  • curl localhost

  • Exit container

  • exit

  • relaunch container

  • docker run -it debian:local bash

  • Start Nginx service

  • nginx

  • Add commands to re-install nginx to Dockerfile

  • RUN apt update && apt upgrade -y
  • RUN apt install -y nginx

  • Add CMD line to bottom of Dockerfile to run nginx in container

  • CMD ["nginx"]

  • Build docker image from your new Dockerfile

  • docker build -t nginx:local .

  • relaunch container

  • docker run -d nginx:local

  • Test local container from your workstation

  • curl localhost

  • Push Dockerfile to git repository

  • git add docker/Dockerfile
  • git commit -m "Add Dockerfile"
  • git push

  • Save container to new image

  • docker tag nginx:local registry.gitlab.com/zillona-dojo/nginx:$YOUR_NAME

  • Authenticate to prvate registry

  • docker login registry.gitlab.com

  • Push container to private registry

  • docker push registry.gitlab.com/zillona-dojo/nginx:$YOUR_NAME

  • Clone website repository and run container from image registry mounting local web content

  • git clone https://gitlab.com/zillona-dojo/website.git
  • docker run -dv ${PWD}:/usr/share/nginx/html registry.gitlab.com/zillona-dojo/nginx:$YOUR_NAME