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.
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.
- Create a new working directory for your project, and change it into it:
mkdir my_app
cd my_app- Create an
index.htmlfile for your web server to host:
echo "hello world" > index.htmlFeel 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.
- If not already done install nano (
sudo yum install nano -y), then create an emptyDockerfileand edit it with nano (or use vi/vim if you're a masochist).
touch dockerfile
nano dockerfile- 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/htmlOnce added, save your dockerfile with CTRL+O and quit nano with CTRL+X.
- 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 .- Verify your image with
docker images - Use your
myappimage to deploy a container.
docker run -d -p 8080:80 myapp-dis to run the containerdetachedi.e. in the background-pis to specify the external > internal port mapping to access the container.
- Use
docker psto view your running container - 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'
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
Networkon the left - For
Attached to:selectBridged 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.
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-toolsThis 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 is a utility which allows you to deploy and configure containers which are defined in yaml files.
- 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:roNOTE - in the original source file the port mapping was
8080:8080but this would conflict with the it-tools container, so it has been changed to8081. 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
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]