08 Video Tutorial for Beginners Summary of Operations

08 Video Tutorial for Beginners Summary of Operations #

Hello, I’m Chrono.

Today’s lesson is a bit different from the previous ones, as it is presented in a video format. We have emphasized the importance of hands-on practical exercises when learning Kubernetes before, and we found that the pure text and image format used in the column may not be intuitive enough. Therefore, at the end of each learning stage, we will provide a video summary to present the previously learned content in a more visual way, which may enhance the learning effect.

The main content of this video course is similar to Lesson 7, which is a review and summary of the “Beginner’s Guide”, but the focus is on practical operations with Docker. We will not repeat the theoretical knowledge. After each video, we will provide key points for the operations, so that you can quickly locate and take notes.

Alright, let’s get started.


一. Familiarity with Docker usage #

Video operation points:

First, let’s operate Docker Engine.

(With the [preparation] foundation mentioned before class), Docker has been installed on this machine. Let me show you its information using docker version and docker info.

docker version displays Docker Engine 20.10.12, the system is Linux, the hardware architecture is arm64, which is Apple M1.

docker info displays the information related to the current system, such as CPU, memory, container count, image count, container runtime, storage file system, and so on. The storage file system used here is overlay2, the Linux kernel is 5.13, the operating system is Ubuntu 22.04 Jammy Jellyfish, the hardware is aarch64, two CPUs, and 4GB of memory.

Now let’s use docker ps to see the container list, which should be empty.

Then, use docker pull to pull the busybox image, and then use docker images to see the image list.

Use docker run to start the busybox image and execute the simplest “hello world” command:

docker run busybox echo hello world

Then, use docker ps -a to view the list of ended containers. You should be able to see the Busybox container that just finished running. You can use docker rm followed by the container ID to delete it.

2. Images and Containers #

Video operation points:

Let’s pull another image, the Alpine operating system:

docker pull alpine

Then use docker run, add the it parameter to run the shell inside it:

docker run -it alpine sh

This temporarily leaves the current Ubuntu operating system and enters the Alpine system inside the container. You can execute any commands inside it, such as cat /etc/os-release.

The container environment is completely isolated from the outside, with independent processes and file systems. However, there are also parts that are not isolated, such as time and the kernel.

Use exit to exit the container, and then execute date and uname -a in the host environment. You will see that they are consistent with the container.

Let’s run another container:

docker run -d --rm nginx:alpine

In the host machine, you can use ps -ef|grep nginx to see that there are three Nginx processes, which are actually the Nginx processes inside the container. After stopping them with docker stop and checking with ps again, you will find that they have disappeared.

This proves that a container is actually a process in the operating system, but with restrictions imposed by the container runtime environment such as namespaces, control groups, and chroot. Therefore, there is no difference between containers and regular processes in terms of resource usage. Moreover, because there is no virtual machine overhead, containers start more quickly and have higher resource utilization.

3. Building Your Own Image #

Video instructions:

Now let’s try writing a Dockerfile and building our own image.

This Dockerfile first uses the ARG instruction to define two variables, IMAGE_BASE and IMAGE_TAG. Then it uses the FROM instruction to specify the base image to build upon, combining these two variables to get nginx:1.21-alpine.

The following two ENV instructions define the PATH and DEBUG environment variables. The variables defined by the ARG instruction can only be used during the image building process, while the ones defined by ENV will be available as environment variables when the container runs, for the processes to use.

Next is the COPY instruction, which will copy ./default.conf from the build context to /etc/nginx/conf.d/ in the image. Note that the COPY instruction cannot use absolute paths; it must be relative to the build context. Additionally, Docker will package all files from the build context and send them to the Docker daemon, so it is recommended to only include necessary files.

The RUN instruction is used to execute a shell command during image building. It can be used to install software, create directories, compile programs, etc. In this case, it simply uses the echo command to generate a text file.

The last two instructions, EXPOSE and WORKDIR, declare the port number for external services and the working directory of the container, respectively.

Now that we understand the contents of the Dockerfile, we can use docker build to build the image. We can use the -t flag to tag the image and specify the build context path, using a dot . for the current directory:

docker build -t ngx-app:1.0 .

Once the build is completed and the image is generated, we can use docker run to start a container from the image and verify if the files in the image were generated correctly:

docker run -it --rm ngx-app:1.0 sh

Finally, we can use the docker save/load commands to export it as a compressed file for easy saving and transferring:

docker save ngx-app:1.0 -o ngx.tar
docker load -i ngx.tar

4. Operations for Interacting with External Systems #

Video Operations Highlights:

Now let’s take a look at some operations for the interaction between containers and external systems.

First, let’s start a Redis container using the docker run command:

docker run -d --rm redis

Next, let’s generate a simple text file using the echo command:

echo 'aaa' > a.txt

By using the docker ps command, we can see the ID of the container. With that, we can use the docker cp command to copy this file into the container:

docker cp a.txt 062:/tmp

We can use the docker exec command to enter the container and check if the file has been copied correctly:

docker exec -it 062 sh
ls /tmp

After exiting the container, let’s rename this file and copy it out:

docker cp 062:/tmp/a.txt ./b.txt

Now, let’s take a look at how to directly mount a local directory into the container using the -v parameter:

docker run -d --rm -v /tmp:/tmp redis

By using the docker exec command to enter the container, we can check the contents of the “/tmp” directory inside the container and confirm that the file is identical to the host machine.

docker exec -it b5a sh     # b5a is the container ID

The -p parameter is used to map local ports to container ports. Let’s start an Nginx container and map port 80 of the host machine to port 80 of the container:

docker run -d -p 80:80 --rm nginx:alpine

With the docker ps command, we can see the mapping of ports. We can also use curl to directly access the Nginx service inside the container:

curl 127.1:80   -I

By using the docker exec command, we can view the network card configuration inside the container:

docker exec xxx ip addr

We can see that the network card configuration inside the container is completely different from the host machine. The eth0 is a virtual network card with a B-class private IP address of “172.17.0.2”.

5. Building WordPress #

Video instructions:

Finally, let’s demonstrate the process of building WordPress using Docker.

Because it’s cumbersome to write environment variables in Docker commands and the commands can be long, I have written a script for the building process.

There are three commands in total. First, we start MariaDB and set the environment variables such as database name, username, and password. Then we start WordPress, using the username and password from the MariaDB setup. The db_host must be the IP address of the MariaDB. Finally, we start Nginx, which needs to specify the address of WordPress in the configuration file. We then use the -v parameter to mount it into the container.

After executing this script, let’s use docker ps to check the status of the containers.

Once we confirm that the containers are running properly, we can enter the IP address in a browser to access the WordPress website.

Homework #

Today’s class is a hands-on practical class. Remember to actually do the hands-on operation yourself for the homework.

Feel free to share your practical experience in the comment section. If you have any questions, feel free to leave a comment and participate in the discussion. See you in the next class, Beginner’s Level.