# Download the Ubuntu image ~ 30MB.
docker pull ubuntu:noble
# NodeJS
docker pull node:21.7.2-slim
# Nginx
docker pull nginx:1.25.4
docker run hello-world
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-03/vite/Dockerfile
Create a Dockerfile for a container hosting the web application from practical-03.
Follow the steps to get to the reasonable Dockerfile.
Use nginx:1.25.4 container to host the application.
Beware of potential pitfalls!
You may add additional files to practical-03.
You can build and run the Dockerfile using following commands.
docker build -t nswi153/frontend .
docker run -p 8080:80 nswi153/frontend
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/~skoda/2023-24/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 as 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
Warning: This URL does not provide full implementation of the API! Use your own implementation instead.
Content beyond this slide is not 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!