【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)。
相关推荐
tmacfrank1 小时前
网络编程中的直接内存与零拷贝
java·linux·网络
weixin_472339462 小时前
Maven 下载安装与配置教程
java·maven
Magnum Lehar3 小时前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
就叫飞六吧3 小时前
Spring Security 集成指南:避免 CORS 跨域问题
java·后端·spring
Mcworld8573 小时前
java集合
java·开发语言·windows
天黑请闭眼3 小时前
IDEA:程序编译报错:java: Compilation failed: internal java compiler error
java·intellij-idea
苍煜4 小时前
Maven构建流程详解:如何正确管理微服务间的依赖关系-当依赖的模块更新后,我应该如何重新构建主项目
java·微服务·maven
冼紫菜4 小时前
[特殊字符]CentOS 7.6 安装 JDK 11(适配国内服务器环境)
java·linux·服务器·后端·centos
isyangli_blog4 小时前
(1-4)Java Object类、Final、注解、设计模式、抽象类、接口、内部类
java·开发语言
秋野酱5 小时前
Spring Boot 项目的计算机专业论文参考文献
java·spring boot·后端