Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding docker -v command

I was just going through this tutorial on Youtube, trying to understand the use of the -v command.

Why is the author using the -v command? He uses the command, like so:

docker run -v /var/lib/mysql --name=my_datastore -d busybox echo "my datastore"

Now I understand the above command to an extent:

  • --name=my_datastore gives the container a specific name.
  • -d busybox starts a container in detached mode, based on the busybox image.

After the command is executed the below line is echoed to the console.

my datastore 

Now, the part I don't understand is the following:

-v /var/lib/mysql

Why is the -v command used here, and why the path /var/lib/mysql specified?

I am having difficulty understanding why the above line is used and in what context. Can anybody explain?

like image 708
Tenali_raman Avatar asked Aug 28 '15 11:08

Tenali_raman


People also ask

How does Docker command work?

Docker CMD defines the default executable of a Docker image. You can run this image as the base of a container without adding command-line arguments. In that case, the container runs the process specified by the CMD command.

How do I read a Dockerfile?

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession. This page describes the commands you can use in a Dockerfile .

How Docker works step by step?

The run function of the docker command tells Docker to find a specified Docker image and start a container running that image. By default, Docker containers run in the foreground. That means, when you execute docker run, your shell will be bound to the container's console and the process running within the container.


1 Answers

The -v (or --volume) argument to docker run is for creating storage space inside a container that is separate from the rest of the container filesystem. There are two forms of the command.

When given a single argument, like -v /var/lib/mysql, this allocates space from Docker and mounts it at the given location. This is primarily a way of allocating storage from Docker that is distinct from your service container. For example, you may want to run a newer version of a database application, which involves tearing down your existing MySQL container and starting a new one. You want your data to survive this process, so you store it in a volume that can be accessed by your database container.

When given two arguments (host_path:container_path), like -v /data/mysql:/var/lib/mysql, this mounts the specified directory on the host inside the container at the specified path (and, to be accurate, this can also be used to expose host files inside the container; for example -v /etc/localtime:/etc/localtime would make /etc/localtime on the host available as /etc/localtime inside the container). This is a way of either feeding information into your container, or providing a way for your container to make files accessible to the host.

If a container has volumes available, either through the use of the -v command line argument or via the VOLUME directive in a Dockerfile, these volumes can be accessed from another container using the --volumes-from option:

docker run --volumes-from my_datastore ...

This will make any volumes defined in the source container available in the container you're starting with --volumes-from.

This is discussed in more detail in the Docker Volumes documentation.

like image 102
larsks Avatar answered Oct 17 '22 13:10

larsks