docker_0">docker搭建
1、下载二进制文件
wget https://mirror.nju.edu.cn/docker-ce/linux/static/stable/aarch64/docker-20.10.7.tgz
解压docker目录
tar -xzvf docker-20.10.7.tgz .
安装文件到/usr/local/bin
mv ./docker/* /usr/local/bin
dockerservice_20">2、配置docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service
Wants=network-online.target containerd.service
Requires=docker.socket[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/local/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500[Install]
WantedBy=multi-user.target
dockersocket_72">3、配置docker.socket
[Unit]
Description=Docker Socket for the API[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker[Install]
WantedBy=sockets.target
4、配置containerd.service
[unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=1048576
TasksMax=infinity
OOMScoreAdjust=-999[Install]
WantedBy=multi-user.target
5、创建一个镜像
admin01@admin01-kaitianm740z-d157:/data/docker/c++$ cat hello.cpp
#include <iostream>
int main() {std::cout << "Hello from Docker!" << std::endl;return 0;
}
创建dockerfile
admin01@admin01-kaitianm740z-d157:/data/docker/c++$ cat dockerfile
FROM gcc:11COPY . /data/docker/helloworkRUN ls -lWORKDIR /data/docker/helloworkRUN g++ -o hello hello.cppCMD ["./hello"]
生成镜像文件,在包含dockerfile的文件中执行
docker build -t hello_img .
检测容器的文件目录
docker run --rm -it hello_img ls /data/docker/hellowork
6、从镜像启动容器运行
docker run hello_imgadmin01@admin01-kaitianm740z-d157:/data/docker/c++$ docker run hello_img
Hello from Docker!
7、移除镜像
docker rmi hell_img:latest
8、镜像与容器
1. 镜像(Image)
- 定义:镜像是一个只读模板,包含了运行一个容器所需的文件系统、依赖项、应用程序代码以及运行环境。
- 特点:
- 不可变:镜像是静态的,一旦创建,内容不会改变。
- 可共享:镜像可以被上传到 Docker Hub 或其他镜像仓库,供其他用户下载和使用。
- 多层结构:镜像由多个层(layers)组成,每一层代表了构建过程中的一次变更。
- 作用:
- 镜像是容器的模板,用于创建容器实例。
- 它定义了容器启动时的初始状态和运行环境。
2. 容器(Container)
-
定义:容器是镜像的运行实例。它是一个隔离的、可执行的环境,运行在镜像的基础上。
-
特点:
- 动态:容器是动态的,可以启动、停止、删除等。
- 隔离:容器之间是隔离的,每个容器都有自己独立的文件系统、网络栈和资源限制。
- 临时:容器的生命周期是临时的,删除容器后,其运行状态和数据会丢失(除非使用了持久化存储)。
-
作用:
- 容器是运行应用程序的实际环境。
- 它可以根据镜像的定义启动应用程序,并提供运行时所需的资源和环境。
镜像与容器的关系