Skip to content

Using data containers #1

@psychemedia

Description

@psychemedia

It's possible to create a database instance with a named data container using dmysql-server --with-volume but this always seems to try to create a new data container, and I don't see a way to connect to a pre-existing container of the same name, which might help portability?

That is, we are forced into running:

docker run -v /var/lib/mysql --name [CONTAINER_VOLUME_NAME] ...
docker run --volumes-from [CONTAINER_VOLUME_NAME] ...

as a bound together pair of instructions.

Would it also make sense to add another flag --volumes-from and use this as a basis for mounting data from a pre-existing container? Perhaps something like:

def getDataVolumeName(containerName):
    # Create the container name variable
    return containerName.upper() + '_DATA'


def runContainer(containerName, rootPassword, hasVolume=False):
    ''' Creates the container
    '''

    if hasVolume:
        containerVolumeName = getDataVolumeName(containerName)

        call('docker run --volumes-from ' + containerVolumeName + ' --name ' + containerName + ' -e MYSQL_ROOT_PASSWORD="' + rootPassword + '" -d mysql', shell=True)
    else:
        call('docker run --name ' + containerName + ' -e MYSQL_ROOT_PASSWORD="' + rootPassword + '" -d mysql', shell=True)
    return

def main():

    parser = argparse.ArgumentParser(
        description="Creates a new MySQL container using the mysql image", 
        prog="dmysql-server")

    parser.add_argument("containerName", help="The name of the mysql container to create")
    parser.add_argument("rootPassword", help="The root password to define when creating the container")
    # --with-volume and --volumes-from should be mutually exclusive?
    parser.add_argument("--with-volume", help="Creates a data-only container", action='store_true')
    parser.add_argument("--volumes-from", help="Use a previously created data-only container", action='store_true')

    args = parser.parse_args()

    # If we need to create a volume
    if args.with_volume:
        # Create the container name variable
        # ?perhaps have an option to get this from a command line argument?
        containerVolumeName = getDataVolumeName(args.containerName)
        # Create data container
        createVolume(containerVolumeName)

    # If there's a volume
    if (args.with_volume or args.volumes_from):
        runContainer(containerName=args.containerName, rootPassword=args.rootPassword, hasVolume=True)
    else:
        runContainer(containerName=args.containerName, rootPassword=args.rootPassword)

    return

It would be even more flexible if we could also specify the name of the data volume container?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions