# 基本结构与常用指令

## 什么是Dockerfile

Dockerfile是由一系列命令和参数构成的脚本，这些命令应用于基础镜像并最终创建一个新的镜像。

&#x20;1、对于开发人员：可以为开发团队提供一个完全一致的开发环境；&#x20;

2、对于测试人员：可以直接拿开发时所构建 的镜像或者通过Dockerfile文件构建一个新的镜像开始工作了；&#x20;

3、对于运维人员：在部署时，可以实现应用的无 缝移植。

## 常用命令

| 命令                                   | 作用                                  |
| ------------------------------------ | ----------------------------------- |
| FROM image\_name:tag                 | 定义了使用哪个基础镜像启动构建流程                   |
| MAINTAINER user\_name                | 声明镜像的创建者                            |
| ENV key value                        | 设置环境变量 (可以写多条)                      |
| RUN command                          | 是Dockerfile的核心部分(可以写多条)             |
| ADD source\_dir/file dest\_dir/file  | 将宿主机的文件复制到容器内，如果是一个压缩文件，将会在复制后自 动解压 |
| COPY source\_dir/file dest\_dir/file | 和ADD相似，但是如果有压缩文件并不能解压               |
| WORKDIR path\_dir                    | 设置工作目录                              |

## 使用脚本创建镜像

步骤：&#x20;

（1）创建目录

```
mkdir –p /usr/local/dockerjdk8
```

（2）下载jdk-8u171-linux-x64.tar.gz并上传到服务器（虚拟机）中的/usr/local/dockerjdk8目录&#x20;

3）创建文件Dockerfile vi Dockerfile

```
#依赖镜像名称和ID
FROM centos:7
#指定镜像创建者信息
MAINTAINER ITCAST
#切换工作目录
WORKDIR /usr
RUN mkdir /usr/local/java
#ADD 是相对路径jar,把java添加到容器中
ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/
#配置java环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
```

（4）执行命令构建镜像

```
docker build -t='jdk1.8' .
```

注意后边的空格和点，不要省略&#x20;

（5）查看镜像是否建立完成

```
docker images
```

## 资料

[Dockerfile命令详解（超全版本）](https://www.cnblogs.com/dazhoushuoceshi/p/7066041.html)

### **示例**

```
FROM centos:latest
RUN yum -y install httpd  
RUN  systemctl enable httpd.service 
EXPOSE 80
CMD /usr/sbin/init
```

**构建镜像**

```
docker build -t centos:httpd .  (这里有个点，很重要。代表当前文件夹)
```

当然也可以指定Dockerfile

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

通过docker images可以看到出现了一个新镜像叫做centos只不过标签变成了httpd

**启用容器**

```
docker run --privileged -d -p 8080:80 --name myhttpd centos:httpd
```

> \--privileged 参数，给容器加特权,否则交互式方式进入容器无法操作一些譬如修改内核、修改系统参数、甚至启动服务等
>
> <https://docs.docker.com/engine/reference/commandline/run/#capture-container-id-cidfile>
>
> 资料
>
> 看github上的一个 问题提交
>
> <https://github.com/moby/moby/issues/7459>
>
> 以及redhat上的一个参考文档
>
> <https://developers.redhat.com/blog/2014/05/05/running-systemd-within-docker-container/>

### **示例java环境**

1、宿主机下载JDK1.8，解压到在/usr/local/jdk1.8.0\_121

2.Dockerfile

```
FROM centos:latest
COPY jdk1.8 /usr/local/jdk1.8.0_121
ENV JAVA_HOME=/usr/local/jdk1.8.0_121
ENV PATH $JAVA_HOME/bin:$PATH
ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
CMD /usr/sbin/init
```

3.构建镜像

```
docker build -t centos:jdk1.8
```

ENV 命令参考：<https://docs.docker.com/engine/reference/builder/#env>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docker.shujuwajue.com/docker-de-shi-yong/dockerfile-jie-shao/ji-ben-jie-gou-yu-chang-yong-zhi-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
