Reputation: 629
I have some problems while building an automated build for my web application, using mySQL.
I will use the example from https://github.com/ehazlett/docker-sample-java-mysql-tomcat.
This is my fig.yml
file:
db:
image: orchardup/mysql
environment:
MYSQL_USER: java
MYSQL_PASSWORD: java
MYSQL_DATABASE: javatest
ports:
- "3306"
dbinit:
image: orchardup/mysql
entrypoint: /bin/bash
volumes:
- .:/host
command: -c "sleep 4; mysql -u java --password=java -h mysql javatest < /host/init.sql; exit 0"
links:
- db:mysql
app:
build: .
links:
- dbinit
- db:mysql
ports:
- "8080"
And this the simple Dockerfile
:
FROM ehazlett/tomcat7
COPY dbtest /opt/tomcat/webapps/dbtest
Now, in order to manually run it I should run these commands:
docker run -d -P -e MYSQL_USER=java -e MYSQL_PASSWORD=java -e MYSQL_DATABASE=javatest --name mysql orchardup/mysql
docker run -ti --rm --link mysql:mysql -v $(pwd):/host --entrypoint /bin/bash orchardup/mysql -c "sleep 4; mysql -u java --password=java -h mysql javatest < /host/init.sql; exit 0"
docker build -t javatest .
docker run -ti -P --rm --link mysql:mysql javatest
I would like the the applciation would run just by typing:
docker-compose up
Is there any way to do that? Thank you for your help.
Upvotes: 0
Views: 939
Reputation: 5259
For ordering of running your docker containers you need to set depends_on.
For your case:
version: '2'
services:
db:
image: orchardup/mysql
environment:
MYSQL_USER: java
MYSQL_PASSWORD: java
MYSQL_DATABASE: javatest
ports:
- "3306"
dbinit:
image: orchardup/mysql
depends_on:
- db
entrypoint: /bin/bash
volumes:
- .:/host
command: -c "sleep 4; mysql -u java --password=java -h mysql javatest < /host/init.sql; exit 0"
links:
- db:mysql
javatest:
build: .
image: javatest
depends_on:
- dbinit
links:
- dbinit
- db:mysql
ports:
- "8080"
and run docker compose up with specific build for javatest
and run it in detached mode with -d
(optional):
docker-compose up --build -d javatest
just keep in mind containers will not wait for startup of db
just for "start container".
In this case you need to modify entrypoint and create additional script that will wait for connection - checking opened ports for example. - check wait-for docker suggestion
AFTER EDIT:
I have added full sample how you docker-compose
should looks like. I added version
and services
with correct formatting for yaml
files.
So back to what you want to do:
(1) docker run -d -P -e MYSQL_USER=java -e MYSQL_PASSWORD=java -e MYSQL_DATABASE=javatest --name mysql orchardup/mysql
(2) docker run -ti --rm --link mysql:mysql -v $(pwd):/host --entrypoint /bin/bash orchardup/mysql -c "sleep 4; mysql -u java --password=java -h mysql javatest < /host/init.sql; exit 0"
(3) docker build -t javatest .
(4) docker run -ti -P --rm --link mysql:mysql javatest
Explanation:
In the docker-compose
, db
is doing step (1)
, dbinit
is doing step (2)
and is depended on db
and service javatest
is doing steps (3)
and (4)
because you build javatest
image and run it with links like in step (4)
and also service javatest
is depended on dbinit
so it will wait until startup of container (with image orchardup/mysql
).
Upvotes: 1