Sharing data between the host machine and the container, between containers

Tram Ho

Hello everyone, continue with the 2 series of Docker practices, now we learn and practice with the content about sharing data in docker.

What is data sharing in docker: data sharing in docker is when data is used in many places, many containers or data is stored on the host machine but the containers want to use and modify them then we will shares that data with the containers that have access to it.

For example, we have 2 files File1.txt and File2.txt stored on the host machine and there are 3 containers, container_1 container_2 and container_3, we share these files for all 3 containers to use, it is data sharing. in docker

Share data between host machine and container

In this practice we will create a data directory inside that has a file hello.txt on the host machine and share this directory with the container.

1. Create data on the host machine

We create the data directory at the desktop and contain the file hello.txt

Using the pwd command, we get the path to this directory: /home/nguyen.van.thinhb/Desktop/data

2. Share the data folder that has just created a new container

We will create a container from the ubuntu image and receive the shared data as the host machine’s data directory with the command

To share data between the host machine and the container, we add the -v parameter

In order for the container to be able to receive interactions and connect to the terminal, add the -it parameter

/home/nguyen.van.thinhb/Desktop/data : is the path to the directory containing the data on the host computer to share (we just used the pwd command above to get this path)

/ home / data_container is the path to the directory storing data on the container

ubuntu is the ubuntu image used to create the container

After running the above command, we have created a container from the ubuntu image and standing in the terminal of that container, cd to the home directory will see the data_container directory and have file hello.txt => shared data from the host machine into the container

Data between these two folders is mapped together, if the container stores data in this directory, it will also be stored on the host, when deleting the container, the data will still be stored on the host machine.

Share data between 2 containers

Problem: we have a data directory on the host machine and want to share this directory for container_1 to use, and we also share this directory for container_2 to use.

Here we continue to use the above practice (sharing data between the host machine and the container), we already have the data directory on the host machine and have shared it for a container as above.

Next, we will create another container and share data from the old container

Still in the old container, press Ctrl + p and Ctrl + q to exit the container but still run that container

running docker ps will see that container is still running.

Now we will create a new container that is sharing data from the data_container directory of the previously created container using the command

The above command we create a container named C2 from the ubuntu image and share data from the container with an Id of b7cf8479548f (previously created container).

After running the above command, we are standing in the newly created C2 container, cd into the home directory will see the shared data_container directory

Create and manage drives (volumes), to share data drives for containers

In addition to sharing data between the host machine and the containers, in docker also allows us to share data through creating and managing drives, the drives are created and assigned to containers, when the container is damaged. If you delete the existing, rhyming drives, they are only lost when you delete the drives

1: Create a drive

First we have the command docker volume ls to see a list of drives

Here I have many available drives in use

To create a drive named D1 we use the command: docker volume create D1

Check the drive information: docker volume inspect D1

Erase the drive: docker volume rm [Ten]

2: Assign the drive to the container

We have created drive D1, now we will create a container and assign drive D1 to that container so that the contaner can use drive D1 with the following command:

docker run -it --name container1 --mount source=D1,target=/home/disk2 ubuntu

The above command is that we create the container which should be container1

The –mount parameter is to assign additional drives when creating the container

source = D1 is the drive parameter to assign to the container

target = / home / disk2 where / home / disk2 is the directory to map drive D1 to the container

We exit and delete that container, the drive will still be there

3: Create a drive mapped onto a foder on the host machine

We want to create a fixed-mapped D2 drive to an available folder on the host machine using the command

docker volume create --opt device=/home/nguyen.van.thinhb/Desktop/data/ --opt type=none --opt o=bind D2

The first parameter --opt device=/home/nguyen.van.thinhb/Desktop/data/ is the path to the directory on the host machine

The second parameter type = none

Drive D2 has been created and mapped to a directory on the host computer, we can test the drive with the docker volume inspect D2 command.

4: create a container with a local mapped drive

docker run -it --name=container3 -v D2:/home/disk ubuntu

The above command creates a container called container3 from the ubuntu image, assigns drive D2 to the path / home / disk on the container3, D2 is created with a foder on the host machine above.


You should practice a lot to remember and better understand, in the next section, I will write about Network in Docker. Thank you everyone for watching.

Share the news now

Source : Viblo