【docker-1】快速入门docker

docker搭建

1、下载二进制文件

shell 复制代码
wget https://mirror.nju.edu.cn/docker-ce/linux/static/stable/aarch64/docker-20.10.7.tgz

解压docker目录

shell 复制代码
tar -xzvf docker-20.10.7.tgz .

安装文件到/usr/local/bin

shell 复制代码
mv ./docker/* /usr/local/bin

2、配置docker.service

shell 复制代码
[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

3、配置docker.socket

shell 复制代码
[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

shell 复制代码
[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、创建一个镜像

shell 复制代码
admin01@admin01-kaitianm740z-d157:/data/docker/c++$ cat hello.cpp 
#include <iostream>
int main() {
    std::cout << "Hello from Docker!" << std::endl;
    return 0;
}

创建dockerfile

shell\ 复制代码
admin01@admin01-kaitianm740z-d157:/data/docker/c++$ cat dockerfile 
FROM gcc:11

COPY . /data/docker/hellowork

RUN ls -l

WORKDIR /data/docker/hellowork

RUN g++ -o hello hello.cpp

CMD ["./hello"]

生成镜像文件,在包含dockerfile的文件中执行

shell 复制代码
docker build -t hello_img .

检测容器的文件目录

shell 复制代码
docker run --rm -it hello_img ls /data/docker/hellowork

6、从镜像启动容器运行

shell 复制代码
docker run hello_img

admin01@admin01-kaitianm740z-d157:/data/docker/c++$ docker run hello_img
Hello from Docker!

7、移除镜像

shell 复制代码
docker rmi hell_img:latest

8、镜像与容器

1. 镜像(Image)

  • 定义:镜像是一个只读模板,包含了运行一个容器所需的文件系统、依赖项、应用程序代码以及运行环境。
  • 特点
    • 不可变:镜像是静态的,一旦创建,内容不会改变。
    • 可共享:镜像可以被上传到 Docker Hub 或其他镜像仓库,供其他用户下载和使用。
    • 多层结构:镜像由多个层(layers)组成,每一层代表了构建过程中的一次变更。
  • 作用
    • 镜像是容器的模板,用于创建容器实例。
    • 它定义了容器启动时的初始状态和运行环境。

2. 容器(Container)

  • 定义:容器是镜像的运行实例。它是一个隔离的、可执行的环境,运行在镜像的基础上。

  • 特点

    • 动态:容器是动态的,可以启动、停止、删除等。
    • 隔离:容器之间是隔离的,每个容器都有自己独立的文件系统、网络栈和资源限制。
    • 临时:容器的生命周期是临时的,删除容器后,其运行状态和数据会丢失(除非使用了持久化存储)。
  • 作用

    • 容器是运行应用程序的实际环境。
    • 它可以根据镜像的定义启动应用程序,并提供运行时所需的资源和环境。

镜像与容器的关系

  1. 镜像是容器的模板
    • 镜像定义了容器的初始状态和运行环境。
    • 容器是基于镜像创建的,每个容器都是镜像的一个独立实例。
  2. 容器是镜像的运行实例
    • 当你运行 docker run <image> 命令时,Docker 会从指定的镜像创建一个新的容器实例。
    • 容器可以独立运行,互不干扰。
  3. 容器可以修改镜像的内容
    • 容器在运行时可以对文件系统进行修改(如创建文件、安装软件等)。
    • 这些修改只影响当前容器,不会影响镜像本身。
  4. 容器的生命周期独立于镜像
    • 容器可以启动、停止、删除,而镜像始终保持不变。
    • 如果需要保存容器的修改,可以将容器提交为一个新的镜像(docker commit)。
相关推荐
罗政1 小时前
冒险岛079 V8 整合版源码搭建教程+IDEA启动
java·ide·intellij-idea
架构默片1 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
不只会拍照的程序猿2 小时前
从插入排序到希尔排序
java·开发语言·数据结构·算法·排序算法
柳鲲鹏2 小时前
docker push镜像到阿里云
阿里云·docker·容器
我荔枝呢!3 小时前
Java中的hashCode和equals方法之间有什么联系
java·开发语言·equals·hashcode
望未来无悔3 小时前
系统学习算法:专题十一 floodfill算法
java·算法
黑客老李3 小时前
新手小白如何挖掘cnvd通用漏洞之存储xss漏洞(利用xss钓鱼)
java·运维·服务器·前端·xss
不良人天码星3 小时前
Redis的简单使用
java·spring boot·redis·mybatis
猪萌萌3 小时前
关于如何利用群晖Docker搭建Project Zomboid(僵尸毁灭工程)私人服务器-保姆级教程
服务器·docker·容器·僵尸世界大战·游戏服务器搭建
面向未来_4 小时前
JAVA Kotlin Androd 使用String.format()格式化日期
java·开发语言·kotlin