Replies: 5 comments
-
Docker file的使用在本地新建docker环境文件夹,将Dockerfile放入文件夹中,执行
即可快速搭建相应的镜像环境,也可以自己写Dockerfile,来创建环境,环境有修改的时候可以直接修改Dockerfile,这样下次再创建相同环境的时候就很方便了,并且可以push到自己的私有库,方便异地使用。 Dockerfile 概念Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。 镜像的定制实际上就是定制每一层所添加的配置、文件。如果我们可以把每一层修改、安装、构建、操作的命令都写入一个脚本,用这个脚本来构建、定制镜像,那么之前提及的无法重复的问题、镜像构建透明性的问题、体积的问题就都会解决。这个脚本就是 Dockerfile。 Dockerfile 是一个文本文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。有了 Dockerfile,当我们需要定制自己额外的需求时,只需在 Dockerfile 上添加或者修改指令,重新生成 image 即可,省去了敲命令的麻烦。 Dockerfile 文件格式 Dockerfile文件格式如下:
Dockerfile 分为四部分:基础镜像信息、维护者信息、镜像操作指令、容器启动执行指令。一开始必须要指明所基于的镜像名称,接下来一般会说明维护者信息;后面则是镜像操作指令,例如 RUN 指令。每执行一条RUN 指令,镜像添加新的一层,并提交;最后是 CMD 指令,来指明运行容器时的操作命令。 构建镜像docker build 命令会根据 Dockerfile 文件及上下文构建新 Docker 镜像。构建上下文是指 Dockerfile 所在的本地路径或一个URL(Git仓库地址)。构建上下文环境会被递归处理,所以构建所指定的路径还包括了子目录,而URL还包括了其中指定的子模块。 将当前目录做为构建上下文时,可以像下面这样使用docker build命令构建镜像:
说明:构建会在 Docker 后台守护进程(daemon)中执行,而不是CLI中。构建前,构建进程会将全部内容(递归)发送到守护进程。大多情况下,应该将一个空目录作为构建上下文环境,并将 Dockerfile 文件放在该目录下。 在构建上下文中使用的 Dockerfile 文件,是一个构建指令文件。为了提高构建性能,可以通过.dockerignore文件排除上下文目录下不需要的文件和目录。 在 Docker 构建镜像的第一步,docker CLI 会先在上下文目录中寻找.dockerignore文件,根据.dockerignore 文件排除上下文目录中的部分文件和目录,然后把剩下的文件和目录传递给 Docker 服务。 Dockerfile 一般位于构建上下文的根目录下,也可以通过-f指定该文件的位置:
构建时,还可以通过-t参数指定构建成镜像的仓库、标签。
|
Beta Was this translation helpful? Give feedback.
-
创建work容器创建容器work: -v 把宿主机上的目录共享给容器work使用 停止与重启容器: 删除容器: 进入容器: 5、退出容器:退出Ctrl+P+Q,退出并停止exit |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
拉取镜像从dockerhubDocker的镜像存储在镜像注册中心(image registry)中,默认是Docker Hub,在拉取镜像的时候也可以指定其它注册中心。 我们使用的大多数镜像都是在Docker Hub中已有镜像的基础上构建的。 拉取镜像,使用 ➜ ~ docker image pull --help
Usage: docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
-q, --quiet Suppress verbose output 从Docker Hub拉取镜像镜像的命名是: ➜ ~ docker image pull alpine
Using default tag: latest
latest: Pulling from library/alpine
cbdbe7a5bc2a: Pull complete
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
从其它注册中心拉取镜像镜像默认都是从Docker Hub上拉取,你也可以使用其它仓库,只需要在镜像名称前加上仓库地址,仓库地址与URL类似,但是不需要协议,默认使用https。 ➜ ~ docker image pull myregistry.local:5000/alpine 上面的命令从 拉取一个仓库的多个镜像
➜ ~ docker image pull -a alpine
2.6: Pulling from library/alpine
Image docker.io/library/alpine:2.6 uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
2a3ebcb7fbcc: Pull complete
Digest: sha256:e9cec9aec697d8b9d450edd32860ecd363f2f3174c8338beb5f809422d182c63
2.7: Pulling from library/alpine
... 使用摘要拉取镜像通过 有些情况下,你希望使用一个固定版本的镜像,可以通过摘要(digest)拉取镜像。摘要对于镜像是唯一的。 想知道镜像的digest,需要先拉取镜像: ➜ ~ docker image pull alpine
Using default tag: latest
latest: Pulling from library/alpine
cbdbe7a5bc2a: Pull complete
`Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54`
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest 通过digest拉取镜像: ➜ ~ docker image pull alpine@sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54: Pulling from library/alpine
Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
Status: Image is up to date for alpine@sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
docker.io/library/alpine@sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54 配置代理如果注册中心通过代理才能访问,则需要配置Docker Daemon的代理。设置 HTTPS_PROXY=https://my-proxy.com references |
Beta Was this translation helpful? Give feedback.
-
docker改变本地缓存位置 |
Beta Was this translation helpful? Give feedback.
-
安装Docker
Host 端配置
sudo apt-get -y install apt-transport-https ca-certificates curl
安装 GPG 证书
写入软件源信息
sudo apt-get install docker-ce docker-ce-cli containerd.io
配置用户组(执行docker命令时不用sudo,可选):
sudo usermod -aG docker $USER
安装工具:
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common
重启docker:
sudo service docker restart
验证是否成功:
sudo docker run hello-world
拉下干净最小的ubuntu:
sudo docker pull ubuntu
打标签:
sudo docker tag ubuntu carlos/ubuntu:v0
创建容器:
sudo docker run -it --name v1 carlos/ubuntu:v0
Docker内部配置
在容器内更新安装源:
export DEBIAN_FRONTEND=noninteractive; apt update; apt upgrade
apt-get -y install apt-transport-https ca-certificates curl sudo
换源: https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
sudo rm -rf /etc/apt/sources.list.d/*
在容器内安装开发常见工具和库:
Host端配置
提交成新的镜像:
sudo docker commit -m "basic os with common libraries" v1 carlos/ubuntu:v1
创建测试环境容器:
sudo docker run -it --rm -v /opt:/opt --name tt carlos/ubuntu:v1
在容器内测试:在/opt下测试编译
保存镜像成文件:
sudo docker save -o carlos_ubuntu_v1.tar carlos/ubuntu:v1
其他Host使用
在目标机里载入镜像:
sudo docker load < carlos_ubuntu_v1.tar
Beta Was this translation helpful? Give feedback.
All reactions