docker指令(docker指令不识别)

本篇文章给大家谈谈docker指令,以及docker指令不识别对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

详解Docker——你需要知道的Docker进阶知识五

Dockerfile 是一个文本文件,其中包含了构建 Docker 镜像需要执行的命令序列。使用 docker build 命令从 Dockerfile 中读取指令来构建镜像。

构建镜像时,该过程的第一件事是将 Dockerfile 文件所在目录下的所有内容发送给 Docker 守护进程。所以大多数情况下,最好创建一个新的目录,在其中保存 Dockerfile ,以及构建镜像所需的其它文件。Dockerfile 文件所在目录也被称为构建上下文(context)。

使用 FROM 指令指定一个基础镜像,后续指令将在此镜像基础上运行:

在 Dockerfile 中可以指定一个用户,后续的 RUN , CMD 以及 ENTRYPOINT 指令都会使用该用户身份去执行,该用户必须已存在。

除了指定用户之外,还可以使用 WORKDIR 指定当前工作目录(CWD), RUN , CMD , COPY , ADD 指令将在指定的工作目录中执行。

RUN 指令用于执行命令,该指令有两种形式:

例如我们执行更新命令:

CMD 的使用方式跟 RUN 类似,不过在一个 Dockerfile 文件中只能有一个 CMD 指令,如果有多个,则只有最后一个会生效。该指令指定了启动容器时要执行的命令,例如:

可以在 docker run 时指定命令来覆盖默认的 CMD 命令,比如 docker run image echo"hello shiyanlou" 。

CMD 指令还有一种特殊用法。在 Dockerfile 中,如果使用 ENTRYPOINT 指令指定了入口命令,则 CMD 指令的内容会作为 ENTRYPOINT 指令的参数:

ENTRYPOINT 指令会覆盖 CMD 指令作为容器运行时的默认指令,并且该指令不会被 docker run 时指定的指令覆盖,如下示例:

上述文件构建出来的镜像,使用 docker run image 等同于 docker run image ls-a-l 。使用 docker run image-i-s 等同于 docker run image ls-a-i-s 。即 CMD 指令的值会被当作 ENTRYPOINT 指令的参数附加到 ENTRYPOINT 指令的后面,只有 CMD 指令可以被覆盖。

COPY 和 ADD 都用于将构建上下文中的文件,目录等复制到镜像中。使用方式如下:

`` 可以指定多个,但是其路径不能超出构建上下文范围,即必须在 Dockerfile 同级或子目录中。

不需要预先存在,不存在时会自动创建,如果使用相对路径,则 为相对于工作目录的路径。塌陪

COPY 和 ADD 的不同之处在于,ADD 可伏衫或以添加远程文件,并且 `` 可以是 gzip 或 tar 等格式的压缩文件,添加时会自动进行解压。

ENV 指令用于设置环境变量:

VOLUME 指令指定要创建的挂载路径,在容器运行时,将为每个挂载路径创建一个匿名卷并挂载上去:

上述指令将会在容器运行时,创建两个匿名卷,并分别挂载到容器中的 /data1 和 /data2 路径。

学习了上面这些常见的 Dockerfile 指令之后,可以使用这些指令来构建一个镜像。如下所示,构建一个提供 ssh 服务的镜像:

构建镜像

查看镜像

启动容器

查看已经启动的容器

测试远程登录

Compose 是运行由多个容器组成的 Docker 应用的工具,使用 Compose 可以一次启动一组有关联的服务,每个服务由来自同一镜像的单个或多个容器组成。

在复杂应用中,应用一般由多个服务(service)组成,例如一个网站后台通常包含 Web 服务、数据库服务、缓存缺伍服务、消息队列服务等。

使用 Compose 的步骤如下:

目前有三种版本的 Compose 文件格式:

下载 docker-compose-Linux-x86_64

下载成功后,为了方便使用,可以将其添加到 PATH 路径下

执行完成后,就能够在终端下直接使用 docker-compose 命令了:

接下来我们将创建一个 Web 应用,该应用包含两个容器:

项目目录结构如下:

首先编辑 app/web/web.py 文件,写入下面的内容:

上述代码创建了一个简单的 Web 应用。该应用会连接 redis 服务,在访问 / 页面时,自动将变量 number 加 1。

编辑 app/web/requirements.txt 文件,输入如下内容:

requirements.txt 文件存放了 Web 应用依赖的第三方库包的名称和版本信息。

编辑 app/web/Dockerfile 文件,添加如下内容

上述 Dockerfile 定义了 Web 应用镜像,该镜像基于 python:2.7 基础镜像,在其基础上安装了应用依赖的库包,并通过 CMD 指令指定了应用的启动命令。

编辑 app/docker-compose.yml 文件:

该 docker-compose.yml 文件定义了两个服务,分别为 web 和 redis 服务,并且配置了 web 服务的端口映射和挂载目录。 depends_on 定义了依赖关系,被依赖的服会先启动。

进入 app 目录,执行 docker-compose up 命令来启动应用:

启动成功后,就可以打开网址 127.0.0.1:8001 来访问 Web 应用了。

另外一些命令:

详解Docker——你需要知道的Docker进阶知识二

Docker镜像

镜像仓库(Repository)用于存放镜像,每个仓库都有唯一的地址,和网址类似。镜像仓库托管在某个 Registry,Registry 和GitHub类似。Docker 提供了一个官方的 Registry,官方 Registry 里的镜像仓库地址可以省去前面的域名前缀,其它 Registry 里的镜像仓库地址必须要指定域名前缀,以保证唯一性。

镜像仓库地址后面可以跟一个 TAG。比如一个镜像名称 ubuntu:14.04 ,冒号前面的 ubuntu 是镜像仓库地址(由于是官方 Registry 里的,可以省略域名前缀),后面的 14.04 是 TAG,TAG 通常设置为镜像的版本号。

Docker 镜像是分层存储的,每一个镜像都由多层组成。镜像之间会森察世共享一些相同的层,从而减小镜像占用的存储空间。

也可以查看指定的镜像:

查看镜像的详细信息

比较常用的配置参数为 -a ,代表下载仓库中所有 TAG 的镜像,默认只下载 latest TAG 的镜像。

如果要下载 ubuntu:14.04 镜像没没,可使用如下命令:

对于我们 pull 的新镜像 ubuntu:14.04 来说,如果我们需要对其进行更新,可以创建一个容器,在容器中进行修改,然后将修改提交到一个新的镜像中。

提交修改使用如下命令:

该命令从一个修改过的容器创建一个新的镜像。例如,我们运行一个容器,然后在其中创建一个文件,最后使用 commit 命令:

通过上述操作我们创建了一个新的镜像,但是本方法不推荐在生产环境使用,因为这种方式的可维护性很差。推荐的创建镜像的方法是使用 Dockerfile ,修改镜像可通过修改 Dockerfile ,然后使用新的 Dockerfile 来构建新的镜像。

docker 可以从一个 Dockerfile 文件中读取指令来构建镜像。 Dockerfile 是一个包含用户构建镜像所需命令的文本文件。在 创建好该文件后,我们此肢可以使用如下命令来构建镜像:

对于一个 Dockerfile 文件内容来说,基本语法格式如下所示:

使用 # 号作为注释,指令( INSTRUCTION )不区分大小写,但是为了可读性,一般将其大写。 Dockerfile 中的指令一般包含下面几个部分:

下面是一个最基本的 Dockerfile :

通过阅读上述内容中我们熟悉的一些 linux 指令,可以很容易的知道该 Dockerfile 将创建一个 apache 镜像。

其中 FROM 指定基础镜像。 RUN 命令默认使用 /bin/sh ,并使用 root 权限执行。 CMD 命令也是默认在 /bin/sh 中执行,但是只能有一条 CMD 指令,如果有多条则只有最后一条会被执行。

下面我们创建一个空目录,在其中编辑 Dockerfile 文件,然后基于此文件构建一个新的镜像:

在构建完成后,我们可以使用该镜像启动一个容器来运行 apache 服务,运行如下命令:

此时,容器启动成功后,并且配置了端口映射,我们就可以通过本机的 8000 端口访问容器 hellodocker3 中的 apache 服务了。我们打开浏览器,输入 localhost:8000

删除 ubuntu:latest 镜像可以使用如下命令:

删除所有的镜像

Docker run 指令详解

本文翻译自docker官网:

The docker run command first creates a writeable container layer over the

specified image, and then starts it using the specified command. That is,

docker run is equivalent to the API /containers/create then

/containers/(id)/start . A stopped container can be restarted with all its

previous changes intact using docker start . See docker ps -a to view a list

of all containers.

The docker run command can be used in combination with docker commit to

change the command that a container runs . There is additional detailed information about docker run in the Docker run reference .

For information on connecting a container to a network, see the " Docker network overview " .

This example runs a container named test using the debian:latest

image. The -it instructs Docker to allocate a pseudo-TTY connected to

the container's stdin; creating an interactive bash shell in the container.

In the example, the bash shell is quit by entering

exit 13 . This exit code is passed on to the caller of

docker run , and is recorded in the test container's metadata.

This will create a container and print test to the console. The cidfile

flag makes Docker attempt to create a new file and write the container ID to it.

If the file exists already, Docker will return an error. Docker will close this

file when docker run exits.

This will not work, because by default, most potentially dangerous kernel

capabilities are dropped; including cap_sys_admin (which is required to mount

filesystems). However, the --privileged flag will allow it to run:

The --privileged flag gives all capabilities to the container, and it also

lifts all the limitations enforced by the device cgroup controller. In other

words, the container can then do almost everything that the host can do. This

flag exists to allow special use-cases, like running Docker within Docker.

The -w lets the command being executed inside directory given, here

/path/to/dir/ . If the path does not exist it is created inside the container.

This (size) will allow to set the container rootfs size to 120G at creation time.

This option is only available for the devicemapper , btrfs , overlay2 ,

windowsfilter and zfs graph drivers.

For the devicemapper , btrfs , windowsfilter and zfs graph drivers,

user cannot pass a size less than the Default BaseFS Size.

For the overlay2 storage driver, the size option is only available if the

backing fs is xfs and mounted with the pquota mount option.

Under these conditions, user can pass any size less than the backing fs size.

The --tmpfs flag mounts an empty tmpfs into the container with the rw ,

noexec , nosuid , size=65536k options.

The -v flag mounts the current working directory into the container. The -w

lets the command being executed inside the current working directory, by

changing into the directory to the value returned by pwd . So this

combination executes the command using the container, but inside the

current working directory.

When the host directory of a bind-mounted volume doesn't exist, Docker

will automatically create this directory on the host for you. In the

example above, Docker will create the /doesnt/exist

folder before starting your container.

Volumes can be used in combination with --read-only to control where

a container writes files. The --read-only flag mounts the container's root

filesystem as read only prohibiting writes to locations other than the

specified volumes for the container.

By bind-mounting the docker unix socket and statically linked docker

binary (refer to get the linux binary ),

you give the container the full access to create and manipulate the host's

Docker daemon.

On Windows, the paths must be specified using Windows-style semantics.

The following examples will fail when using Windows-based containers, as the

destination of a volume or bind mount inside the container must be one of:

a non-existing or empty directory; or a drive other than C:. Further, the source

of a bind mount must be a local directory, not a file.

For in-depth information about volumes, refer to manage data in containers

The --mount flag allows you to mount volumes, host-directories and tmpfs

mounts in a container.

The --mount flag supports most options that are supported by the -v or the

--volume flag, but uses a different syntax. For in-depth information on the

--mount flag, and a comparison between --volume and --mount , refer to

the service create command reference .

Even though there is no plan to deprecate --volume , usage of --mount is recommended.

Examples:

This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of the host

machine. You can also specify udp and sctp ports.

The Docker User Guide

explains in detail how to manipulate ports in Docker.

Note that ports which are not bound to the host (i.e., -p 80:80 instead of

-p 127.0.0.1:80:80 ) will be accessible from the outside. This also applies if

you configured UFW to block this specific port, as Docker manages his

own iptables rules. Read more

This exposes port 80 of the container without publishing the port to the host

system's interfaces.

Use the -e , --env , and --env-file flags to set simple (non-array)

environment variables in the container you're running, or overwrite variables

that are defined in the Dockerfile of the image you're running.

You can define the variable and its value when running the container:

You can also use variables that you've exported to your local environment:

When running the command, the Docker CLI client checks the value the variable

has in your local environment and passes it to the container.

If no = is provided and that variable is not exported in your local

environment, the variable won't be set in the container.

You can also load the environment variables from a file. This file should use

the syntax variable=value (which sets the variable to the given value) or

variable (which takes the value from the local environment), and # for comments.

A label is a key=value pair that applies metadata to a container. To label a container with two labels:

The my-label key doesn't specify a value so the label defaults to an empty

string ( "" ). To add multiple labels, repeat the label flag ( -l or --label ).

The key=value must be unique to avoid overwriting the label value. If you

specify labels with identical keys but different values, each subsequent value

overwrites the previous. Docker uses the last key=value you supply.

Use the --label-file flag to load multiple labels from a file. Delimit each

label in the file with an EOL mark. The example below loads labels from a

labels file in the current directory:

The label-file format is similar to the format for loading environment

variables. (Unlike environment variables, labels are not visible to processes

running inside a container.) The following example illustrates a label-file

format:

You can load multiple label-files by supplying multiple --label-file flags.

For additional information on working with labels, see Labels - custom

metadata in Docker in

the Docker User Guide.

When you start a container use the --network flag to connect it to a network.

This adds the busybox container to the my-net network.

You can also choose the IP addresses for the container with --ip and --ip6

flags when you start the container on a user-defined network.

If you want to add a running container to a network use the docker network connect subcommand.

You can connect multiple containers to the same network. Once connected, the

containers can communicate easily need only another container's IP address

or name. For overlay networks or custom plugins that support multi-host

connectivity, containers connected to the same multi-host network but launched

from different Engines can also communicate in this way.

You can disconnect a container from a network using the docker network disconnect command.

The --volumes-from flag mounts all the defined volumes from the referenced

containers. Containers can be specified by repetitions of the --volumes-from

argument. The container ID may be optionally suffixed with :ro or :rw to

mount the volumes in read-only or read-write mode, respectively. By default,

the volumes are mounted in the same mode (read write or read only) as

the reference container.

Labeling systems like SELinux require that proper labels are placed on volume

content mounted into a container. Without a label, the security system might

prevent the processes running inside the container from using the content. By

default, Docker does not change the labels set by the OS.

To change the label in the container context, you can add either of two suffixes

:z or :Z to the volume mount. These suffixes tell Docker to relabel file

objects on the shared volumes. The z option tells Docker that two containers

share the volume content. As a result, Docker labels the content with a shared

content label. Shared volume labels allow all containers to read/write content.

The Z option tells Docker to label the content with a private unshared label.

Only the current container can use a private volume.

The -a flag tells docker run to bind to the container's STDIN , STDOUT

or STDERR . This makes it possible to manipulate the output and input as

needed.

This pipes data into a container and prints the container's ID by attaching

only to the container's STDIN .

This isn't going to print anything unless there's an error because we've

only attached to the STDERR of the container. The container's logs

still store what's been written to STDERR and STDOUT .

This is how piping a file into a container could be done for a build.

The container's ID will be printed after the build is done and the build

logs could be retrieved using docker logs . This is

useful if you need to pipe a file or something else into a container and

retrieve the container's ID once the container has finished running.

It is often necessary to directly expose devices to a container. The --device

option enables that. For example, a specific block storage device or loop

device or audio device can be added to an otherwise unprivileged container

(without the --privileged flag) and have the application directly access it.

By default, the container will be able to read , write and mknod these devices.

This can be overridden using a third :rwm set of options to each --device

flag. If the container is running in privileged mode, then the permissions specified

will be ignored.

For Windows, the format of the string passed to the --device option is in

the form of --device=IdType/Id . Beginning with Windows Server 2019

and Windows 10 October 2018 Update, Windows only supports an IdType of

class and the Id as a device interface class

GUID .

Refer to the table defined in the Windows container

docs

for a list of container-supported device interface class GUIDs.

If this option is specified for a process-isolated Windows container, all

devices that implement the requested device interface class GUID are made

available in the container. For example, the command below makes all COM

ports on the host visible in the container.

The --gpus flag allows you to access NVIDIA GPU resources. First you need to

install nvidia-container-runtime .

Visit Specify a container's resources

for more information.

To use --gpus , specify which GPUs (or all) to use. If no value is provied, all

available GPUs are used. The example below exposes all available GPUs.

Use the device option to specify GPUs. The example below exposes a specific

GPU.

The example below exposes the first and third GPUs.

Use Docker's --restart to specify a container's restart policy . A restart

policy controls whether the Docker daemon restarts a container after exit.

Docker supports the following restart policies:

[img]

docker build 命令

docker build  命令用于使用 Dockerfile 创建镜像。

语法     docker build [OPTIONS] PATH | URL | -

创建一个空dir,名为py:

编辑docketfile 和 requirement.txt

然后创建镜像

docker build -t myimage

OPTIONS说明:

--build-arg=[] : 设置镜像创建时的变量;

--cpu-shares : 设置 cpu 使用权重;

--cpu-period : 限制 CPU CFS周期;

--cpu-quota : 限制 CPU CFS配额;

--cpuset-cpus : 指定使用的CPU id;

--cpuset-mems : 指定使用的内存 id;

--disable-content-trust : 忽略校验,默认开启;

-f : 指定要使用的Dockerfile路径;

--force-rm : 设置镜像过程中删除中间容器;

--isolation : 使用容器隔离技术;

--label=[] : 设置镜像使用的元数据;

-m : 设置内存最大值;

--memory-swap : 设置Swap的最大值为内存+swap,"-1"表示不限swap;

--no-cache : 创建镜像的过程不使用缓存;

--pull : 尝试去更新镜像的新版本;

--quiet, -q : 安静模式,成功后只输出镜像 ID;

--rm : 设置镜像成功后删除中间容器;

--shm-size : 设置/dev/shm的大小,并罩默认值是64M;

--ulimit : Ulimit配置。

--tag, -t:  镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。

--network:  默认 default。在构建期间设置RUN指令的网络模式

实例

1. 使用当前目录的 Dockerfile 创建镜像,标签为 runoob/ubuntu:v1。

$ docker build -t runoob/ubuntu:v1 .

Question: 这是不是name: tag的形式????

2. 使用URL  github.com/creack/docker-firefox  的 Dockerfile 创建镜像。

$ docker build github.com/creack/docker-firefox

3. 也可以通过 -f Dockerfile 文件的位置:

$ docker build -f /path/to/a/Dockerfile .

在 Docker 守护进程执行 Dockerfile 中的指令前,首先会对纯掘 Dockerfile 进行语法检查,有语法错误绝裤闹时会返回:

$ docker build -t test/myapp .Sending build context to Docker daemon 2.048 kBError response from daemon: Unknown instruction: RUNCMD

Docker常用命令大全

基础操作:

1  docker images  查看镜像信息列表 镜像是静态的

2  docker ps -a  查看运行中的所有容器

3  docker pull  [images]:[version] 从dockerhub拉取指定镜像

4  docker run -p 8000:80 -tdi --privileged [imageID] [command]   后台启动docker,并指定宿主机端口和docker映射端口。

  -i: 以交租搏含互模式运行容器,通常与 -t 同时使用;

  -d: 后台运行容器,并返回容器ID;

-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;

--privileged  容器将拥有访问主机所有设备的权限

通常情况下 [command] 填下  /bin/bash  即可。

特殊情况下,如需要在centos镜像中使用 systemctl  . 则应添加 --privileged  并设置[command ]为  init 。

5 当镜像通过run 启动后,便会载入到一个动态的container(容器)中运行,此时若需要进入终端交互模式:

sudo docker exec -it [containerID] /bin/bash

交互模式中,使用  ctrl+p+q退出交互 保持运行,使用 exit命令退出并停止容器。

6 在容器非交互模式下,通过docker  start/stop 命令来启动/停止已部署的容器服务。

7  docker rm [containerID]  删除容器

8  docker rmi [imageID]  删除镜像

9 docker cp [YourHostFilePath] [containerID]:[DockerPath]  将宿主机内的指定文件传输至容器内部的银誉指定地址。

镜像制作:

1   docker commit [containerID] [ImageName]:[Version]  将修改后的容器重新打包成镜像

2  docker commit -a "runoob.com" -m "my apache" a404c6c174a2 mymysql:v1  将容器a404c6c174a2 保存为新的镜像,并添加提交人信息和说明信息。

-a  :提交的镜像作者;

  -c  :使用Dockerfile指令来创建镜像;

  -m  :提交时的说明文字;

  -p  :在commit时,将容器暂停。

3  docker push [ImageID] [repertory_address] 提交镜像到云仓库

(暂时先记录弊笑这些,后续再更新)

关于docker指令和docker指令不识别的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

标签列表