> For the complete documentation index, see [llms.txt](https://a7246c5516ab4c80cdfe21ca2be3e40c.gitbook.io/docker-basics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://a7246c5516ab4c80cdfe21ca2be3e40c.gitbook.io/docker-basics/docker-file.md).

# docker file

A Dockerfile is a script used to build a Docker image. It contains a set of instructions for assembling a containerized application. Below is a comprehensive reference for various instructions that can be used in a Dockerfile:

#### **Dockerfile Instructions:**

**1. FROM:**

* Defines the base image for subsequent instructions.

```Dockerfile
FROM image_name:tag
```

**2. WORKDIR:**

* Sets the working directory for subsequent instructions.

```Dockerfile
WORKDIR /path/to/directory
```

**3. COPY:**

* Copies files or directories from the build context to the container.

```Dockerfile
COPY source destination
```

**4. ADD:**

* Similar to COPY but allows for additional features like URL support and automatic extraction of compressed files.

```Dockerfile
ADD source destination
```

**5. RUN:**

* Executes commands in a new layer on top of the current image.

```Dockerfile
RUN command
```

**6. CMD:**

* Provides default command for the container when it starts. It can be overridden by command-line arguments.

```Dockerfile
CMD ["executable", "arg1", "arg2"]
```

**7. ENTRYPOINT:**

* Configures a container that will run as an executable.

```Dockerfile
ENTRYPOINT ["executable", "arg1", "arg2"]
```

**8. EXPOSE:**

* Informs Docker that the container will listen on the specified network ports at runtime.

```Dockerfile
EXPOSE port
```

**9. ENV:**

* Sets environment variables in the form key=value.

```Dockerfile
ENV key=value
```

**10. ARG:**

* Defines variables that users can pass at build-time to the builder with the docker build command.

```Dockerfile
ARG variable_name=default_value
```

**11. VOLUME:**

* Creates a mount point with specified name for externally mounted volumes.

```Dockerfile
VOLUME /path/to/mountpoint
```

**12. USER:**

* Sets the username or UID and optionally the user group or GID to use when running the image.

```Dockerfile
USER user[:group]
```

**13. HEALTHCHECK:**

* Tells Docker how to test a container to check that it is still working.

```Dockerfile
HEALTHCHECK [options] CMD command
```

**14. LABEL:**

* Adds metadata to an image in the form of key-value pairs.

```Dockerfile
LABEL key="value" key2="value2"
```

**15. ONBUILD:**

* Adds a trigger instruction to the image that will be executed at a later time.

```Dockerfile
ONBUILD instruction
```

**16. SHELL:**

* Overrides the default shell used for the RUN instruction.

```Dockerfile
SHELL ["executable", "parameters"]
```

#### **Example Dockerfile:**

```Dockerfile
# Use an official base image
FROM ubuntu:latest

# Set the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install dependencies
RUN apt-get update && apt-get install -y python3

# Set environment variable
ENV NAME World

# Expose port
EXPOSE 80

# Run the command when the container starts
CMD ["python3", "app.py"]
```

Remember that Dockerfiles are case-sensitive, and instructions must be written in uppercase. Additionally, Dockerfiles are usually kept in the root of the application code. The `docker build` command is used to build an image from a Dockerfile, and `docker run` is used to run a container from that image.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://a7246c5516ab4c80cdfe21ca2be3e40c.gitbook.io/docker-basics/docker-file.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.
