emilywoods / pyladies-docker-workshop

a hands-on introduction to docker 🐳

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PyLadies Docker Workshop

Welcome everyone! 🐍 🌈

The purpose of this workshop is to provide an interactive introduction to Docker.

This repository contains a configurable Hello World web server application. The goal of this exercise is to containerize the application and run it as an image.

Prerequisites

  • Docker
  • Git

Instructions

The application code is located within the /helloworld directory. This is a simple Flask application which takes in one environment variable, NAME and runs a web server on port 5000.

Install and run the application

Get started with the usual approach to installing and running the application. We can later compare this approach with how you would run the application with Docker.

Navigate to http://localhost:5000/

Complete the Dockerfile

An empty Dockerfile has been provided has been provided, to complete this:

  1. Choose a base image
  2. Add your project files
  3. Install project with pip
  4. Configure the ENTRYPOINT to run the project

Run your container

  1. Build the container with a tag, replacing <IMAGE-TAG> with hello-world:latest:

    $ docker build . -t <IMAGE-TAG>

Once it's built, you can show your built images with:

$ docker images
  1. Run the container. --publish maps a port from within the container to a port on your local machine, and -env defines an environment variable that will be present in your container's environment:

    $ docker run --publish 5000:5000 --env NAME=<YOUR NAME> <IMAGE-TAG>
  2. Go to https://localhost:5000
  3. List the running docker processes, and take note of your container's ID:

    $ docker ps
  4. View the logs of the container:

    $ docker logs <CONTAINER-ID>
  5. Connect to the container:

    $ docker exec -it <CONTAINER-ID> sh

From within the container you can show the environment variables:

$ echo $NAME
  1. Stop the container:

    $ docker stop <CONTAINER-ID>

Image Optimisation

If you used the python:3.7 base image, you will see that the image size is ~900MB when you run:

$ docker images

This is quite a large image, for an application of this size! The official Python base images have slim variants with fewer system libraries installed, such as python:3.7-slim. Does using this base image have an impact on your image size?

Docker Compose

Docker Compose is a tool which can be used to define and run multiple docker containers. An empty docker-compose file has been provided for you to fill in.

You can install it from here.

Complete the docker-compose file

Note: if you use Linux, you will need to install this separately from Docker, you can find install instructions here. For MacOS and Windows, it will already be installed.

Provide the version of Compose to use:

version: "3"

Define the container, giving it a name hello-world, and providing an image tag with version latest to use:

services:
  hello-world:
    image: hello-world:latest

Map port 5000 on the container to port 5000 on the host machine:

ports:
  - "5000:5000"

Define your environment variable:

environment:
  NAME: <YOUR NAME>

Use docker-compose!

From within the root directory of the repository, run:

$ docker-compose up -d

To stop:

$ docker-compose down

Further learning

Well done! You've just:

  • Made a Dockerfile
  • Built, run and explored the docker container
  • Used Docker Compose

That's a lot to take in! What's next?

If you want to learn more about Docker and containers, we recommend:

About

a hands-on introduction to docker 🐳

License:MIT License


Languages

Language:CSS 41.6%Language:HTML 34.8%Language:Python 23.5%