Managing Docker containers. šŸ³

Ankit Wadhwana
2 min readSep 26, 2021

--

Today weā€™ll be looking into two things.
First is how we can persist the data with docker-compose.
The second is using a script to manage your docker composes.

Persisting data with docker-compose:

Use volumes: Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.
In addition, volumes are often a better choice than persisting data in a containerā€™s writable layer, because a volume does not increase the size of the containers using it, and the volumeā€™s contents exist outside the lifecycle of a given container.

A single docker-compose service for Redis with a volume looks like this:

On the first invocation of docker-compose up the volume will be created. The same volume will be reused on the following invocations.
Use docker-compose up -d for running the process in the background.
If a volume created outside of the compose needs to be attached, the external true property:

volumes:
myapp:
external: true

Note:
docker-compose down would not remove the volume attached to your container, but docker-compose down -v will.
Alternatively, you can use this docker volume rm <volume-name> to remove the volume. Use docker volume ls to get the volume name.

Smartly manage your docker composes.

You can use a docker-desktop client to manage your Docker containers and do much more. For me, I just needed a simple script that will start the docker containers after my machine has started.
For which I created the following script, which works perfectly with
Docker version 20.10.7, build f0df350

Note:
For the script to work, create a main directory with sub dirs containingt the docker-compose.yml.

This is the directory structure I currently have.

dir structure for the docker-compose.

Iā€™m further planning to extend this script to be dynamic and support the docker-compose down command too.

Contact:
Email: 24ankitw@gamil.com
Github: https://github.com/awadhwana
Linkedin: https://www.linkedin.com/in/ankit-wadhwana/
IG: https://www.instagram.com/ankitwadhwana/

--

--

Ankit Wadhwana
Ankit Wadhwana

Written by Ankit Wadhwana

Live in the moment, be happy (:

No responses yet