Sunday , July 21 2019
Home / Uncategorized / How to keep data in the mobile container

How to keep data in the mobile container



TL; DR

Containers should be light. Adding unnecessary data will make it difficult to create and execute. Docker offers several ways to mount storage space from the host machine to containers. Volumes are the most commonly used. It can be used to maintain application data and also to share data between multiple containers. (However, local volumes can not be shared between docker services, but you will need shared sharing).

background

I listened to dockers and containers some time ago, however, I'm new to using them. I have only recently started to explore because it helps to create Web services and easily implement on multiple operating systems. (They are fantastic tools!)

For one of the Web services, its job is to create / update / activate another virtual environment and perform an & # 39; activity using that environment. Different requests sometimes require a different virtual environment. The requirements.txt the file for each virtual environment is synchronized from time to time, then pip install is called to update the virtual environment. pip install it can take time and must be called as few times as possible. This means that the web service must maintain virtual environments so that when the service is restarted, it does not have to repeat the environment creation / update processes.

Here the problem arises that, every time a new image has been created for the web service, obviously it does not have the virtual environments stored in the old container. This makes the service "very cold". To solve it, I initially thought of transferring the changes from the old container to the new image. However, this greatly increases the size of the image and the container.

After some hours of research in the documentation of the docker, I realized that so far I thought that the containers were "completely self-sufficient", while it has more power when working with its host machine.

Solution

Docker provides three ways to mount data on the container: volumes, bindings, and tmpfs storage [1].

  • volumes they are part of the host filesystem, but they are managed by the floating window in the specific path and should not be modified by other applications
  • Bind mounts it can be anywhere on the host, but it can be modified by other applications
  • tmpfs they are in the memory space of the host and are never written to the filesystem.

In general, volumes are the ideal solution to solve most data persistence problems in a container. Volumes can be created from create the volume of the floating window command, or created when a container is started.

Examples like my solution

The mobile window documentation is here [2].

1. Create volume

First, let's create a volume called as virtualenv to act as a path to store virtual environments.

➤ volume docker creates virtualenv

We can control the volume with the following command

➤ volume docker inspect virtualenv
[[[[
    {
        "CreatedAt": "2018-09-15T05: 29: 36Z",
        "Driver": "Local",
        "Labels": {},
        "Mountpoint": "/ Var / lib / floating window / volumes / virtualenv / _data",
        "First name": "Virtualenv",
        "Options": {},
        "Purpose": "Local"
    }
]

2. Create container

The example app structure looks like this:

  • Dockerfile
  • main.py: the access point
  • create_env.sh (used to create another virtual environment)

What main.py does to check if the "my_env" virtual environment exists. Otherwise, it will create it. We will mount the volume created above as ~ / .virtualenv folder in the container.

I use the following Dockerfile to create the simplest python image:

FROM    python: 3.7
WORKDIR    / app
INSERT    . / app
TO RUN seed to install virtualenv
CMD ["python", "./main.py"]

main.py It looks like this:

import os
import under process

DEF main():
    Self os.pathway.exists(& # 39; /root/.virtualenv/my_env'):
        print("my_env already exists")
    other:
        under process.to run([[[[& # 39; & # 39 Bash;, & # 39; Create_env.sh & # 39;])
        print("my_env created")

Self __first name__ == & # 39; main __ __ & # 39;:
    main()

And the line create_env.sh

CD ~ / .virtualenv / && virtualenv my_env

3. Start the container with the mounted volume

First we build the python image:

➤ build docker -t mobile-data-persistence window .

So to mount the volume, we use --montare discussion:

➤ run docker 
  --montare source=virtualenv, target=/root/.virtualenv 
  mobile-data-persistence window

Using the base prefix & # 39; / Usr / local & # 39;
New python executable in /root/.virtualenv/my_env/bin/python
Installation of setuptools, pip, wheel ... done.
my_env created

As we can see above, when we will run the container for the first time, the "my_env" virtual environment will be created as it does not yet exist in the volume. If we run it the second time, it will say that "my_env" already exists.

➤ run docker 
  --montare source=virtualenv, target=/root/.virtualenv 
  mobile-data-persistence window

my_env already exists

4. Inspect the volume

We can look at the files in the volume (in a hacky way) [3]) to check the contents:

➤ run docker -is 
  --montare source=virtualenv, target=/root/.virtualenv 
  mobile-data-persistence window 
  find /root/.virtualenv/my_env/bin

/root/.virtualenv/my_env/bin
/root/.virtualenv/my_env/bin/python3
/root/.virtualenv/my_env/bin/activate.csh
/root/.virtualenv/my_env/bin/easy_install-3.7
/root/.virtualenv/my_env/bin/python
/root/.virtualenv/my_env/bin/python-config
/root/.virtualenv/my_env/bin/easy_install
/root/.virtualenv/my_env/bin/python3.7
/root/.virtualenv/my_env/bin/activate
/root/.virtualenv/my_env/bin/pip
/root/.virtualenv/my_env/bin/activate.fish
/root/.virtualenv/my_env/bin/pip3
/root/.virtualenv/my_env/bin/wheel
/root/.virtualenv/my_env/bin/activate_this.py
/root/.virtualenv/my_env/bin/pip3.7

5. Delete the volume

To eliminate the volume, we can use volume docker rm . However, it is not possible to delete a volume when there is a container that uses it, even if the container has been closed.

➤ volume docker rm virtualenv
Error response from the daemon: remove virtualenv: volume is in use - [[[[dc4425b806a67a9002d68703cdd9854feba44e43d591278b4eb2869f43c0da6d]

References

dev.to is the place where software developers stay in the circuit and avoid career stagnation.
Registering (for free!) Is the first step.


Source link

Leave a Reply

Your email address will not be published.