Docker Volumes - Hooking up your source code to a docker container

    1. Volumes exist OUTSIDE of the docker container (on host machine)
    2. Volumes can be “connected” to more than 1 container
    3. See if any volumes are “mounted” (aka connected) to a specific container
      1. $ docker inspect [container ID] → ie, $ docker inspect 901
        1. Look at JSON output for “Mounts”
        2. Source = file location of source code (outside of container)
          1. Source is “source” for volume (on host of docker machine, OSX).
          2. Destination is w/in container
        3. Destination = file location w/in this container

"Mounts": [

{

"Source":

"/Users/dougwells/Dropbox/1-Code/Docker/DockerBasics1/ExpressSite",

"Destination": "/var/www",

"Mode": "",

"RW": true,

"Propagation": "rprivate"

}]

How connect container to local code (how set “Source” and “Destination” in Mounts?

      1. You create a volume when you first create the container from its image
        1. In this example, we create a container ($ docker run) from image ‘node’
      2. MUST RUN THIS COMMAND FROM W/IN DESIRED LOCAL DIRECTORY(kinda true but only because use $(pwd) … print working directory)

$ docker run -p 8080:3000 -v $(pwd):/var/www -w "/var/www" node npm start

        1. node: simply the name of image you want to run ($ docker run node)
        2. -v Source (external to container) : destination (internal to container)
        3. -v → tells docker you want to connect this container to external volume
        4. -w → sets working directory. Where all commands, ie “npm start”, are run from
        5. -d → not show. Runs process in “detached mode” so console stays free
        6. $(pwd):/var/www
          1. $(pwd)→ print working dir (dir where you are typing command)
          2. : → separates source from destination
          3. /var/www → destination WITHIN container
        7. npm start → command you want to run when container starts (commands run from working directory called out in -w “xxxx/xxx”

Can also create a volume without a source.

      1. Simply name the volume (ie var/www) and docker sets it (volume) up with a docker specified source.

$ docker run -p 8080:3000 -v /var/www node npm start.

docker run -p 8080:3000 -v /var/www -w "/var/www" node npm start

(Note: no “source”)

      1. Docker creates that folder (/var/www) and mounts/connects it to host machine
      2. Careful, when do $ docker rm -v [cont id], this folder (var/www) will be DELETED
      3. But, this folder will NOT be deleted if just delete container ($ docker rm [cont id])

High level concepts

      1. External source file/”volume” is connected to machine’s “Destination”
        1. Note: volume is OUTSIDE of container. It is connected to the machine aka linux virtual box
        2. If delete container, volume persists (b/c other containers might use it)
      2. Container looks at machine’s “Destination” location for its code
      3. If Delete container, volume persists

Start & Stop container

      1. Cool thing is once container is connected to source code, next time it is started, it (container) remains hot linked to source code
        1. $ docker start [cont id] $ docker stop [container id]

Remove volume

      1. $ docker rm [container id] → removes container, not volume (b/c mult cont)
      2. $ docker rm -v [container id] → removes container AND volume
        1. Does not delete local folder (source code)
          1. CAUTION: it would delete Source if it set by default by Docker
        2. Only worry about removing volume if source was created by docker (clutters hard drive if not needed). If you specified source on your local machine, you can clean up or delete this file as you normally do. Reminder, only delete volume when ALL containers are done using it.

Managing volumes

      1. $ docker volume → list all volume commands
      2. $ docker volume ls → lists all volumes

results matching ""

    No results matching ""