Day 14 Docker Part 3 ( Advanced )
Docker compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.
Compose works in all environments; production, staging, development, testing . It also has commands for managing the whole lifecycle of your application:
Start, stop, and rebuild services
View the status of running services
Stream the log output of running services
Run a one-off command on a service
command :
docker-compose down : stop the whole application
docker-compose up : start the whole application
What is YAML ?
YAML (YAML Ain't Markup Language or, recursively, "YAML Ain't Markup Language") is a human-readable data serialization format. It is often used for configuration files and data exchange between languages with different data structures. The syntax is simple and clean, relying on indentation and indentation levels to define the structure of data.
YAML is a popular programming language because it is human-readable and easy to understand.
YAML files use a .yml or .yaml extension.
Task-1
Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.
- Firstly Install Docker-compose if not installed in your system
The following command will download the 1.29.2 release and save the executable file at /usr/local/bin/docker-compose, which will make this software globally accessible as docker-compose:
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Next, set the correct permissions so that the docker-compose command is executable:
$ sudo chmod +x /usr/local/bin/docker-compose
To verify that the installation was successful, you can run:
$ docker-compose --version
Next clone the repo in your machine
git clone https://github.com/ApurvDevops/two-tier-flask-app.git
Once done delete the docker-compose. yml and try writing it on your own
version: '3'
services:
backend:
build:
context: .
ports:
- '5000:5000'
environment:
MYSQL_HOST: mysql
MYSQL_USER: admin
MYSQL_PASSWORD: admin
MYSQL_DB: testdb
depends_on:
- mysql
mysql:
image: mysql:5.7
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: testdb
MYSQL_USER: admin
MYSQL_PASSWORD: admin
volumes:
- ./message.sql:/docker-entrypoint-initdb.d/message.sql
- once this is done we can simply use below command to run container
docker compose up -d
-d will help to run in detached mode
- We can see the above message in table
we can tear down the container using one command
docker-compose down
Task-2
- Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint- Use
usermod
command to give user permission to docker). Make sure you reboot instance after giving permission to user.
docker pull testloginapurv/node-app
docker run -d -p containerport:hostport imagename/tag
- Inspect the container's running processes and exposed ports using the docker inspect command.
Below command shows container process
docker inspect --format '{{.State.Pid}}' <container name>
Below cmd displays container port
docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{$conf}} {{end}}' <container_name>
Blow are the snip that gives details of process and exposed ports
- Use the docker logs command to view the container's log output.
docker logs <containername>
Few flags that can be used with Docker logs
- Use the docker stop and docker start commands to stop and start the container.
docker stop <container name>
docker start <container name>
Use the docker rm command to remove the container when you're done.
Below you remove the contained, we need to stop itdocker stop <container name> docker rm <container name>
How to run Docker commands without sudo?
Make sure docker is installed and system is updated (This is already been completed as a part of previous tasks):
sudo usermod -a -G docker $USER
Reboot the machine.
Subscribe to my newsletter
Read articles from Apurv Samadder directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by