# 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
```

