0

I am trying to test and use docker for my environment. Here is my Dockerfile When I access the tomcat (http://localhost:8080), I get the problem This site can’t be reached.

Note => For window, I use http://192.168.99.100:8080/.

Build

docker build -f Dockerfile -t docker-spring-rest .

Run

docker run -p 8080:8080 -p 3306:3306 docker-spring-rest   

Dockerfile

#Prat-1 tomcat
FROM tomcat:8.5.35
COPY ./target/spring-rest.war /usr/local/tomcat/webapps/
EXPOSE 8080

#Prat-1 tomcat
FROM mysql:5.5
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE spring-rest
COPY ./DB.SQL /docker-entrypoint-initdb.d/

One strange thing is, If I setup only Part-1 tomcat without mysql, I can access http://192.168.99.100:8080/ or my application http://192.168.99.100:8080/spring-rest Is there any missing in my file?

Googling => I checked this tomcat-mysql reference. Why they use apt-get to install because of docker already have multiple images? Can I use multiple FROM like FROM tomcat, FROM mysql, FROM xxx in single Dockerfile?

Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • If you have multiple containers you should use docker-compose for that. Maybe they use apt-get because they install it into current image machine? – daremachine Dec 07 '18 at 08:18
  • inspiration https://github.com/twogg-git/docker-compose-java – daremachine Dec 07 '18 at 08:20
  • https://stackoverflow.com/questions/33322103/multiple-froms-what-it-means : The general syntax involves adding FROM additional times within your Dockerfile - whichever is the last FROM statement is the final base image. To copy artifacts and outputs from intermediate images use COPY --from=. – Sven Hakvoort Dec 07 '18 at 08:29

1 Answers1

2

The key question is here: "Can I use multiple FROM ... in a single Dockerfile".

If you do that, you are using a new docker feature called multistage-build. This feature allows you to build an image in multiple stages, where each FROM starts from a fresh base layer, discarding everything you did before.

This is not what you intent to do, because when you do FROM mysql:5.5 you loose the entire tomcat part from above that line.

You could build a docker image containing tomcat and mysql (using only one FROM) instruction, but I would advised against it. Docker images are supposed to deal with one concern only.

So indeed the best solution would be to create two Docker images (2 Dockerfiles), one for tomcat, one for mysql and then use docker-compose to run the two as a composite.

Fabian Braun
  • 3,612
  • 1
  • 27
  • 44
  • In addition to this (for the OP): Also note that the tomcat-mysql image is BAD practice as an image since it also violates the single concern as mentioned by @fab – Sven Hakvoort Dec 07 '18 at 08:32
  • @fab, one xxx for one Dockerfile? Is is possible to run all of them at the same time using single shell or what ever? Now I will check your reference `multistage-build` – Zaw Than oo Dec 07 '18 at 08:47
  • Yes, the tool to run all of them at the same time is [docker-compose](https://docs.docker.com/compose/). In your shell you simply type `docker-compose up` and you need to provide a docker-compose.yaml file in the current directory which references your tomcat & mysql image. – Fabian Braun Dec 07 '18 at 09:00