You need to bring your own computer to the practical.
Make sure you have Docker installed you can execute following command:
docker run hello-world
In addition, please run following command before attending the practical.
# Ubuntu image
docker pull ubuntu:noble
# Apache
docker pull httpd:2.4.57
# NodeJS
docker pull node:21.7.2-slim
# Nginx
docker pull nginx:1.25.4
docker run hello-world
# Create a container from an image, does not run the container!
# We may need to add a random command at the end.
docker create --name="{containername}" image:tag
# Export file system from the container.
docker export {container identifier} -o dump.tar
docker run -it ubuntu:noble bash
docker exec -it {container} bash
docker ps
docker kill {container}
Sometimes we need persistent storage across containers. The solution is to save data outside of the container.
Compare Named Volumes / Bind Mounts
What about mapping Windows directory to Docker? It is possible via WSL. However not all functionality is available and there may be performance issues.
# :(
npm run dev
Start with ubuntu:noble and create custom image named server-php:latest with PHP and Apache.
Do not try this!
How to install and configure PHP.
# Update package index.
apt-get update
# Install packages.
apt install php php-cli
# ...
Now we can pause the image and create a container.
# Pause the image.
docker pause {container}
# Create a container.
docker commit {container} server-php:latest
This probably takes a while ...
<!doctype html><title>Title</title><p>Content</p>
# PowerShell
docker run -it --rm -d -p 8080:80 -v "${PWD}:/usr/local/apache2/htdocs/" httpd:2.4.57
# Bash
docker run -it --rm -d -p 8080:80 -v ${PWD}:/usr/local/apache2/htdocs/ httpd:2.4.57
docker kill $(docker ps -q)
A better way to create and share definition of a Docker image.
The are a number of commands documentation,
as well as best-practices.
We need only a few basic commands:
More commands ...
./practical-09/
There is a web application which can be used as a simple client for your 04-php-orm server application. Your task is to add a "production-grade" Docker deployment. Use nginx:1.25.4 container to host the application.
Follow the steps to get to the reasonable Dockerfile.
Beware of potential pitfalls!
You are allowed to modify the application, but try to keep the changes minimal.
You need to be able build and run the Dockerfile using following commands in the "./practical-09/" directory.
docker build -t nswi153/frontend .
docker run -p 8080:80 nswi153/frontend
Start by downloading and unpacking a web application. Once you familiarize yourself with the application, your task is to create a Dockerfile.
Test the application locally. You can use your solution from 04-php-orm or an API mock at webik.
Once you feel you know all there is about the application continue to the following slide.
COPY . .
RUN npm install
RUN npm build
.*
node_modules
dist
We do not need the build-related libraries and code. We only need the content of the this directory.
FROM {image} as build
FROM {another-image}
COPY --from=build {source} {target}
EXPOSE 80
The backend API is not part of our image, we need to proxy to other container / locations / ...
The Nginx configuration (container specific) is stored in /etc/nginx/conf.d/default.conf. We need to change this file and add instruction for a proxy.
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /api/ {
proxy_pass https://webik.ms.mff.cuni.cz/nswi153/2024-2025/service/09/api/;
}
}
We want the backend to be set using ENV variable! This is Nginx specific.
Files placed to /etc/nginx/templates/ are processed by 20-envsubst-on-templates.sh This script substitutes environment variables in the Docker image at startup (runtime).
COPY ./nginx/default.conf.template /etc/nginx/templates/
# We need to use env variable as we need it to be available at runtime.
ENV SERVER_URL=http://server/api/
We need to set the ENV variable when running the container!
docker run -p 8080:80 -e SERVER_URL="https://webik.ms.mff.cuni.cz/~skoda/2023-24/api/" nswi153/frontend
Content beyond this slide is not a mandatory part of the seminar. Yet it may help you with better use and understanding of Docker.
List of selected arguments:
There are single letter alternatives!