Dockerfile - where am I going wrong?

FROM tomcat:latest
COPY webapps/smartslot.war /usr/local/tomcat/webapps/
RUN rm /usr/local/tomcat/etc/server.xml
RUN rm /usr/local/tomcat/etc/context.xml
COPY etc/server.xml /usr/local/tomcat/etc/
COPY etc/context.xml /usr/local/tomcat/etc/
EXPOSE 8080
CMD /usr/local/tomcat/bin/catalina.sh run

“Where am I going wrong”

I don’t know, except you shared nothing about what your problem is with the Dockerfile. Please, help the community to understand what you need help with and don’t let them spend time to figure out your issue.

And also please, use markdown to format code blocks or use the </> button to open a new code block.

Sorry about that. I am trying to include a mysql Datasource to connect with a tomcat web server. So I have included the Datasource info in the tomcat server.xml and the context.xml to replace existing files. I have tomcat up and running together with mysql. Ultimately I am writing a java API which uses this datasource. The issue is that I cannot tell if the files have been replaced successfully because I cannot look into the tomcat container (STATUS Exited), but tomcat is running. Have I got the Dockerfile commands correct? Thanks for listening.

FROM tomcat:latest
COPY webapps/smartslot.war /usr/local/tomcat/webapps/
RUN rm /usr/local/tomcat/etc/server.xml
RUN rm /usr/local/tomcat/etc/context.xml
COPY etc/server.xml /usr/local/tomcat/etc/
COPY etc/context.xml /usr/local/tomcat/etc/
EXPOSE 8080
CMD /usr/local/tomcat/bin/catalina.sh run

Is is exited or running???

You can try something like:

COPY etc/server.xml /usr/local/tomcat/etc/
COPY etc/context.xml /usr/local/tomcat/etc/
RUN cat /usr/local/tomcat/etc/server.xml
RUN cat /usr/local/tomcat/etc/context.xml

@bluepuma77 already pointed out, but it is not possible that the tomcat container existed when the tomcat in the container is running. The container is actually the isolation of the process running in it. A container doesn’t exist without a running process in it and the container cannot exit while the process is running in it.

So what do you actually see? Why do you think Tomcat is running?

By the way you don’t need a running container to be able to read files in it. You just can’t run processes in it like a shell. But the docker cp command can copy out files even after the container stopped. That is useful when you are using Docker Desktop and you have no direct access to the Docker Daemon’s data root. On Liux, using Docker CE, you could find the container’s folder, but if you know the path of the file you want to copy out, docker cp is better so you don’t accidentally change something in the data root you shouldn’t.

Bluepuma77’s suggestion with the Dockerfile is also useful, but the docker build command will not keep the output on the screen unless you set --progress plain, so keep that in mind. Also if you run the build command first, the layers will come from the cache next time, so you might want to add --no-cache as well.

Observations:

  • looks like another tomcat instance must be running, of course this instance will not use the configuration of the containerized tomcat.
  • Does /usr/local/tomcat/bin/catalina.sh run really start a foreground process that is kept running?
    • we haven’t seen any error logs from tomcat that indicates why it exits.

Its me being stupid. I needed to create an image from the Dockerfile and run that as a container. Now I have:

FROM tomcat:latest
COPY webapps/smartslot.war /usr/local/tomcat/webapps/
COPY lib/mysql-connector-j-8.3.0.jar /usr/local/tomcat/lib/
COPY etc/server.xml /usr/local/tomcat/conf/
COPY etc/context.xml /usr/local/tomcat/conf/
EXPOSE 8080
CMD /usr/local/tomcat/bin/catalina.sh run

So does it work now? Did you just have to add the missing mysql connector library?

localhost:8080 points to tomcat, but localhost:8080/smartslot comes up with a Firefox unable to connect error. Its hard to see where the issue is.

It would be helpful if you share your compose file or Docker command to run the container.

And the outputs of docker info, docker ps and docker image ls.

version: "3"
services:
  wordpress:
    image: wordpress
    volumes:
      - ./wp_data:/var/www/html
    ports: 
      - "8089:80"
    depends_on:
      - mysql
    environment:
        WORDPRESS_DB_HOST: mysql
        WORDPRESS_DB_USER: ****
        WORDPRESS_DB_PASSWORD: "****"
        WORDPRESS_DB_NAME: wordpress2
    networks:
      - my_app_network
  mysql: 
    image: "mysql:8.0.45"
    init: true
    environment: 
      MYSQL_DATABASE: wordpress2
      MYSQL_ROOT_PASSWORD: "****"
    ports:
      - "3307:3306"
    volumes:
      - ./mysql:/var/lib/mysql
    networks:
      - my_app_network
  phpmyadmin:
    image: phpmyadmin
    restart: always
    depends_on:
      - mysql
      - apache
    ports: 
      - 8083:80
    environment:
      - PMA_ARBITRARY=1
    networks:
      - my_app_network
  apache:
    image: httpd
    restart: always
    ports:
      - 8081:80
    networks:
      - my_app_network
  web:
    image: tomcat-war:latest
    build: .
    ports:
        - 8080:8080
    volumes:
        - ./webapps:/usr/local/tomcat/webapps
        - ./lib:/usr/local/tomcat/lib
    networks:
        - my_app_network
networks: # <-- DEFINE THE NETWORK
  my_app_network:
    driver: bridge
nigel-evenden@nigel-evenden-HP-Laptop-17-cp3xxx:~/docker_test$ sudo docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                      PORTS                                                    NAMES
2e81785ff649   wordpress           "docker-entrypoint.s…"   33 seconds ago   Up 32 seconds               0.0.0.0:8089->80/tcp, [::]:8089->80/tcp                  docker_test-wordpress-1
2f006b1aae59   phpmyadmin          "/docker-entrypoint.…"   33 seconds ago   Up 32 seconds               0.0.0.0:8083->80/tcp, [::]:8083->80/tcp                  docker_test-phpmyadmin-1
bcc2488f46ac   httpd               "httpd-foreground"       33 seconds ago   Up 32 seconds               0.0.0.0:8081->80/tcp, [::]:8081->80/tcp                  docker_test-apache-1
a8b2681335fe   mysql:8.0.45        "docker-entrypoint.s…"   33 seconds ago   Up 32 seconds               33060/tcp, 0.0.0.0:3307->3306/tcp, [::]:3307->3306/tcp   docker_test-mysql-1
bec93d8dfe31   tomcat-war:latest   "/bin/sh -c '/usr/lo…"   33 seconds ago   Exited (0) 32 seconds ago     

The tomcat container exited. Use docker logs bec93d8dfe31 to see the logs.