Docker-2: Dockerfile And Volume

Dockerfile

  1. Create a Dockerfile in one folder with name "Dockerfile"
  2. Edit this file with below contents:
    FROM alpine:latest
    MAINTAINER Chuantao
    CMD echo "Hello Docker!"
  3. Run this command to build a new docker image locally:
    docker build -t hello-docker .
    -t: Name and optionally a tag in the ‘name:tag’ format. So above command will create a docker image with its name "hello-docker" and tag "latest".
  4. After creating the image, docker run hello-docker will print out the message "Hello docker!"

Volume

  1. Mount a contain's path to host. A new path will be created in the host.
    docker run -d --name nginx -v /usr/share/nginx/html nginx
    This will run a docker container and expose the container's inside path /usr/share/nginx/html to host so that host can access this path.

    chuantao@chuantao-IdeaPad:~/docker/d2$ docker run -d --name nginx -v /usr/share/nginx/html nginx
    8db75f013f8b84a182974690177088a19a2199aa3738788aa14daeb6a34c85aa
    chuantao@chuantao-IdeaPad:~/docker/d2$ docker inspect nginx
    ...
    "Mounts": [
    {
    "Type": "volume",
    "Name": "7558ecd064bb74123cb3aca0a054a68b6254158b87b5a8bcd80f6fd4c3c4c378",
    "Source": "/var/lib/docker/volumes/7558ecd064bb74123cb3aca0a054a68b6254158b87b5a8bcd80f6fd4c3c4c378/_data",
    "Destination": "/usr/share/nginx/html"
    ...

    The path in red is the host path, a mapping of the contain's path. Then the folder can be accessed from this path.

  2. Mount an exist local host path to a container's path.
    docker run -d -p 8080:80 -v $PWD:/usr/share/nginx/html nginx
    This will run image 'nginx' server. User can access the server through host's 8080 port. When user updates the content of the index.html file inside the host path $PWD, refresh the browser to access '192.168.1.98:8080' will immediately reflect the updates.
  3. Run from another volume container
      • Create a data container first.
        docker create -v $PWD/data:/var/mydata --name data_container ubuntu
        This will generate a docker data container(not docker image) from base image 'ubuntu'. The data folder of current path in the host will be mapped to the '/var/mydata' path in the container. After running this command, the generated container can be listed by running 'docker ps -a'.
      • Run another container.
        docker run -it --volumes-from data_container ubuntu /bin/bash
        This will launch another container based from ubuntu and enter the interactive mode.
      • Type 'mount' in command and we can see one line like
        "/dev/sda5 on /var/mydata type ext4 (rw,relatime,errors=remount-ro,data=ordered)"
      • cd /var/mydata
      • As currently it is empty, we create one file here by typing
        "touch whatever.txt", then exit
      • Now we can see the same file in host's current data folder.
      • By this means, multiple docker containers can share the same host folder.

 

 

Leave a Reply

Your email address will not be published.