DEV Community

Cover image for Learn Docker — from the beginning, part V Docker Compose, variables, volumes, networks and databases

Learn Docker — from the beginning, part V Docker Compose, variables, volumes, networks and databases

Chris Noring on February 28, 2019

Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris This article is part of a series: Docker - from the begi...
Collapse
 
edssatish profile image
eds-satish

Thank you so much for sharing your valuable knowledge. This is such a gem!!!

Collapse
 
softchris profile image
Chris Noring

glad to hear it

Collapse
 
thiago_tgo90 profile image
Thiago Oliveira

Hi, tks for these articles. They are very helpful.

Running the example from your repo (github.com/softchris/docker-compos...) I had a problem when I tried to start the containers.

When I run the code the way it is, I got the following error in the console (I increased the default timeout. Thought maybe it would be the problem):

wait-for-it.sh: timeout occurred after waiting 60 seconds for db:8000

When I update the compose file to use the container db port instead of the one mapped in the host, it works:

wait-for-it.sh: db:3306 is available after 7 seconds

I think, it kinda makes sense because one container will be talking to the other one without using the host (maybe?)

Sorry if you explained/fixed this and I missed the piece.

tks a lot.

Collapse
 
bijoy26 profile image
Anjum Rashid • Edited

I ran into same timeout issue as Thiago on Windows host and solved it by updating the internal DB port as instructed.

However, then I stumbled upon following issue on the product-service container:

sh: nodemon: command not found

Few hours of head scratching and unhealthy debugging later, the culprit turned out to be bind mount wiping out the node_modules directory. Got a workaround with this solution.

Better late than never!

Collapse
 
softchris profile image
Chris Noring

thank you for lettting me know. I'm sure someone else will scratch their head here and see your comment

Collapse
 
softchris profile image
Chris Noring

hi Thiago.
Thanks for writing this. I'm trying to understand what kind of OS you are on, linux, mac, windows? Just trying to rule out if it is OS dependent or not?

Collapse
 
thiago_tgo90 profile image
Thiago Oliveira

Hi Cris

I'm using Ubuntu 18.04.2 LTS and Docker (Client and Host) 18.03.1-ce

Thank you.

Collapse
 
opensas profile image
opensas

WOW! Excellent guide indeed.

I got a bit lost in the last step. What does the "ADD init.sql /docker-entrypoint-initdb.d" really do (just copying the file?) and how come mysql knows it has to run it? Or is it something programmed in the mysql:5.6 image? and how does it auths to the db?

The other question I wanted to ask, is how would you combine all this with a git repo in order to trigger automated tests and, if everything goes ok, automatic deploy?

Collapse
 
softchris profile image
Chris Noring

Thanks for that :)

From hub.docker.com/_/mysql
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d

if I understand your question correctly that's a whole chapter in the official Docker documentation, docs.docker.com/docker-hub/builds/

Collapse
 
israelmuca profile image
Israel Muñoz

THIS IS AMAZING!

Thank you SO MUCH!

Now I have an urgent need to refactor mi monolithic API into microservices with Docker 😄

Collapse
 
softchris profile image
Chris Noring

That was the effect I was hoping for :) Good to hear you are finding it useful :)

Collapse
 
harkinj profile image
harkinj

Brilliant series of articles.I have recommended it to all my colleagues. Thanks very much.

Collapse
 
softchris profile image
Chris Noring

happy to hear that it helps someone. Thank you :)

Collapse
 
harkinj profile image
harkinj

On a broader subject. Do u think in the future will devs have to worry about setting up docker or k8s etc or rather just leverage a PaaS such as cloud foundry? I like dev but not ops :)

Thread Thread
 
softchris profile image
Chris Noring

That's a good question. Cloud is becoming more of a default. Tools will become better. It will be easier to do these things. There will be more services with a one click and you are in the cloud or just knobs and levers to pull to scale your app up and down. With that said there will always be a need to build these tools, question is it that falls into the laps of normal devs... DevOps is a very strong movement at the moment and understanding dev + devops imo makes you more into an architect

Thread Thread
 
harkinj profile image
harkinj

Thanks for the info and your time. I believe the whole ops side of devops will die off in 3-5 years as the tooling, PaaS (e.g cloud foundry) etc make it easier to any ops required and we will just e back to dev again :) lets wait and see. There might be a topic for a blog in our mini discussion :)

Collapse
 
isitavi profile image
Avijit Das Noyon

From:

conn.on('error', function(err) {

if(err) {

console.log('shit happened :)');

connect()

}

})

To:
con.on('error', function(err) {

if(err) {

console.log('shit happened :)');

connect()

}

})

one 'n' need to Exclude

Collapse
 
araphiel profile image
Armon Raphiel

Thank you for posting this series.

My biggest question: How do you deploy a docker-compose setup?

It seems like I only see people using compose for development.

  • Can we use it to deploy to a server?
  • Should we be using docker-compose at all for production servers?
  • What is the easiest way to get a bunch of containers running on a cloud service like Azure
Collapse
 
softchris profile image
Chris Noring

Here's an example of that xhttps://docs.microsoft.com/en-us/azure/app-service/containers/quickstart-multi-container

Collapse
 
narayanncube profile image
narayanncube

attempting to connect to DB time: 1
Example app listening on port 3000!
Error { Error: connect ECONNREFUSED 192.168.0.3:3306
at TCPConnectWrap.afterConnect as oncomplete
--------------------
at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/app/node_modules/mysql/lib/Connection.js:119:18)
at connect (/app/app.js:27:7)
at Object. (/app/app.js:45:1)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.0.3',
port: 3306,
fatal: true }

I get this error

Collapse
 
goranpaunovic profile image
Goran Paunović

Great series of articles. It really helps to write everything down as you learn. You will learn it better and you can help others to learn from your mistakes. I hope you will continue to learn us as you learn more.

Collapse
 
elrumordelaluz profile image
Lionel

Hey Chris, thank your for the whole serie!!

Just a little detail to avoid errors for new readers, in the Turn your current directory into a Volume section, instead of version: 3 in the docker-compose.yaml, needs to be at least version 3.2 as you have in the repository file.

Again, thanks for the pretty well explained serie of posts!

Collapse
 
softchris profile image
Chris Noring

hi Lionel. Thanks for reaching out. Really appreciate your comment. I'll make sure to update

Collapse
 
jagtapmv profile image
Mahesh Jagtap

Great article!, and thanks for that.

But, as in first 3 articles you used single/same example and that was very easy to understand. In article number 4 and 5 you used unrelated examples to first 3. If you had used the same example, it would have been great.

Also, can you add articles for continuous integration and deployment of docker containers?
Thanks, again!

Collapse
 
clhenrick profile image
Chris Henrick

Thanks for this excellent and well written walk through of Docker, it's helped me out quite a bit with a personal project I'm working on.

A couple important things that I didn't quite catch from this post relating to how the database container communicates to the service container that I think are worth calling out:

  1. When you reference your database from your app service, the database host name is the same as the database service in the docker-compose.yml In your example this is "db". This seems obvious now but wasn't apparent to me at first.

  2. For other database systems such as PostgreSQL where you need to specify the port number in the connection, the port to use is the container port, not the host port that it's mapped to. Using your example this would be 3306 not 8002.

For my use case I'm using PostgreSQL and the node-postgres library, so it's slightly different than MySQL, but I imagine others may run into this as well!

Collapse
 
alexmenor profile image
Alex Menor

Thank you for this whole series!

Collapse
 
softchris profile image
Chris Noring

Thanks Alex :)

Collapse
 
maccevedor profile image
Mauricio Acevedo Rueda

Thanks

Collapse
 
softchris profile image
Chris Noring

Hi Mauricio, glad it's helpful :)

Collapse
 
npras profile image
Prasanna Natarajan

Thanks for this series. I learned few things about a thing called Docker.

Collapse
 
artoodeeto profile image
aRtoo

@softchris hi chris I really liked this article. quick question though. Should we still use compose or stack? thank you. :)

Collapse
 
softchris profile image
Chris Noring

If I understand correctly Stack are used with Docker Swarm.. so for me your question becomes more should I use Docker Swarm or Kubernetes as an orchestrator? Both solve the job, I would go Kubernetes, it's more known, though but take that with a grain of salt. I need to deep dive more in Docker Swarm and Kubernetes