How to install and use Docker on Ubuntu 20.04 system

0. Docker example, running docker example application in ubuntu

1. First install the docker environment. This is a ubuntu20.04 system. I will demonstrate and update the apt package index.

sudo apt update

2. Install dependency packages

sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

3. Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4. After prompting OK

Official installation

sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

5. Install the latest version of Docker Engine-Community

sudo apt install docker-ce

5.1 The installation is now complete and you can enter

sudo docker --version or sudo docker run hello-world to test whether the installation is successful! !

If you want to enter docker without sudo, directly enter the following command and test it yourself:

The following command means to add the user to the new docker group, restart docker, and switch the current session to the new group.

sudo groupadd docker
 
sudo gpasswd -a ${
    
    USER} docker
 
sudo service docker restart
 
newgrp - docker

Note: The last step is necessary, otherwise because the groups command obtains the cached group information, the newly added group information will not take effect, so there will be errors when executing docker images.

two

The above tutorial comes from: Thanks to G_1012_blogger

Install docker on Ubuntu 20.04 (one-click tutorial for fools) https://blog.csdn.net/G_1012_/article/details/129730072

Let's start with a small example of using docker and run a C++test test code!

It's very simple to create a file called a Dockerfile on your computer. Just follow these steps:
Open the directory where you want to create the Dockerfile.
Open a new file using the text editor of your choice (e.g. Notepad, Sublime Text, Visual Studio Code, etc.).
Copy and paste the following sample content into the file:
Dockerfile does not have a suffix name and can be operated directly with the command:

touch Dockerfile
创建一个Dockerfile文件(没有后缀名直接及时文件名字)

The next step is to write the configuration file in the Dockerfile. This file is similar to the CMakeLists.txt file in our C++. All docker configurations are operated through this file, so this is the most important. Dockerfile can be created in any directory.

FROM ubuntu:latest
RUN apt-get update && \
    apt-get install -y g++
COPY . /app
WORKDIR /app
RUN g++ -o myprogram main.cpp
CMD ["./myprogram"]

The above Dockerfile:
This is a Dockerfile file used to build a Docker image. Here's what each command does:


FROM ubuntu:latest:使用最新版的 Ubuntu 镜像作为基础镜像。

RUN apt-get update && \ apt-get install -y g++:在容器中运行更新和安装 g++ 编译器的命令。

COPY . /app:将当前目录中的所有文件复制到容器的 /app 目录中。

WORKDIR /app:将容器的工作目录设置为 /app。

RUN g++ -o myprogram main.cpp:在容器中编译名为 main.cpp 的 C++ 程序,并生成可执行文件 myprogram。

CMD ["./myprogram"]:设置容器启动时默认运行的命令为 ./myprogram,即运行刚刚编译出来的 C++ 程序。

After creating this Dockerfile, create a C++ test code

#include <iostream>

int main() {
    
    
while(1)
{
    
    
  std::cout << "Hello, Docker!" << std::endl;
  
}
  return 0;
}

The above main and Dockerfile files are in a folder.
Use the following command to build the Docker image:

docker build -t myapp .
Insert image description here

This will create a new image named "myapp" using the instructions in the Dockerfile.

Run the following command to start your application in a Docker container:
docker run --rm myapp
Insert image description here
This is running in an infinite loop. How can I stop it? ?
First use docker ps to view the running containers.

Insert image description here
Our ID is 91d2e199abf4.
Kill this through docker kill 91d2e199abf4
or docker stop 91d2e199abf4. This command is slow. It is recommended to use docker kill
to explain that the CPU required by this docker is cumulative. Frankly speaking, if a container is running, it takes up 20% of the CPU. , if both are 20%, then your CPU is occupied 40% cumulatively.
Commonly used commands in docker include
docker ps to view running containers

docker kill kills the process
docker run --rm runs the docker container. If –rm is not used, the hard disk will fill up quickly.

docker images View docker images

Docker rmi -f forcefully deletes the image

C++ output printing to web page

以下是在 Docker 中运行此程序的步骤:

创建一个名为 "Dockerfile" 的文件,内容如下:
# 设置基础镜像
FROM ubuntu:latest

# 更新软件包列表并安装必要的软件包
RUN apt-get update && \
    apt-get install -y g++

# 创建一个目录来存放我们的应用程序
RUN mkdir /app

# 将当前目录中的所有文件复制到容器的 /app 目录中
COPY . /app

# 设置工作目录为 /app
WORKDIR /app

# 编译应用程序
RUN g++ -o app main.cpp

# 声明容器需要暴露的端口号
EXPOSE 80

# 启动应用程序
CMD ["./app"]

Navigate to the directory containing the Dockerfile and main.cpp in a terminal or command prompt.

Build the Docker image using the following command:

docker build -t myapp .

Run the following command to start your application in a Docker container:
docker run -p 8080:80 myapp
This command will start a new Docker container and map port 80 inside the container to port 8080 on the host machine. You should be able to access "http://localhost:8080" in your browser and see "Hello, Docker!" output.

Notice! ! !

Why does my hard disk fill up quickly after running docker, but the image is very small? Why? ? ?
answer:

在 Docker 运行时,除了镜像文件本身外,还有其他一些文件会占用磁盘空间。以下是可能导致磁盘空间使用过多的原因:

容器日志:Docker 默认会将容器日志输出到宿主机的文件系统中,这些日志文件可能会占用大量的磁盘空间。你可以通过设置日志驱动(logging driver)来控制容器日志的大小和保存位置。

挂载卷:如果你在容器中挂载了一个宿主机目录或文件作为数据卷,那么容器写入到该卷的所有数据都会保存在宿主机上,可能会占用大量的磁盘空间。

缓存和临时文件:Docker 会在构建镜像和启动容器时生成一些缓存和临时文件,这些文件可能会占用大量的磁盘空间。你可以定期清理这些文件来释放磁盘空间。

未使用的镜像和容器:如果你有很多未使用的镜像和容器,它们也会占用大量的磁盘空间。你可以使用 docker system prune 命令来删除未使用的镜像、容器和卷等资源。

如果你想了解更详细的信息,你可以使用 docker system df 命令来查看 Docker 的磁盘使用情况。这个命令会列出所有 Docker 相关的文件和目录,并显示它们占用的磁盘空间大小。

Follow-up question: How to prevent this kind of thing from happening?

要防止 Docker 运行时占用过多的磁盘空间,你可以采取以下措施:

定期清理无用的镜像、容器和卷等资源,可以使用 docker system prune 命令来删除未使用的资源。

配置 Docker 的日志驱动,将容器日志输出到指定位置,并设置日志文件大小和保留时间等参数,避免日志文件过大。

确保容器不会在运行过程中产生大量的临时文件或缓存文件。如果需要在容器内生成大量文件,可以考虑将这些文件保存在数据卷中而不是容器本身。

避免在容器中挂载宿主机上的大型目录或文件,可以将数据保存在数据卷中而不是直接挂载到宿主机上。

在构建镜像时,尽可能减小镜像体积,避免包含不必要的文件和依赖项。

在启动容器时,尽可能使用基于 Alpine Linux 等轻量级操作系统构建的镜像,避免使用过于臃肿的基础镜像。

通过以上措施,可以有效地避免 Docker 运行时占用过多的磁盘空间。

The disk becomes full when you are not careful, so be careful when cleaning up the image.

To delete unused images in Docker, you can use the docker image prune command. This command will delete images that are not referenced by any containers to free up disk space.

The following is an example command to delete unused images:

After docker image prune
runs this command, Docker will list all images that can be deleted and ask you to confirm the deletion. If you are sure you want to delete these images, you can enter y or yes to confirm the deletion.

If you want to force the deletion of all unused images without confirming, you can use the --force parameter:

The docker image prune --force
command will directly delete all unused images without confirmation.

If you want to delete a specific image, you can use the docker rmi command. For example:

The docker rmi myimage:latest
command will delete the image named myimage and its latest label. If the image is being referenced by a container, you need to stop and delete the container before you can delete the image.

Guess you like

Origin blog.csdn.net/qq_38156743/article/details/130401015