Tutorial for sharing data between Docker container and host



In general, Docker containers do not work for a long time. In fact, these containers are temporary and will run until the command issued in the container is completed. All data generated in the container will only be available in the container when executed. In this article, we will teach you how to share data between a Docker container and a host.

Data sharing between Docker container and host

Docker volumes can be used to share files between host systems and docker containers. For example, if you plan to use the official Docker Nginx image and keep a permanent copy of the Nginx report files for analysis, by default the Docker Nginx image will go to the var / log / nginx folder and the Docker Nginx container.

Information between host, container and docker

How to connect container, docker and host?

prerequisites

To do this, you need an Ubuntu 18.04 server that has the following prerequisites:

  1. A non-root user with sudo privileges who follows the initial Ubuntu server installation guide.
  2. Docker with the instructions for the first and second steps, which are described below and are used in Ubuntu.

Note: Be sure to note that even if the prerequisites provide instructions for installing Docker on Ubuntu 18.04, Docker data volume commands must run on other operating systems while Docker is installed.

Stage 1. Binding volume

The following command creates a directory called nginxlogs in your current user’s root directory and moves it to / var / log / nginx in the container:

$ docker run --name=nginx -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 nginx

We will now take a closer look at this command:

  • -Name = nginx names the container so that we can access it more easily.
  • -d is responsible for the execution of the process and its execution in the background; Otherwise we will only see the empty Nginx notification. Note that we will not be able to use this terminal until we kill Nginx.
  • -v ~ / nginxlogs: / var / log / nginx Sets the volume of Bindmount and associates the / var / log / nginx folder of the Nginx container with the ~ / nginxlogs directory of the host machine.
  • Docker uses the path of the container to split the path of the host, and the path of the host always precedes the path of the container.
  • -p 5000: 80 also sets the forwarding port. The Nginx container will call port 80 by default. This flag retrieves port 80 containers to port 5000 on host systems.
  • nginx specifies that the container must be made from an Nginx image that will issue the nginx -g “daemon off” command to start Nginx.

Connect to cloud container and docker and hosts

Docker team in Ubuntu 18.04

Note: The -v flag is very flexible. This allows you to name or associate the volume with a slight change in settings.

If the first argument starts with / or ~ /, you create Bindmount.

-v / path: / path / in / the container installs the host directory; / path در / path / in / container

-v path: / path / in / container also creates a volume called path without connection to the host.

Step 2. Access host data

We already have a copy of Nginx running in the Docker container of our machine or host and the 5000 port of our host machine is written directly to this copy of the 80 Nginx port.

In the web browser of your choice, load the address using your IP or hostname, and the port number is http: // your_server_ip: 5000.

Data sharing between Docker container and host

Create an nginx container

It is interesting to know that if you look at the ~ / nginxlogs list in the host, you will see access.log created by the nginx container. This will indicate your request:

$ cat ~/nginxlogs/access.log

This command should look like this:

Exit

۲۰۳٫۰٫۱۱۳٫۰ - - [۱۱/Jul/2018:00:59:11 +0000] "GET / HTTP/1.1" 200 612 "-"
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" "-"

If you make changes to the / nginxlogs folder, you can see them from the Docker container.

Conclusion

In this article we will teach you how to share data between container, docker and host. With this tutorial, you can easily create a Docker data volume to share information between the container and the host’s file system.

Frequently Asked Questions

1. How long do Docker containers last?

Docker containers are temporary and will run until the command issued in the container is completed.

2. How can we share files between systems and Docker containers?

You can use Docker volumes to share files between host systems and Docker containers.

3. What tools do you need to use this method to share files between systems and the Docker container?

To Ubuntu server 18.04

{“@context”: “https://schema.org”, “@type”: “FAQPage”, “mainEntity”: [{
“@type”: “Question”,
“name”: “کانتینرهای داکر تا چه زمانی کار می‌کنند؟”,
“acceptedAnswer”: {
“@type”: “Answer”,
“text”: “کانتینرهای داکر حالت موقتی دارند و تا زمانی کار خواهند کرد که فرمان‌ صادرشده در کانتینر تکمیل شود.”
}
},{
“@type”: “Question”,
“name”: “چگونه می‌توانیم فایل‌ها را بین سیستم هاست و کانتینر داکر به‌اشتراک بگذاریم؟”,
“acceptedAnswer”: {
“@type”: “Answer”,
“text”: “از حجم‌های Docker می‌توانید برای اشتراک‌گذاری فایل‎ها بین سیستم هاست و کانتینر داکر استفاده کنید.”
}
},{
“@type”: “Question”,
“name”: “استفاده از این روش برای اشتراک‌گذاری فایل‌ها بین سیستم هاست و کانتینر داکر به چه ابزاری نیاز دارد؟”,
“acceptedAnswer”: {
“@type”: “Answer”,
“text”: “به سرور اوبونتو ۱۸٫۰۴”
}
}]
}

A lesson on sharing data between a Docker container and a host for the first time in Parsupak. appear.

Leave Your Comment

Your email address will not be published.

Supportscreen tag