getting-started
getting-started copied to clipboard
Running app through docker-compose doesn't work with default network
Working on Ubuntu 18.04, when I run the app through docker-compose I get nothing in the browser.
Running the two containers from the command line works fine, with the commands below:
~/dev/docker/app$ docker run -d \
--network todo-app \
--network-alias mysql \
-v todo-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=todos \
mysql:5.7
~/dev/docker/app$ docker run -dp 3000:3000 \
-w /app -v ${PWD}:/app \
--network todo-app \
-e MYSQL_HOST=mysql \
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=secret \
-e MYSQL_DB=todos \
node:12-alpine \
sh -c "yarn install && yarn run dev"
But when I run it using docker-compose, it fails, without any errors (other than the working version). The docker-compose.yml is according to the tutorial:
version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
The logging output of the mysql container is the same, same warnings. Why don't I see anything in the browser?
mysql database
I can log into the database, using docker exec -it e46fae64e430 mysql -p todos
. But they don't point to the same todos
database. The 'composer' version is empty, the other contains the test data I entered before.
I have it running now, adding the following to the docker-compose.yml to force the apps to use the previously created network 'todo-app':
networks:
default:
external:
name: todo-app
This works now, but clearly, the default network should have worked as well. Anybody a suggestion what is causing this behaviour? A bug in docker(compose)? Something I did wrong somewhere?
Also what confuses me is that the mysql database in both situaties use the same volumes and give the same output on SHOW VARIABLES WHERE Variable_Name LIKE "%dir" ;
, but show other data.
I was having trouble getting docker-compose tutorial working. mysql container was not able to initialize the database. I found that volume "todo-mysql-data" from previous tutorial was still there and probably that was conflicting. So i removed that volume and tried again and it worked.
I'm having a similar problem with the tutorial. After running docker-compose, I open localhost:3000
but there's an error saying that the host hadn't sent any data. I check Docker dashboard and see that the app has crashed because it timed out, and that mysql is still setting up.
Once mysql is done setting up, I restart the app (from Docker dashboard) and then it runs without a problem. However, when I open localhost:3000
in the browser none of the items I previously added are there. It seems to be using a different (empty) mysql database? I run docker exec -it <CONTAINER> mysql -p todos
and then select * from todo_items;
, and the response is ERROR 1146 (42S02): Table 'todos.todo_items' doesn't exist
, which I think means that the list is empty.
I then run docker-compose down -v
to shut everything down and remove volumes, and go over the steps in the previous lesson to access mysql todos database. Once I'm there, I once again run select * from todo_items;
and, sure enough, the test data is there. So I'm guessing docker-compose is not connecting with that database, but creating a new one?
I tried adding the network info as suggested by @EelcoA but that didn't work for me.
Any help would be appreciated.
The docker-compose.yaml file should add the networks definition
version: '3.8'
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
- MYSQL_HOST=mysql
- MYSQL_USER=root
- MYSQL_PASSWORD=secret
- MYSQL_DB=todos
networks:
- todo-network
depends_on:
- mysql
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=todos
networks:
- todo-network
volumes:
todo-mysql-data:
networks:
todo-network:
The docker-compose.yaml file should add the networks definition
this also fixed for me this error - when no database todos
is created.