做人呢,最紧要就系开心啦

docker常见用法

1,089次阅读
没有评论

1. 几个概念

虚拟机:
模拟一个完整的操作系统。常见的有 virtual box,vmwrare。

Linux 容器 /docker:
不是模拟一个完整的操作系统,而是对进程及其依赖环境进行隔离。对于容器里的进程来说,它接触到的资源都是虚拟的,与底层系统是隔离的。

Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口。是目前最流行的 Linux 容器解决方案。

镜像:
Docker 将应用程序及其依赖包,全部打包在一个文件里,就是镜像。镜像是可以迁移的。

下载,运行这个文件,就会生成一个本地实例,虚拟容器。程序在这个虚拟容器里运行,就好像在真实的物理机上运行一样。有了 Docker,就不用担心环境问题了。
(本地修改只会改动实例,而不会修改镜像)

镜像可以理解为容器的模版。根据镜像,运行的一个实例,就是容器

2.Docker 的用途

1. 提供一次性的部署开发环境,让开发,测试,可以使用统一的环境。
2. 提供弹性云服务。可以动态扩容 / 缩容。
3. 组建微服务架构。通过多个容器,一个机器运行多个容器,可以模拟微服务架构。

3.docker 使用方法:

3.1 加入 docker 用户组

Docker 需要用户具有 sudo 权限,为了避免每次命令都输入 sudo,可以把用户加入 Docker 用户组(官方文档)。

sudo usermod -aG docker $USER

激活 docker 组

newgrp docker

3.2 启动 docker 服务

# systemctl 命令的用法
$ sudo systemctl start docker

设置 Docker 服务在每次开机时自动启动

sudo systemctl enable docker

查看 docker 运行状态

systemctl status docker

3.3 查看本地有哪些镜像

$ docker image ls

image 文件是通用的,可以拷贝到不同机器使用,为了节省时间,一般使用制作好的 image, 或者基于已有的 image 文件添加内容,不用从零开始制作。

制作好的 image 也可以上传服务器共享,Docker 官方仓库 https://hub.docker.com/ 是最常见的 image 仓。

3.4 拉取并运行第一个 docker

docker 官方服务器,已经存放很多镜像在 library 组(默认组)。可以直接拉取使用。
拉取 hello-world 镜像

docker image pull library/hello-world

docker image pull hello-world

也可以直接运行 hello-world

docker run hello-world

hello-world 镜像本地不存在的话,会自动从 docker 官方仓库拉取。
运行成功,会有如下输出:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

hello-world 运行后停止,容器自动退出。

有些容器不会自动终止,其提供的服务,可以交互式登陆。

## 加速
docker pull dockerproxy.com/library/ubuntu:22.04

容器文件,image 文件生成的容器实例,本身也是一个文件,称为容器文件。也就是说,一旦容器生成,就会同时存在两个文件:image 文件和容器文件。而且关闭容器并不会删除容器文件,只是容器停止运行而已。

3.5 本地 docker 操作

创建一个 docker

运行 docker

docker run --name ubuntu1 -idt ubuntu-demo:0.0.1 

--name: docker 名字
-idt 中的 -d 指定容器的运行模式,使 docker 的服务在后台运行。默认不会进入容器,想要进入容器需要使用指令 docker exec.
-i: 交互式容器
ubuntu-demo:0.0.1 本地 image 名字

有些容器不会自动终止,其提供的服务,可以交互式登陆。
不自动终止的容器,使用 kill 命令停止。

docker container kill [containID]

查看本机容器实例

docker ps -a

docker container ls --all

启动 docker

docker start 'CONTAINER ID'

已经停止,但未删除的 docker,可以再次启动,运行。

docker exec -it  'CONTAINER ID' /bin/bash

停止 docker

docker stop 'CONTAINER ID'

删除容器

容器实例,先停止,才能删除

docker rm 'CONTAINER ID'

终止运行的容器文件,依然会占据硬盘空间,可以使用 docker container rm 命令删除。

再使用 docker container ls --all 命令,发现被删除的容器文件就消失了。

3.6 创建 image 文件

(1) 从 Dockerfile 文件创建 image

为方便自己跨平台使用,或者推广自己的软件,可以自己制作 image 文件。如何生成 image 文件?需要用到 Dockerfile 文件。
Dockerfile 是一个文本文件,用来配置 image。Docker 根据 Dockerfile 文件生成二进制的 image 文件。

下面通过一个实例,演示如何通过编写 Dockerfile 文件,创建镜像;
在当前目录创建一个.dockerignore 文件, 所列文件不会打包进 image

.git
xxx

touch Dockerfile 文件,内容为

FROM ubuntu        #从官方 ubuntu 镜像继承
COPY . /root/       #当前目录所有文件(.dockerignore 列表目录除外),拷贝到新 image 的 /root 目录
WORKDIR /root     #指定 image 工作目录
RUN pwd              #在 /root 目录,执行 pwd 命令
EXPOSE 9000        #将 image9000 端口暴露出来,外部可以链接

创建 image

docker image build -t ubuntu-demo:0.0.1 .
-t 指定 image 名字,冒号后 0.0.1 为标签,“.”当前目录下的 Dockerfile 文件。

查看新生成 image

$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED             SIZE
ubuntu-demo   0.0.1     215f37664606   About an hour ago   98.8MB
ubuntu        latest    fabf3a8d4949   3 weeks ago         98.8MB
hello-world   latest    ee301c921b8a   13 months ago       9.14kB

$ docker run -it ubuntu-demo:0.0.1 
root@344fd88b50eb:~# 

(2)在现有 docker 实例创建 image

运行 docker

docker run --name ubuntu1 -idt ubuntu-demo:0.0.1 

进入 docker

docker exec -it ubuntu1 /bin/bash

exit 退出,不会关闭容器

docker cp cpuinfo.txt  ubuntu1:/root/

生成新 image

docker commit -a 'author' -m 'instruction' ubuntu1 image-new1
ubuntu1: 运行中的容器名字
image-new1:新 image 名字

(3)将 image 存为压缩包

$ docker image ls
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
image-new1    latest    a46a6deba2ca   10 seconds ago   141MB
ubuntu-demo   0.0.1     215f37664606   2 hours ago      98.8MB
ubuntu        latest    fabf3a8d4949   3 weeks ago      98.8MB
hello-world   latest    ee301c921b8a   13 months ago    9.14kB

$ mkdir image-dir
$ cd image-dir/
$ docker save -o image-new1.tar image-new1
$ ls
image-new1.tar

(4)读取压缩包,创建镜像

将压缩包 image-new1.tar 拷贝到目标机器。

docker load -i image-new1.tar

用 image-new1 镜像创建一个新的容器

docker run --name image-new-test -idt image-new1

导出多个镜像合并一个包

docker save [image1] [image2]... [image-n]> [name.tar]

还原后,依然是多个 image.

3.7 容器其他操作

删除镜像(需先删除关联的容器):

docker rmi 'IMAGE ID'

干净卸载 docker:

rm -rf /etc/systemd/system/docker.service.d

rm -rf /var/lib/docker
rm -rf /var/run/docker
rm -rf /var/run/docker.pid

本地与 docker 共享目录 '

-v [local dir]:[docker dir], 将本地 [local dir] 目录,映射到 docker 目录中[docker dir]

docker run --name test2  -v  ~/:/root/ -idt my-ubuntu:0.0.1

docker 与 qemu 共享目录
进入 kernel 后,将共享目录挂载到指定目录。
mount -t 9p -o trans=virtio,version=9p2000.L share-dir /mnt

正文完
 
admin
版权声明:本站原创文章,由 admin 2024-05-29发表,共计4493字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)