Common Docker Compose commands
Docker is fantastic tool to containerize a service and all it's dependencies. However, in practice rarely do you use a service by itself. Typically, you will have multiple services (or micro-services) collaborating to provide all the features of a customer facing application.
Docker compose is a tool from Docker that allows you to quickly and reliably spin up multiple containers locally on your Mac, Windows or Linux desktop. It provides constructs to:
- Put all the related containers in one docker-compose file
- Ensure they spin up in certain order (E.g. a database should be up before an application calls it)
- Are part of same local network so they can send API requests to each other
Without Docker compose it would be quite a pain to do all the above yourself.
Here are some commands i use frequently when running docker compose:
- Let's say you modified one of the dockerfile or the service code. You then run the below command to build a new image:
- docker compose build <service_name>
- Once you build the image, then you will need to publish it to a public or private registry.
- docker push <image name from step 1>
- Now run below command to spin up ALL the services that are part of the docker compose file:
- docker compose up -d
- Sometimes you need to recreate docker images of all the services and then start the respective containers:
- docker-compose up -d --build --force-recreate
- To stop and restart specific docker services:
- docker-compose stop <service_name>
- docker-compose restart <service_name>
Comments
Post a Comment