如何使用Docker文件运行Docker命令 [英] How to run docker commands using a docker file

查看:40
本文介绍了如何使用Docker文件运行Docker命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些在码头上运行的基本docker命令.现在,我要使用所有基本的docker命令到一个docker文件中,然后构建该docker文件.

I've certain basic docker command which i run in my terminal. Now what i want is to use all the basic docker commands into one docker file and then, build that docker file.

例如考虑两个docker文件文件-Docker1,Docker2

For eg. Consider two docker files File - Docker1, Docker2

Docker1包含要运行的命令列表

Docker1 contains list of commands to run

在Docker2内部,我想构建Docker1并同时运行它

And inside Docker2 i want to build Docker1 and run it as well

Docker2 :(考虑带有演示代码的场景)

Docker2:(Consider the scenario with demo code)

FROM ubuntu:16.04
MAINTAINER abc@gmail.com
WORKDIR /home/docker_test/
RUN docker build -t Docker1 .
RUN docker run -it Docker1

我想做这样的事情.但是它抛出了-docker:来自守护程序oci运行时的错误响应创建失败的container_linux.go

I want to do something like this. But it is throwing - docker: error response from daemon oci runtime create failed container_linux.go

我该怎么做?我要去哪里错了

How can I do this? Where am I going wrong

PS-我是Docker的新手

P.S - I'm new to Docker

推荐答案

您的示例混合了两个步骤,图像创建运行图像,这不可能这种方式(与 Dockerfile 混合).

Your example is mixing two steps, image creation and running an image, that can't be mixed that way (with a Dockerfile).

一个 Dockerfile 用于创建映像.让我们以这个 alpine3.8 docker文件作为最小示例

A Dockerfileis used to create an image. Let's take this alpine3.8 docker file as a minimal example

FROM scratch
ADD rootfs.tar.xz /
CMD ["/bin/sh"]

这是一个基础映像,它不是基于另一个映像,它会开始 FROM暂存.然后将一个tar文件复制并解压缩,请参见 ADD ,其外壳为设置为启动命令,请参见 CMD .您可以使用

It's a base image, it's not based on another image, it starts FROM scratch. Then a tar file is copied and unpacked, see ADD and the shell is set as starting command, see CMD. You can build this with

docker build -t test_image .

从同一文件夹发布,该文件夹位于 Dockerfile 所在的位置.您还将需要该文件夹中的 rootfs.tar.xz ,从上面的高山链接中复制它.

Issued from the same folder, where the Dockerfile is. You will also need the rootfs.tar.xz in that folder, copy it from the alpine link above.

现在,您可以从该 test_image 中生成一个容器,

From that test_image you can now spawn a container with

docker run -it test_image

它将启动并为您提供容器内的外壳.

It will start up and give you the shell inside the container.

通常不需要在生成新的容器之前一遍又一遍地构建图像.但是,如果您确实需要,可以使用 docker-compose 来实现. Docker Compose 旨在定义和运行由多个容器组成的服务堆栈.堆栈是在 docker-compose.yml 文件中定义的.

Usually there is no need to build your images over and over again before spawning a new container. But if you really need to, you can do it with docker-compose. Docker Compose is intended to define and run a service stack consisting of several containers. The stack is defined in a docker-compose.yml file.

version: '3'
services:
  alpine_test:
    build: .

build:.在启动之前会先重新构建 image ,但通常只需 image:< image_name> 并使用一个已经存在的图像.

build: . takes care of building the image again before starting up, but usually it is sufficient to have just image: <image_name> and use an already existing image.

这篇关于如何使用Docker文件运行Docker命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆