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

# Dockerfile

A **Dockerfile** is a text file that contains a series of instructions on how to build a Docker image. It defines the environment and the steps to set up your application inside the container. Think of it as a recipe that tells Docker how to create an image.

A basic structure of a Dockerfile might look like this:

```dockerfile
DockerfileCopyEdit# Use an official base image
FROM ubuntu:latest

# Set the working directory inside the container
WORKDIR /app

# Copy files from your local machine to the container
COPY . .

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

# Expose a port
EXPOSE 80

# Command to run the application
CMD ["python3", "app.py"]
```

Here’s what each instruction does:

* `FROM` specifies the base image (e.g., Ubuntu).
* `WORKDIR` sets the working directory in the container.
* `COPY` transfers files from your local machine to the container.
* `RUN` executes commands to install software or dependencies.
* `EXPOSE` makes a specific port available to the outside world.
* `CMD` defines the default command to run when the container starts.

#### 2. How to Build an Image from a Dockerfile

Once you have your Dockerfile ready, you can build a Docker image using the `docker build` command. Here’s the process:

**Step 1: Navigate to the directory where your Dockerfile is located.**

If your Dockerfile is in a folder called `my-app`, use the terminal to go to that folder:

```bash
cd my-app
```

**Step 2: Build the image.**

Use the following command to build the image. The `.` at the end indicates that Docker should use the current directory (where your Dockerfile is) to build the image.

```bash
docker build -t my-app-image .
```

* `-t` allows you to tag your image with a name (in this case, `my-app-image`).
* The `.` refers to the current directory where your Dockerfile is located.

This command will read the Dockerfile, execute its instructions, and create an image named `my-app-image`.

#### 3. How to Run an Image as a Container

After building the image, you can create and run a container from that image. This is done using the `docker run` command.

**Step 1: Run the container.**

Here’s how you can run a container based on the image you just built:

```bash
bashCopyEditdocker run -d -p 80:80 --name my-running-app my-app-image
```

* `-d` runs the container in detached mode (in the background).
* `-p 80:80` maps port 80 on your host machine to port 80 in the container, so you can access the app via your browser.
* `--name my-running-app` gives the container a name (`my-running-app`).
* `my-app-image` is the name of the image you want to run.

**Step 2: Verify the container is running.**

You can check if the container is running using the command:

```bash
docker ps
```

This will list all running containers. If your container is running, you’ll see it listed here.

**Step 3: Access the app.**

If your container exposes a web server on port 80, you can now access the app by opening your browser and going to `http://localhost`.

#### Summary:

1. **Dockerfile**: A set of instructions to create a Docker image.
2. **Build an Image**: Use `docker build -t <image-name> .` to create an image from the Dockerfile.
3. **Run a Container**: Use `docker run -d -p <host-port>:<container-port> --name <container-name> <image-name>` to run the image as a container.


---

# 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://42-guide.gitbook.io/42-guide/docker/dockerfile.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.
