I want to run a webapp via Docker by running 2 containers as a unit.
1 container runs my web-server (Tomcat 7).
The other container runs my database (Postgres 9.4).
I can run docker-compose up
and Docker is able to spin up my two containers as specified in my docker-compose.yml
:
web:
build: .
ports:
- "5000"
links:
- db
db:
image: postgres
I'd like to be able to spin up another copy of my webapp by running docker-compose up
again, but this results in Docker telling me that there are already containers running:
$ docker-compose up -d
Creating composetest_db_1
Creating composetest_web_1
$ docker-compose up -d
composetest_db_1 is up-to-date
composetest_web_1 is up-to-date
I've gotten around this issue by using the -p
option to give new copies different project names:
$ docker-compose -p project1 up -d
...
Successfully built d3268e345f3d
Creating project1_web_1
$ docker-compose -p project2 up -d
...
Successfully built d3268e345f3d
Creating project2_web_1
Unfortunately, this creating new images for each copy:
$ docker images
project1_web latest d3268e345f3d 2 hours ago 682 MB
project2_web latest d3268e345f3d 2 hours ago 682 MB
Is there a way to use docker-compose
to spin up multiple instances of a multi-container app by using a single image?
You can re-use your docker compose template by specifying the project name (which defaults to the directory name):
$ docker-compose --project-name inst1 up -d
Creating inst1_web_1
$ docker-compose --project-name inst2 up -d
Creating inst2_web_1
You could also scale up the container instances within a project:
$ docker-compose --project-name inst2 scale web=5
Creating and starting 2 ... done
Creating and starting 3 ... done
Creating and starting 4 ... done
Creating and starting 5 ... done
There should now be 6 containers running:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e4ab4cebacf tomcat:8.0 "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:32772->8080/tcp inst2_web_2
ced61f9ac2db tomcat:8.0 "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:32773->8080/tcp inst2_web_5
efb1ef13147c tomcat:8.0 "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:32771->8080/tcp inst2_web_4
58e524da3473 tomcat:8.0 "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:32770->8080/tcp inst2_web_3
0f58c3c3b0ed tomcat:8.0 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:32769->8080/tcp inst2_web_1
377e3e5b03e4 tomcat:8.0 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:32768->8080/tcp inst1_web_1
If you want to reuse the image, you should build the image independent of the compose script.
run docker build -t somewebapp/web:latest
Then change your build section of docker-compose.yml to reference an image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With