Part 2: Docker – Mounting Local Storage with Container : Working with Volumes & Pushing image to Docker Hub

Continuing from Part 1 Docker.

In part 1, we have deployed a php container to host some files.

However to change those files, we need to re-deploy the container.

To avoid this, we can mount local directory on the container.

To achieve this, we will mount local volume on the container.

### "/Users/saket/docker/src" directory is mounted to "/var/www/html/"

$ docker run -p 80:80 -v /Users/saket/docker/src:/var/www/html/ php_container1
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Mon Jan 07 19:28:53.642081 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.13 configured -- resuming normal operations
[Mon Jan 07 19:28:53.642172 2019] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

### Current file output
$ cat src/index.php 
<?php
echo "Hello new world";
?>

### Lets see the current output.



### Now change the file content and see it changes the web page content immediately.
$ cat src/index.php 
<?php 
echo "Hello new world, Welcome to ngelinux.com docker tutorial"; 
?>




Pushing the image to Docker Hub

### First list the images
$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php_container1      latest              581fbbf7642a        24 hours ago        377MB
                            3da63c0ee91a        4 days ago          377MB
                            4ceb16ee1c86        4 days ago          367MB
                            456479a538c0        4 days ago          367MB
                            c4fa2adcf0ab        4 days ago          367MB
hello-world         latest              fce289e99eb9        7 days ago          1.84kB
php                 7.2-apache          342a9fa6554c        10 days ago         377MB
php                 7.2-cli             a1c0790840ba        10 days ago         367MB

### Optional step: Now tag the name to the image: "docker tag image username/repository:tag"
$ docker tag php_container1 saketdockerapple/php_container1:part2

### List again
$ docker image ls
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
php_container1                    latest              581fbbf7642a        24 hours ago        377MB
saketdockerapple/php_container1   part2               581fbbf7642a        24 hours ago        377MB
                                          3da63c0ee91a        4 days ago          377MB
                                          4ceb16ee1c86        4 days ago          367MB
                                          456479a538c0        4 days ago          367MB
                                          c4fa2adcf0ab        4 days ago          367MB
hello-world                       latest              fce289e99eb9        7 days ago          1.84kB
php                               7.2-apache          342a9fa6554c        10 days ago         377MB
php                               7.2-cli             a1c0790840ba        10 days ago         367MB

### Push the image to docker hub with syntax: "docker push username/repository:tag"
$ docker push saketdockerapple/php_container1
The push refers to repository [docker.io/saketdockerapple/php_container1]
53f89e354439: Pushed 
eccae8073817: Mounted from library/php 
708942a92eb6: Mounted from library/php 
d3fe85001836: Mounted from library/php 
9ab9eef0e136: Mounted from library/php 
cd27e9cdba62: Mounted from library/php 
f4d3eff47c62: Mounted from library/php 
58cd3b2b073c: Mounted from library/php 
c8500dc8d342: Mounted from library/php 
6c26c5566c37: Mounted from library/php 
bcd6f0091bba: Mounted from library/php 
c25c4aba0c63: Mounted from library/php 
2fbf086236e5: Mounted from library/php 
5fbcd9a02436: Mounted from library/php 
7b4e562e58dc: Mounted from library/php 
part2: digest: sha256:5fac458906d8716c6c419c1fe0491f65fd3c3571609582f6c443b9c6877de5e1 size: 3450
$ 

 

Test the remote image

$ docker run -p 5000:80 saketdockerapple/php_container1:part2
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Tue Jan 08 18:02:26.302064 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.13 configured -- resuming normal operations
[Tue Jan 08 18:02:26.317529 2019] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
172.17.0.1 - - [08/Jan/2019:18:02:47 +0000] "GET / HTTP/1.1" 200 245 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15"
172.17.0.1 - - [08/Jan/2019:18:02:48 +0000] "GET /favicon.ico HTTP/1.1" 404 503 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15"

Output



Now we can run this container image on any system remotely and there is no need to build the container image again and again.

Also as this is container in itself, so there is no issue with any dependencies with it.

We can also view the change on Docker Hub.


Now we will check the usage of docker swarm & docker compose from part 3 onwards.

Leave a Reply

Your email address will not be published.