HowTo:Docker

From Computer Science Wiki
Jump to navigation Jump to search

Introduction

This guide will walk you through creating a git repository which gives you a docker registry to host docker images, build a docker image, and push the image up to the registry. The image will be based on the official PHP docker image https://hub.docker.com/_/php , and a copy of Chatbot for College Students app installed https://github.com/AaravRajSIngh/Chatbot You will need access to workstation that has Docker installed. You can SSH to rlogin.cs.vt.edu and build docker images there. This example compliments the CS Launch walkthrough at HowTo:CS Launch which will walk you through hosting this docker image using Computer Science kubernetes cluster.

Create Repository

Computer offers two Gitlab instances: https://git.cs.vt.edu and https://version.cs.vt.edu git.cs.vt.edu is geared toward academic use, and version.cs.vt.edu is for departmental or research use. We will use git.cs.vt.edu for this example.

  • Log into https://git.cs.vt.edu using your CS username and password.
  • Click on Projects from the menu on the left.
  • Click on the New Project button.
  • Click on the Create blank project tile.
  • Fill in Project name field with a unique name for your docker image project. Example: chatbot
  • Fill in Project URL fields with your own sub path and project slug.
  • Select Visibility Level If your project is public then anyone on the Internet will be able to download and use your docker image given the correct URL. If your project is Private or Internal then to use your docker image you need to create and use a deployment token which will be discussed further in this walk through.
  • Click on the Create project button.
  • A docker image registry is automatically created with your git repository.
  • Check your docker registry:
    • Select Deploy->Container Registry from the menu on the left
    • Take note of the example docker commands it gives, this contains your Docker registry URL. Example: container.cs.vt.edu/carnold/chatbot
  • Adding files to this git repository is optional, however you might find it a useful place to store versioned copies of your Docker image creation files.

Create Docker Image

  • SSH to rlogin.cs.vt.edu See Howto::Access_rlogin_service Alternatively, you can use any workstation that has Docker installed.
  • Optional: Clone your git repository.
  • Use a text editor to create a new file called Dockerfile
    • Example: nano Dockerfile
    • Contents:
# Start from Apache based PHP image
FROM php:apache

# Make sure git command is installed
RUN apt-get update && \
    apt-get install vim git -y

# Install additional PHP modules
RUN docker-php-ext-install mysqli pdo pdo_mysql

# Download chatbot app
RUN cd /var/www/html && \
    git clone https://github.com/AaravRajSIngh/Chatbot.git . && \
    chmod 600 .git && \
    cp -f homepage.php index.php
  • Build the docker image and give it a tag for easy identification.
    • Example: docker build . -t test-chatbot
  • If successful, your image will be built and store locally but is not yet accessible from your registry.
  • Optional: You can run your docker image locally to make sure it works
    • Example: docker run -it test-chatbot /bin/bash
  • Optional: Commit and push your Dockerfile up to the git repository.
    • Example: git add --all
    • Example: git commit -m init
    • Example: git push origin main

Push Docker Image to Registry

At this point you should have a built and working docker image that is stored locally. You will need to push the image up to your docker registry to be able to access it from other hosts or clusters.

  • Log into your docker registry using the docker command You need to give the docker command the hostname of the registry. If you are using https://git.cs.vt.edu then the hostname is container.cs.vt.edu If you are using https://version.cs.vt.edu then the hostname is docker.cs.vt.edu
    • Example: docker login container.cs.vt.edu
  • Tag your docker image with your Docker Registry URL from earlier.
    • Example: docker tag test-chatbot container.cs.vt.edu/carnold/chatbot
  • Push the image to your Docker Registry URL
    • Example: docker push container.cs.vt.edu/carnold/chatbot
    • You can use tags to store multiple versions of your docker image by adding a :<tag> to the end of your Docker Registry URL. Example: container.cs.vt.edu/carnold/chatbot:latest
  • If successful, your docker image should be uploaded and ready to deploy.

Use a Token to Access Registry

If your git repository that hosts your Docker Registry is set to Internal or Private visibility, then you will need to create and use a deployment token to download the images.

  • Navigate to your git repository website such as https://git.cs.vt.edu/carnold/chatbot
  • Select Settings -> Access Tokens from the menu on the left.
  • Click on the Add new token button
  • Fill in Token name with a unique name for your access token. Take note of the name that you give this token, as it will be used as the username in kubernetes. Example: test-chatbot
  • The Expiration date is optional.
  • Check the box for read_registry
  • Click on the Create project access token button
  • It will display the contents of your access token, make a copy of the content as you will need to paste it into a secret resource in the kubernetes cluster.
  • Refer to HowTo:CS_Launch#Create_a_Custom_Web_Application on how to import this token into kubernetes for hosting your docker images.