Want to use volumes in my running Docker container

I have a docker container that is currently running on my ubuntu server.

The container is basically running nginx and mysql on it, but currently it isn’t using volumes so if I destroy the container my data is gone.

  • Docker.io for Windows [closed]
  • Docker Timezone in Ubuntu 16.04 Image
  • Remove docker image depending on an already removed image
  • 404 while serving static files via docker nginx
  • Exception on reloading remote spring boot application with Intellij IDEA
  • How to make redis BROKER_URL dynamic on deployment to AWS instance
  • The DockerFile was updated with the following:

    VOLUME ["/var/lib/mysql", "/opt/nginx/www"]
    

    I want to create the following folders on my host machine like:

    /home/ubuntu/container_data/container1/mysql
    /home/ubuntu/container_data/container1/www
    

    How can I start a container and use the 2 folders I created above for mysql and my nginx/www files.

    I’m confused what the VOLUME command does inside of a DockerFile, and what the -v command does when starting a container. How are they related? Really hoping someone can explain this for me.

    Also, is it possible for me to do this with an already running container or will I have to stop and destroy my current container b/c it was setup with volumes?

  • how to access OSX non-storage devices from docker container
  • Docker quickstart terminal not opening
  • What does Go Version means?
  • Docker network with AWS VPC
  • Docker image dns probem, name solve failed
  • Can we pass ENV variables through cmd line while building a docker image through dockerfile?
  • 2 Solutions collect form web for “Want to use volumes in my running Docker container”

    How can I start a container and use the 2 folders I created above for mysql and my nginx/www files.

    docker run -v /home/ubuntu/container_data/container1/mysql:/var/lib/mysql -v /home/ubuntu/container_data/container1/www:/opt/nginx/www image_name
    

    The above command will mount your host directories as data volumes in the container when it runs. The pattern for the -v flag is:

    -v /path/on/host:/path/in/container
    

    I’m confused what the VOLUME command does inside of a DockerFile, and what the -v command does when starting a container. How are they related? Really hoping someone can explain this for me.

    I believe declaring a volume in the Dockerfile is functionally equivalent to using the -v CLI flag like this: (corrections welcome)

    docker run -v /data image_name
    
    VOLUME /data
    

    Mount-points for the volumes are created when the container is created. More info from Docker docs

    You can retrieve files from the existing container before you destroy it using the export command.

    I’m confused what the VOLUME command does inside of a DockerFile, and
    what the -v command does when starting a container. How are they
    related? Really hoping someone can explain this for me.

    The VOLUME command inside a Dockerfile simply marks a directory as “not part of the container filesystem”, such that it’s contents will not be included in a generated image nor will they be committed when you run docker commit. It also makes the directory available to other containers that are started with the --volumes-from argument.

    The -v command line option, when given a simple pathname, performs the same as the VOLUME command. When given a hostpath:containerpath argument, mounts the specified hostpath inside the container at containerpath. In either case, the volume is available via --volumes-from to other containers.

    It is not necessary to have a VOLUME directive in your Dockerfile in order to use the -v command line option, but you often will in any case to clearly identify the data directories.

    For your two directories:

    /home/ubuntu/container_data/container1/mysql
    /home/ubuntu/container_data/container1/www
    

    You would:

    docker run \
      -v /home/ubuntu/container_data/container1/mysql:/var/lib/mysql \
      -v /home/ubuntu/container_data/container1/www:/opt/nginx/www \
      ...
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.