【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)。
相关推荐
engchina7 小时前
WSL Ubuntu で Kubernetes v1.34.2 + Docker 環境を構築する
ubuntu·docker·kubernetes
JH30737 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
Coder_Boy_8 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
invicinble8 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟9 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖9 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_124987075310 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_10 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.10 小时前
Day06——权限认证-项目集成
java
瑶山10 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard