Skip to content

Generation-UK-I/Docker-Engine-Demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Docker Engine Demo

This module should be shared upon completion of the accompanying slide deck.

Note: This module builds upon the Generation's bootcamps which incorporate a Linux module utilising a CentOS virtual machine. If you do not have this VM available because you followed a different curriculum, find installation instructions here.

Creating and Running a Container from a Dockerfile

Once you have installed Docker Engine in your CentOS VM you can follow the below instructions to create, run, and access an NginX web server running in a container.

  1. Create a new working directory for your project, and change it into it:
mkdir my_app
cd my_app
  1. Create an index.html file for your web server to host:
echo "hello world" > index.html

Feel free to add different text to your file, but don't write any complex HTML for now, because we're only going to access it from the CLI.

  1. If not already done install nano (sudo yum install nano -y), then create an empty Dockerfile and edit it with nano (or use vi/vim if you're a masochist).
touch dockerfile
nano dockerfile
  1. Add the following two lines to the dockerfile instructing Docker to use the standard NginX container image from DockerHub, and copy your HTML file to it.
FROM nginx
COPY index.html /usr/share/nginx/html

Once added, save your dockerfile with CTRL+O and quit nano with CTRL+X.

  1. If it's not already running, start the Docker service, and then build your container image.
sudo systemctl start docker.service
sudo docker build -t myapp .
  1. Verify your image with docker images
  2. Use your myapp image to deploy a container.
docker run -d -p 8080:80 myapp
  • -d is to run the container detached i.e. in the background
  • -p is to specify the external > internal port mapping to access the container.
  1. Use docker ps to view your running container
  2. In the absense of a web browser use the cURL tool to retrieve resources from a web address (URL) curl http://localhost:8080

Challenge: Can you change the output of the webserver without rebuilding your image? Hint: 'docker exec'

Deploying an Existing App

In the previous task you created your own container image, launched a container from it, and accessed the container from within the VM. In the next section we're going to deploy some open source app's, which we'll access from the host computer over your LAN. The easiest way to facilitate this is by changing the VirtualBox Network settings for your VM.

Follow these steps to change the relevant setting:

  • Power your VM down
  • Right click your VM and select settings
  • Select Network on the left
  • For Attached to: select Bridged adapter
  • Click OK and take a new snapshot
  • Start up your VM
  • NOTE - if you restore to an earlier snapshot the settings also revert.

Previously we've connected to the VM with ssh centos@localhost but this will not work with the new network configuration. This is because Bridged Adapter maps your VM to your physical network adapter and presents it to the LAN as a unique computer, and it requests an IP address from your DHCP server.

In order to connect to the VM with Bridged Adapter selected we need to type ssh centos@[IP address], so you need to find the IP. The easiest way is to login directly through the 'virtual screen', type ip a and note down the IP address, then log out with exit and go back to using your terminal and ssh.

Running a Container

You've already used docker run to launch your own container, we can also use this command to deploy a publicly available, containerised open source apps from Docker Hub.

Run the following command to deploy the it-tools container.

docker run -d -p 8080:80 --name it-tools -it corentinth/it-tools

This command pulls the corentinth/it-tools image, launches a container called it-tools from it, -p exposes port 8080 to the LAN, and maps it to port 80 inside the container.

If successful at the bottom of the terminal output you should see a container ID returned, something like fccfc15e674540fa3600590d96bb62ff383e77b34ee78daae9f1a2aec3cfe6ae.

You can access the IT-Tools application through your web browser by going to http://[virtual_machine_IP]:8080

Docker Compose

Docker compose is a utility which allows you to deploy and configure containers which are defined in yaml files.

Create a Docker Compose file

  • Create a directory for your containerised app, and move into it.
  • Create a file called docker-compose.yaml
  • In this file add the following:
# Running `docker-compose up` will create/use the "trilium-data" directory in the user home
# Run `TRILIUM_DATA_DIR=/path/of/your/choice docker-compose up` to set a different directory
# To run in the background, use `docker-compose up -d`
services:
  trilium:
    # Optionally, replace `latest` with a version tag like `v0.90.3`
    # Using `latest` may cause unintended updates to the container
    image: triliumnext/trilium:latest
    # Restart the container unless it was stopped by the user
    restart: unless-stopped
    environment:
      - TRILIUM_DATA_DIR=/home/node/trilium-data
    ports:
      # By default, Trilium will be available at http://localhost:8080
      # It will also be accessible at http://<host-ip>:8080
      # You might want to limit this with something like Docker Networks, reverse proxies, or firewall rules,
      # however be aware that using UFW is known to not work with default Docker installations, see:
      # https://docs.docker.com/engine/network/packet-filtering-firewalls/#docker-and-ufw
      - '8081:8080'
    volumes:
      # Unless TRILIUM_DATA_DIR is set, the data will be stored in the "trilium-data" directory in the home directory.
      # This can also be changed with by replacing the line below with `- /path/of/your/choice:/home/node/trilium-data
      - ${TRILIUM_DATA_DIR:-~/trilium-data}:/home/node/trilium-data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro

SOURCE

NOTE - in the original source file the port mapping was 8080:8080 but this would conflict with the it-tools container, so it has been changed to 8081. This is something to bare in mind as you deploy more and more containers.

  • Save and close the file
  • Type docker compose up -d

If successful you will see the latest image being pulled from Docker Hub, a network being created within Docker, and the container being started. Again, if successful you can access the app at http://[virtual_machine_IP]:8081

Stretch and Challenge

Create a container to run your own Python code.

Here is a starter Dockerfile:

# Use the official Python image
FROM python:3.9

# Set the working directory in the container
WORKDIR /app

# Copy the script to the container
COPY script.py .

# Run the Python script
CMD ["python", "script.py"]

Hint: docker run --rm [container]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published