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.

  • How can I use docker-compose locally after setting up a docker-machine host?
  • SSH from Docker image using Drone
  • Sharing a single file from host machine with Docker Container and having the Container r+w to same file
  • vnc to a docker container in vagrant
  • is it advisable to have multiple applications in a single docker image
  • Docker /dev/mapper permission
  • 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?

  • Using the host ip in docker-compose
  • How to install an application in Hadoop Cluster using YARN?
  • docker-machine stop command kills all my containers
  • docker-compose run with isolated sets of services?
  • Running multiple instances of an image with docker-compose fails
  • Debug Django project with environment in docker container
  • 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.