Docker Volumes and Docker-compose

Creating Docker Volumes and Docker-compose yaml scripts

·

2 min read

Creating Docker Volume

🧊 To create a new Docker volume, you can use the docker volume create command. For example, to create a volume named "myvolume", you would

# my volume is the volume name
docker volume create myvolume

🧊 Once the volume is created, you can use the docker volume ls command to see list of all volumes on your system.

🧊 To mount a volume to a container, you can use the -v or --mount option when running the docker run command.

For example, to run a container named "mycontainer" and mount the "myvolume" volume to the "/data" directory within the container, you would run:

#jenkins is the name of the image
docker run -it --name mycontainer -v myvolume:/data jenkins

You can also use the following command to inspect the volume

Copy codedocker volume inspect my_volume

And when you no longer need the volume you can remove it by using the command

Copy codedocker volume rm my_volume

Docker compose

Docker Compose is a tool that is used to define and run multi-container Docker applications. It allows you to use a YAML file to configure the services, networks, and volumes that make up your application, and then start and stop the application using a single command.

We can run the container without using run command.

Install docker-compose if it is not available. By default, it won't be there.

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


sudo chmod +x /usr/local/bin/docker-compose

or 
sudo apt-get install docker-compose


docker-compose --version

Create a YAML file> docker-compose.yaml

run the docker-compose up command

sudo docker-compose up

It will run the images .

you can see the apps on the particular ports allocated in the file.

ex: we used nginx image, we need to see this app by 80 port

To stop the services use

sudo docker-compose down

Thanks for reading.

Â