Docker安全之: 基本使用
1、docker安装
docker安装以官方手册为指导。
官方手册为主:https://docs.docker.com/get-started/overview/
1.1 安装类型
官网上提供了两种Docker安装方式:
- Docker Dektop:如果装在macOS、windows、linux等有图形的桌面电脑,则用docker desktop。比如windows电脑、macos电脑、ubuntu、fedora电脑。
- Docker Engine:如果装在没有图形的电脑,则用docker engine,比如公司的centos服务器、阿里云的centos服务器等。
1.2 安装实例
以ubuntu为例,安装脚本如下。
# 卸载旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
# 更新仓库
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
# 添加docker GPG Key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 设置仓库
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 安装特定版本的docker,在复现老版本docker漏洞时可能会遇到
# List the available versions:
apt-cache madison docker-ce | awk '{ print $3 }'
5:20.10.16~3-0~ubuntu-jammy
5:20.10.15~3-0~ubuntu-jammy
5:20.10.14~3-0~ubuntu-jammy
5:20.10.13~3-0~ubuntu-jammy
VERSION_STRING=5:20.10.13~3-0~ubuntu-jammy
sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-compose-plugin
#测试安装是否成功
sudo docker run hello-world //运行一个镜像
sudo docker info //查看docker详细信息
sudo docker version //查看docker版本信息
sudo docker images //查看docker镜像
在windows下安装的Docker Desktop。

1.3 更新卸载
卸载Docker:
-
卸载包及依赖:
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin -
删除dockers目录
sudo rm -rf /var/lib/docker sudo rm -rf /var/lib/containerd
更新Docker:
- 查找主机上关于Docker的软件包: rpm -qa | grep docker --
- 卸载软件: apt remove xxx //上面查到的软件的信息
- 使用curl更新到最新版:curl -fsSL https://get.docker.com/ | sh
- 重启docker:systemctl restart docker
- 设置开机自启:systemctl enable docker
2、镜像命令
2.1 查看镜像
查看本地系统中的docker镜像:docker images
docker images [OPTIONS] [REPOSITORY[:TAG]]
--all , -a Show all images (default hides intermediate images)
--digests Show digests
--filter , -f Filter output based on conditions provided
--format Pretty-print images using a Go template
--no-trunc Don't truncate output
--quiet , -q Only show image IDs
2.2 获取镜像
获取镜像的几个途径:docker-hub获取、Dockerfile构建。
从docker-hub拉取镜像
Docker Hub 网址为: https://hub.docker.com/
-
查找镜像:
docker search [OPTIONS] TERM --filter , -f Filter output based on conditions provided --format Pretty-print search using a Go template --limit 25 Max number of search results --no-trunc Don't truncate output -
拉去镜像:
docker pull [OPTIONS] NAME[:TAG|@DIGEST] --all-tags , -a Download all tagged images in the repository --disable-content-trust true Skip image verification --platform Set platform if server is multi-platform capable --quiet , -q Suppress verbose output
使用Dockerfile构建镜像
Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
FROM scratch
ADD ubuntu-jammy-oci-amd64-root.tar.gz /
CMD ["bash"]
有了Dockerfile文件之后,通过 docker build 命令来构建一个镜像。docker build 参数说明:
- -t :指定要创建的目标镜像名
- . :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
2.3 删除镜像
docker rmi 镜像ID
2.4 导出镜像
导出/保存镜像到tar文件
docker save [OPTIONS] IMAGE [IMAGE...]
--time , -t 10 Seconds to wait for stop before killing the container
导出镜像到tar文件后,可以从tar生成一个新的镜像。
docker load [OPTIONS]
--input , -i Read from tar archive file, instead of STDIN
--quiet , -q Suppress the load output
3、容器命令
3.1 查看容器
docker ps [OPTIONS]
--all , -a Show all containers (default shows just running)
--filter , -f Filter output based on conditions provided
--format Pretty-print containers using a Go template
--last , -n -1 Show n last created containers (includes all states)
--latest , -l Show the latest created container (includes all states)
--no-trunc Don't truncate output
--quiet , -q Only display container IDs
--size , -s Display total file sizes
3.2 删除容器
docker rm [OPTIONS] CONTAINER [CONTAINER...]
--force , -f Force the removal of a running container (uses SIGKILL)
--link , -l Remove the specified link
--volumes , -v Remove anonymous volumes associated with the container
3.3 启动容器
镜像是静态的文件, 通过镜像可以运行一个容器,容器实在镜像上加了一层可写层。镜像和容器的关系类似于可执行文件与进程的关系。容器的启动参数是学习Docker最重要的部分,涉及Docker容器核心机制。
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
[OPTIONS]
--add-host Add a custom host-to-IP mapping (host:ip)
--attach , -a Attach to STDIN, STDOUT or STDERR
--blkio-weight Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device Block IO weight (relative device weight)
--cap-add Add Linux capabilities
--cap-drop Drop Linux capabilities
--cgroup-parent Optional parent cgroup for the container
--cgroupns API 1.41+
.....
IMAGE // 镜像名称
[COMMAND] 启动命令
[ARG...] 启动命令参数
3.3 停止容器
docker stop [OPTIONS] CONTAINER [CONTAINER...]
--time , -t 10 Seconds to wait for stop before killing it
3.4 重启容器
docker restart [OPTIONS] CONTAINER [CONTAINER...]
--time , -t 10 Seconds to wait for stop before killing the container
3.5 导入导出
导出本地容器
docker export [OPTIONS] CONTAINER
--output , -o Write to a file, instead of STDOUT
//eg: docker import /path/to/exampleimage.tgz
导入容器
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
--change , -c Apply Dockerfile instruction to the created image
--message , -m Set commit message for imported image
--platform Set platform if server is multi-platform capable
//eg; docker export --output="latest.tar" red_panda
3.6 容器交互
容器交互-attach模式
docker attach [OPTIONS] CONTAINER
--detach-keys Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy true Proxy all received signals to the process
容器交互-exec模式
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
--detach , -d Detached mode: run command in the background
--detach-keys Override the key sequence for detaching a container
--env , -e Set environment variables
--env-file Read in a file of environment variables
--interactive , -i Keep STDIN open even if not attached
--privileged Give extended privileges to the command
--tty , -t Allocate a pseudo-TTY
--user , -u Username or UID (format: <name|uid>[:<group|gid>])
--workdir , -w Working directory inside the container
4、容器进阶
启动容器时关注的几个参数
4.1 容器网络
4.2 容器存储
权限

浙公网安备 33010602011771号