Menu

Poland

GRANDMETRIC Sp. z o.o.
ul. Metalowa 5, 60-118 Poznań, Poland
NIP 7792433527
+48 61 271 04 43
info@grandmetric.com

Sweden

Drottninggatan 86
111 36 Stockholm
+46 762 041 514
info@grandmetric.com

UK

Grandmetric LTD
Office 584b
182-184 High Street North
London
E6 2JA
+44 20 3321 5276
info@grandmetric.com

US Region

Grandmetric LLC
Lewes DE 19958
16192 Coastal Hwy USA
EIN: 98-1615498
+1 302 691 94 10
info@grandmetric.com

  • en
  • pl
  • se
  • Docker-compose commands for container management

    Docker-compose commands for container management

    Date: 17.07.2020

    Category: DevOps


    The docker-compose is a useful tool for orchestrating multi-container Docker applications. It allows us to run applications based on a YAML file. This file is a list of instructions that define the parameters of the launched applications. For example, we can run two containers – database MySQL and base web application WordPress. The docker-compose files might look like this:

    version: '3.3'
    
    services:
       db:
         image: mysql:5.7
         volumes:
           - db_data:/var/lib/mysql
         restart: always
         environment:
           MYSQL_ROOT_PASSWORD: somewordpress
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: wordpress
    
       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         ports:
           - "8000:80"
         restart: always
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: wordpress
           WORDPRESS_DB_NAME: wordpress
    volumes:
        db_data: {}

    How to manage multi-container application?

    To manage containers we can use a few basic docker-compose commands:

    • Build or rebuild services
    docker-compose build
    • Create and start containers
    docker-compose up

    The containers have been run and logs printed on the console. To run in background use parameter -d.

    • Run a one-off command on a service
    docker-compose run <service> <bash_command>
    ex: docker-compose run wordpress <service> <bash_command>

    For example, print Debian version of a WordPress container.

    • Show the list of containers running on a machine
    docker ps

    The above command presents the information about

    • container id,
    • image name and version,
    • how long ago the container was created,
    • status,
    • ports and container name.
    • Print list of information about running containers from current docker-compose.yaml
    docker-compose ps
    • Show all logs in container since its start
    docker logs <service>
    • Stop and remove containers, networks, volumes, and images created by the command “up”
    docker-compose down
    • Kill one or more running containers
    docker kill <container_id>
    • Show images list
    docker images
    • Execute a command in a running container
    docker exec <service> <bash_command>
    • Remove all unused data
    docker system prune

    Useful command combinations

    • Kill all run containers
    docker kill $(docker ps -q)
    • Stop all run containers
    docker stop $(docker ps -q)
    • Remove all images
    docker rmi $(docker images -q)
    • Remove all containers
    docker rm $(docker container ps -q)

    Summary

    Docker-compose is a useful and easy tool to manage multiple containers. You can decide how containers are separated, what resources they can use, and how they communicate with each other. You can also set what should happen if the status of one of the containers changes. This feature allows you to restore the state of the system automatically.

    I invite you to explore the entire IoT analysis and management system.

    If you have any question, leave it in the comment, I will be happy to answer 😉

    References

    [1] Official documentation https://docs.docker.com/compose/

    [2] Quickstart: Compose and WordPress
    https://docs.docker.com/compose/wordpress/

    Author

    Mateusz Sobkowiak

    Mateusz Sobkowiak received his B.Sc. degree from Poznan University of Technology in 2018 majoring in electronics and telecommunications. He is currently studying computer science at the same faculty. His interests include IoT, Smart Home, K8s and ML.

    Leave a Reply

    Your email address will not be published. Required fields are marked *


    Grandmetric