All you need to know about Docker Compose
docker-compose file create and run commands - build, up, down, ps, exec, start, stop, pause, unpause, restart and rm
What is docker-compose?
docker-compose is the tool to set up docker for multi-container environments. Using the single docker-compose file we can define all the containers with their environments.
docker-compose is always written in the yaml/yml file (docker-compose.yml).
It takes a single command to build the images and run the containers.
Before running the docker-compose we need to have Dockerfile for all the services. Click here for dockerfile project.
Next, create a docker-compose.yml file and below is an example of the file.
version : "3.3"
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123"
This is the docker-compose.yml file with two services one is the web and the second is db (web and db are just names given by the author). 'web' consist of nginx image and will run on port 80 whereas 'db' consists of MySQL database image and will work on port 3306:3306. There is an environment key that provides a password to MySQL.
We also need to define the version of the file.
Note: From July 1st version 1 will not support as per the docker docs.
Now to run docker-compose you need to have docker compose installed on your system.
If you don't have then run: sudo apt install docker-compose
or refer this link.
If you do not have docker then refer link here to install docker.
Commands for Docker Compose
build
The build option is used to build images for services for which the build is defined.
docker-compose build ## Build all services
docker-compose build web ## Build single service
up
The up command is used to create docker containers with available services in the docker-compose.yml file in the current directory. Use -d
switch to launch containers in daemon mode.
docker-compose up -d ## Create all containers
docker-compose up -d web ## Create single container
down
The down will stop and delete all the containers, network and associated images for the services defined in a config file.
docker-compose down ## Restart all containers
docker-compose down web ## Restart single container
ps
The ps command will list all containers created for the services defined in a config file with their status, port bindings and command.
docker-compose ps
exec
The exec commad is used to execute a command to the running container. For example list files in containers associated with web service.
docker-compose exec web ls -l
start
The start command is used to start stopped containers of the services defined in config file.
docker-compose start
stop
The command stop is used to stop running containers for the services defined in config file.
docker-compose stop ## Stop all containers
docker-compose stop web ## Stop single container
restart
The command restart is used to restart containers of the services defined in config file.
docker-compose restart ## Restart all containers
docker-compose restart web ## Restart single container
pause
The command pause is used to pause running containers for the services defined in config file.
docker-compose pause ## Pause all containers
docker-compose pause web ## Pause single container
unpause
The command unpause used to resume paused containers for the services defined in config file.
docker-compose unpause ## Start all paused containers
docker-compose unpause web ## Start single paused container
rm
The command rm is used to remove stopped containers for the services defined in config file.
docker-compose rm
That is all for the compose file. If you like the blog, do like, have any queries? do comment and share the blog.
You can also subscribe to the newsletter for updates on my blogs.
Follow me:
LinkedIn: https://www.linkedin.com/in/sonineelp