Introduction to Docker, part I
December 12, 2024

Introduction to Docker, part I

This is the blueprint content of the Docker Workshop I plan to conduct on December 12, 2024.


Agenda (aka ToC)

  1. Welcome and Setup (10 minutes)
  2. What is Docker? (10 minutes)
  3. Docker key concepts (15 minutes)
  4. Hands-on: Your first Docker container (30 minutes)
  5. A quick introduction to Dockerfiles (15 minutes)
  6. 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:

Solomon Hex and Muhammad Ali Arabi


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
Enter full screen mode

Exit full screen mode

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
Enter full screen mode

Exit full screen mode

There is also a list of running containers:

docker ps -a
Enter full screen mode

Exit full screen mode

If you omit -a flag, you will only see running containers.

docker ps
Enter full screen mode

Exit full screen mode

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'
Enter full screen mode

Exit full screen mode

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
Enter full screen mode

Exit full screen mode

You can then use the container ID to get the logs:

docker logs 
Enter full screen mode

Exit full screen mode

If you want to view the logs instantly, you can use -f banner:

docker logs -f 
Enter full screen mode

Exit full screen mode

To stop a container you can use docker stop Order:

docker stop 
Enter full screen mode

Exit full screen mode

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'
Enter full screen mode

Exit full screen mode

Now if you make a docker psyou 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
Enter full screen mode

Exit full screen mode

it will error because bash Command is not available in busybox image. You can use sh instead:

docker exec -it hello sh
Enter full screen mode

Exit full screen mode

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, pwdand 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
Enter full screen mode

Exit full screen mode

This Dockerfile does the following:

  1. it starts with busybox image.
  2. it runs echo "hello world" > /hello.txt The command creates a file named hello.txt with content hello world.
  3. 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 .
Enter full screen mode

Exit full screen mode

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 hellowhich 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
Enter full screen mode

Exit full screen mode

As expected, the container runs cat /hello.txt Command and print hello worldand then exit. Let’s change the command to see if the changes have been saved:

docker run hello ls /
Enter full screen mode

Exit full screen mode

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
Enter full screen mode

Exit full screen mode

This Dockerfile does the following:

  1. it starts with nginx:alpine image.
  2. 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 .
Enter full screen mode

Exit full screen mode

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
Enter full screen mode

Exit full screen mode

If you open your browser and go to http://localhost:8080you should see the message Hello, World!.


practise

  1. Create a Dockerfile for installation curl and run curl google.com.
  2. Create a Dockerfile that starts a web server on port 8080 and serves a static HTML file containing the following information Hello, Docker!.
  3. 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.
  4. 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?
  5. Write a Python script to read a file named data.txt and print its contents. Create a Dockerfile to copy data.txt file to the image and run the Python script. For this exercise you can use python:latest image.
  6. Change data.txt file and reconstruct the image. Does the Python script print new content?
  7. Change data.txt file and run the image again without rebuilding it. Does the Python script print new content?
  8. Execute the Docker image from the previous exercise and mount the volume to /data Table of contents. Change data.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! 🚀

2024-12-12 02:03:23

Leave a Reply

Your email address will not be published. Required fields are marked *