This is the blueprint content of the Docker Workshop I plan to conduct on December 12, 2024.
Agenda (aka ToC)
- Welcome and Setup (10 minutes)
- What is Docker? (10 minutes)
- Docker key concepts (15 minutes)
- Hands-on: Your first Docker container (30 minutes)
- A quick introduction to Dockerfiles (15 minutes)
- Summary and Q&A (10 minutes)
Welcome and setup
Welcome to the Docker Workshop!
My name is Mohammad-Ali A’râbi, I am a software engineer and Docker captain. This means that I am a Docker expert and have been recognized by Docker for my contributions to the community. I have regular meetings with Docker engineers and get new features and updates as early as possible.
Read more:
What is Docker?
Docker was proposed by Solomon Hykes in 2013. The world is never the same again.
About 10 years later, he gave another talk at KubeCon 2024 in Paris, looking back on 10 years of Docker.
Here’s a photo of me and Solomon Hex:
It works on my machine!
——Every developer
Docker is a tool that allows you to run applications in containers. Containers are like lightweight virtual machines that run on your computer. They are isolated from each other and from the host system. This means you can run multiple containers on the same machine without them interfering with each other.
An application’s dependencies are packaged with the application itself. This makes it easy to run applications on different machines without worrying about the environment.
Let us ship your machine!
— Dockers
notes: Docker is not the only containerization tool. There are others such as Podman, LXC and rkt. However, Docker is the most popular one.
notes. Docker, Inc. built the Docker execution time and the Docker CLI. The Docker runtime (now called Containerd) was donated to CNCF in 2017.
Docker uses a core Linux feature called namespaces to isolate processes. Therefore, Docker runs natively on Linux. Other solutions, such as Docker Desktop for Mac and Windows, use a Linux VM to execute Docker.
Key concepts of Docker
Let’s try to review some Docker concepts:
- image: A read-only template that contains instructions for creating a Docker container. It contains application code, execution time, libraries, environment variables, and configuration files.
- container: An image instance running as a process on the host. It is isolated from other containers and has its own file system, network and process space.
- Dockerfile: A text file containing a set of instructions for building a Docker image. It is used to automate the process of creating images.
- Dockers Center: A cloud-based registry service that allows you to share Docker images publicly or privately. It’s like GitHub for Docker images.
Hands-on: Your first Docker container
Let’s start with an example Solomon Hykes used in his talk:
docker run -it busybox echo hello
This command runs the container from busybox
image and run echo hello
There are orders in it. this -it
flag Attach a terminal to the container so you can see the output.
By running this command the image is busybox
Download from Docker Hub and create a container from it, echo hello
The command is run inside the container and the container exits.
After pulling the image, you should be able to see it in the image manifest:
docker images
There is also a list of running containers:
docker ps -a
If you omit -a
flag, you will only see running containers.
docker ps
The hello container is not visible because it exits after executing the command. Now let’s set up a container that runs indefinitely:
docker run -d busybox sh -c 'while true; do echo hello; sleep 10; done'
To see the output you can use docker logs
command, but to get the container ID you need to use docker ps
Order:
docker ps
You can then use the container ID to get the logs:
docker logs
If you want to view the logs instantly, you can use -f
banner:
docker logs -f
To stop a container you can use docker stop
Order:
docker stop
Let’s run the same container again, but this time we’ll give it a name:
docker run -d --name hello busybox sh -c 'while true; do echo hello; sleep 10; done'
Now if you make a docker ps
you should see a container with the name hello
. We can use this name to inspect logs or attach to the container:
docker exec -it hello bash
it will error because bash
Command is not available in busybox
image. You can use sh
instead:
docker exec -it hello sh
notes. You can use docker debug
Attach the debugger container to the running container. This allows you to use tools that are not available on the running container. Docker debugging is a paid feature of Docker Desktop.
Now that you are inside the container you can execute the following command ls
, pwd
and ps
. You can also exit the container by typing exit
.
notes. If you change the state of the container, such as installing new software or creating new files, these changes are not saved. When you exit the container, changes will be lost. To save changes, you need to create a new image.
A quick introduction to Dockerfile
A Dockerfile is a text file that contains a set of instructions for building a Docker image. It is used to automate the process of creating images. The following is an example Dockerfile:
FROM busybox
RUN echo "hello world" > /hello.txt
CMD cat /hello.txt
This Dockerfile does the following:
- it starts with
busybox
image. - it runs
echo "hello world" > /hello.txt
The command creates a file namedhello.txt
with contenthello world
. - It sets the default command to
cat /hello.txt
.
To build an image from this Dockerfile you can use docker build
Order:
docker build -t hello .
this -t
flag tag image with name hello
. this .
The build context is specified at the end of the command, which is the current directory.
notes. Here, the image name is hello
which is different from the container name. The container name is used to identify the running container, and the image name is used to identify the image.
After building the image, you can run containers from it:
docker run hello
As expected, the container runs cat /hello.txt
Command and print hello world
and then exit. Let’s change the command to see if the changes have been saved:
docker run hello ls /
you should see hello.txt
files in the root directory. This means the changes have been saved in the image.
real world examples
Let’s look at a real-world Dockerfile example. Here is a Dockerfile for a simple web server:
this
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
This Dockerfile does the following:
- it starts with
nginx:alpine
image. - it copied
index.html
file from the build context to/usr/share/nginx/html/
Table of contents in the image.
To build an image from this Dockerfile you can use docker build
Order:
echo "" > index.html
docker build -t hello-server .
this -t
flag tag image with name hello-server
. this .
The build context is specified at the end of the command, which is the current directory. this index.html
The file should be in the same directory as the Dockerfile.
After building the image, you can run containers from it:
docker run -d -p 8080:80 hello-server
If you open your browser and go to http://localhost:8080you should see the message Hello, World!
.
practise
- Create a Dockerfile for installation
curl
and runcurl google.com
. - Create a Dockerfile that starts a web server on port 8080 and serves a static HTML file containing the following information
Hello, Docker!
. - Write a simple web server in Python to get the name from the query string and return it
Hello,
. Create a Dockerfile that executes this web server on port 8080.! python:latest
image. - After creating the Python web server, use the following command to check whether the image is vulnerable
docker scout cves
Order. How many vulnerabilities are there? How can you fix them? - Write a Python script to read a file named
data.txt
and print its contents. Create a Dockerfile to copydata.txt
file to the image and run the Python script. For this exercise you can usepython:latest
image. - Change
data.txt
file and reconstruct the image. Does the Python script print new content? - Change
data.txt
file and run the image again without rebuilding it. Does the Python script print new content? - Execute the Docker image from the previous exercise and mount the volume to
/data
Table of contents. Changedata.txt
View the file on the host machine and see if the Python script prints new content.
last words
I hope you enjoyed this article. If you have any questions or feedback, please feel free to contact me via the following methods:
To learn more about Docker, check out these posts on the Docker blog:
Thank you for reading! 🚀