Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I automatically run a docker container on/after image build?

I'm new to docker so sorry if I miss something obvious

My plan for the Dockerfile.

  1. Pull tomcat:7.0
  2. Start the server
  3. Download helper files and unzip them.
  4. Download the required war file and rename it to ROOT.war.

I've managed to do this by running these commands manually.

docker run -it --rm -p 8888:8080 tomcat:8.0

Then manually doing the wget's and renaming, although I would like this to all be done in the docker file.

This is what I've managed to do so far.

FROM tomcat:7.0

RUN /bin/bash -c "cd /usr/local/tomcat/webapps"


RUN /bin/bash -c "wget -O files.zip https://***"
RUN /bin/bash -c "unzip files.zip"

RUN /bin/bash -c "rm -r ROOT"
RUN /bin/bash -c "wget -O ROOT.war https://***"

Although I am not sure how to run the docker line from earlier docker run -it --rm -p 8888:8080 tomcat:8.0

I've taken that line from the official Tomcat online here. I noticed they mentioned this Run the default Tomcat server (CMD ["catalina.sh", "run"]): but I have no idea how to implement this.

like image 830
Jack Avatar asked Dec 19 '22 20:12

Jack


1 Answers

What a Dockerfile does, is create an image not a container.

When you do docker build -t <tagname> . and will see the result (tagged) if you run docker images.

You can then run that image to create a container.

You can also specify a CMD to run (by default) in the Dockerfile. That would be (I think)

CMD [ "catalina.sh", "run" ] 

So:

docker build -t my_tomcat_app .
docker run -d -p 8888:8080 --name my_tomcat_container my_tomcat_app

This will create a container from your image, and run whatever the CMD said. You can poke around inside it with docker exec

docker exec -it my_tomcat_container bash

But it'll stay running (because of the -d flag) and show up in docker ps.

like image 177
Sobrique Avatar answered Jan 05 '23 00:01

Sobrique