Tool provided by docker to build
and run multi container applications.
Services are components of your architecture. Every service in a docker compose file is associated with only one image.
We should design in such a way
that there is one image for the backend, one image for database, one image for
front end, api gateway and so on.
Limitation of docker-compose is that it works only on a single machine. We cannot run the docker-compose on a cluster. docker-swarm is a orchestration solution from docker.
Then why are we using docker-compose
The docker-compose file that is
used on a single machine can be used on a cluster docker-swarm scenario.
Very much helpful for a
development scenario.
The name docker-compose.yml is the default name of the compose file. The docker-compose command will look for the Compose file by this name.
docker compose build
version: '3.7'
services:
queue:
image: rmohr/activemq:5.15.6 ports:
- "${queueConPort}:8161" - "${queuePort}:61616" environment:
ACTIVEMQ_OPTS: "-Xms64m -Xmx2g"
proc:
build:
context: ./procdir
environment:
- SPRING_ACTIVEMQ_BROKER_URL=tcp://queue:61616
Building without cache
C:\Deployment>docker-compose
build --no-cache
To bring up the application
C:\Deployment>docker-compose
up -d
Networking in compose
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
C:\Deployment\sample>docker
network ls
NETWORK ID NAME DRIVER SCOPE
cc3f5301d2e9 bridge bridge local
6559e0781a5c docker-compose_default bridge local
1143df529db9 host host local
8499a7131938 none null local
035a925c6b97
sample_default
bridge local
For example, suppose your app is
in a directory called myapp, and your docker-compose.yml looks like this:
version:
"3"
services:
web:
build: .
ports:
- "8000:8000"
db:
image: postgres
ports:
- "8001:5432"
When you run docker-compose up,
the following happens:
1.
A network called myapp_default is created.
2.
A container is created
using web’s configuration. It joins the network myapp_default under the
name web.
3.
A container is created
using db’s
configuration. It joins the network myapp_default under the name db.
docker inspect on the network will yield the following
C:\Deployment\sample>docker
inspect xsightdev_default
[
{
"Name": "sample_default","Id":"035a925c6b97d041145f4162b8df593888b7a89cdd80bfc1
2f6a740cf4b16c0a","Created": "2020-04-01T15:11:54.8837377Z","Scope": "local","Driver":
"bridge","EnableIPv6": false,"IPAM": {
"Driver": "default","Options": null,"Config": [
{
"Subnet": "172.19.0.0/16","Gateway": "172.19.0.1"}
]
},"Internal": false,"Attachable": true,"Ingress": false,"ConfigFrom": {
"Network": ""},"ConfigOnly": false,"Containers": {
"1cfe54891c042d100d17652444e90269445a4f24e984a53a212b0c838598cb45": {
"Name": "sample_queue_1","EndpointID":
"2dfced06fc92192901365f1ec65687687a6df7dce244d20d6ba175295b0d6dcc",
"MacAddress": "02:42:ac:13:00:02","IPv4Address": "172.19.0.2/16","IPv6Address": ""},
"500798d2fb86cccf474ceecc99e2c9a013afbaa3004cd86a377a423c5e0ed38b": {
"Name": "sample_proc_1","EndpointID":
"07c3af9b4108ab1cd70ef26f56dc61aa8a1ccf131b1a1997792f9b1134a9d383",
"MacAddress": "02:42:ac:13:00:03","IPv4Address": "172.19.0.3/16","IPv6Address": ""}
},"Options":
{},"Labels": {
"com.docker.compose.network": "default","com.docker.compose.project":
"sample","com.docker.compose.version": "1.24.1"}
}
]
C:\Deployment\sample>
The containers that were created
are connected to the default network.
The containers that are connected to this network can communicate with each other by name, that is we can use the name of the service to be used different services to talk to each other.
docker-swarm does n't support build.
You can use couple of docker commands in docker-compose
List all the containers by docker-compose ps
This lists all the processes.docker-compose top
Follow the logs of all the containers by docker-compose logs -f
You can an independent service outside the docker-compose by
docker-compose run proc
Here the ports won't be mapped.
docker-compose up --scale proc=3
You can use docker-compose to
start more than one container for a particular service
C:\Deployment\sample>docker-compose
up --scale proc=3 Starting
sample_proc_1 ...
Starting sample_proc_1 ... done
Creating sample_proc_2 ... done
Creating sample_proc_3 ... done
If we want to run an application with worker containers
If there is .env file in the folder where docker-compose is running that file would be picked up by the docker-compose
How to use multiple compose files in a single project
Getting an image to Docker Hub
Sign up for an account on https://hub.docker.com/.
2. Click on Create Repository.
3. Log into the Docker Hub from the command line
C:\Deployment\sample>docker login --username=madhutomy
Password:
Login Succeeded
4. Check the image that you wanted to push to hub
C:\Deployment\sample>docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomymad/proc latest 21a3053e4529 11 hours ago 4.43GB
5. Tag your image
C:\Deployment\sample>docker tag 21a3053e4529 madhutomy/proc1:first
6. Push your image to the repository you created
C:\Deployment\sample>docker push madhutomy/proc1 The push refers to repository [docker.io/madhutomy/proc1] 6f301a019730: Pushed c5feef8de600: Pushed 580f3756ce8a: Pushed cc785f55844a: Pushed 5fe84091d04f: Pushed 5d3cbc907abc: Pushing [======> ] 475.5MB/3.859GB 80246f30af28: Pushed d69483a6face: Pushed
7. Your image is now available for everyone to
use.
No comments:
Post a Comment