# Tomcat

As noted in the [Tomcat docs](https://hub.docker.com/_/tomcat) browsing the webapps on a fresh container *"will return a 404 since there are no webapps loaded by default"*, forcing some tweaking when setting up the container.&#x20;

### Bare minimum including default landing page and manager

#### Manual installation

```bash
$ docker run -d -p 8888:8080 --name tomcat1101 --hostname tomcat1101 tomcat:11.0.1
$ docker exec -it tomcat1101 bash

## copy landing and manager applications
cp -r /usr/local/tomcat/webapps.dist/ROOT/ /usr/local/tomcat/webapps/ROOT
cp -r /usr/local/tomcat/webapps.dist/manager /usr/local/tomcat/webapps/manager

## allow manager access from all ip's
sed -i 's/allow="[^"]*"/allow=".*"/' /usr/local/tomcat/webapps/manager/META-INF/context.xml

## add user admin:admin
sed -i '/<\/tomcat-users>/d' /usr/local/tomcat/conf/tomcat-users.xml
echo '  <role rolename="manager-gui"/>' >> /usr/local/tomcat/conf/tomcat-users.xml
echo '  <role rolename="admin-gui"/>' >> /usr/local/tomcat/conf/tomcat-users.xml
echo '  <user username="admin" password="admin" roles="manager-gui,admin-gui"/>' >> /usr/local/tomcat/conf/tomcat-users.xml
echo '</tomcat-users>' >> /usr/local/tomcat/conf/tomcat-users.xml
```

#### Using Dockerfile

```bash
# Dockerfile              
# Stage 1: Build stage to copy web apps and make modifications
FROM tomcat:11.0.1 as build

# Copy web applications (ROOT and manager) from the `webapps.dist` folder inside the container
RUN mkdir -p /usr/local/tomcat/webapps/ROOT && \
    cp -r /usr/local/tomcat/webapps.dist/ROOT/* /usr/local/tomcat/webapps/ROOT && \
    cp -r /usr/local/tomcat/webapps.dist/manager /usr/local/tomcat/webapps/manager && \
    sed -i 's/allow="[^"]*"/allow=".*"/' /usr/local/tomcat/webapps/manager/META-INF/context.xml && \
    sed -i '/<\/tomcat-users>/d' /usr/local/tomcat/conf/tomcat-users.xml && \
    echo '  <role rolename="manager-gui"/>' >> /usr/local/tomcat/conf/tomcat-users.xml && \
    echo '  <role rolename="admin-gui"/>' >> /usr/local/tomcat/conf/tomcat-users.xml && \
    echo '  <user username="admin" password="admin" roles="manager-gui,admin-gui"/>' >> /usr/local/tomcat/conf/tomcat-users.xml && \
    echo '</tomcat-users>' >> /usr/local/tomcat/conf/tomcat-users.xml

# Stage 2: Final image to run Tomcat
FROM tomcat:11.0.1

# Copy from the build stage
COPY --from=build /usr/local/tomcat/webapps /usr/local/tomcat/webapps
COPY --from=build /usr/local/tomcat/conf /usr/local/tomcat/conf

# Expose the necessary port
EXPOSE 8080

# Start Tomcat in the foreground
CMD ["catalina.sh", "run"]

```

```bash
$ docker build -t tomcat1101 .
$ docker run -d -p 8888:8080 --name tomcat1101 tomcat1101
```


---

# 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://0xpthree.gitbook.io/notes/development/docker/tomcat.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.
