docker-mysql-scripts icon indicating copy to clipboard operation
docker-mysql-scripts copied to clipboard

Using data containers

Open psychemedia opened this issue 11 years ago • 2 comments

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?

psychemedia avatar Jan 14 '15 09:01 psychemedia

Hi @psychemedia I agree. Can you make a patch?

luiselizondo avatar Jan 14 '15 17:01 luiselizondo

I was going to say I will try to do it tomorrow if that's okay, but I just raised a related issue on naming data containers and the two issues together perhaps have a bearing on ways of defining and using flag and/or parameter names?

psychemedia avatar Jan 14 '15 19:01 psychemedia