Docker Commands 2

Docker storage

Linux Filesystem Hierarchy

Mount point

Docker mount type

Data storage in Docker:

  • bind mount - shared between Docker host and container
  • volume - persistent data storage (both named and anonymous volumes)
  • tmpfs mount - non-persistent data storage

Docker: bind mount

Map file/directory on Docker host into container.

Dokcer: bind mount (2)

Create 2 containers using the same nignx image.

  • one without bind mount
  • one with bind mount
docker container run -dp 8001:80 --name n1 --rm nginx:alpine

docker container run -dp 8002:80 --name n2 --rm --volume ${PWD}:/usr/share/nginx/html/ nginx:alpine

docker container run -dp 8022:80 --name n22 --rm --mount type=bind,source=${PWD},target="/usr/share/nginx/html/" nginx:alpine
  • Explaination

    • --volume - bind mount a volume
    • ${PWD} - a shell environment variable whose value is current working directory (may have to use /${PWD} for Windows)

Dokcer: bind mount (3)

Compare files between the two containers.

docker container exec n1 ls /usr/share/nginx/html
docker container exec n2 ls /usr/share/nginx/html
  • Explaination

    • exec - subcommand to run a command in a running container
    • n1 - container name given to the first running container created
    • n2 - container name given to the second running container created

Docker volume

There are two types of docker volumes

  • anonymous volume - volum with name (64 hex digits) created automatically by Docker
  • named volume - volume with custom name

Manage volumes

  • create
  • ls
  • rm

Docker volume: create

Create anonymous volume:

docker volume create

Create named volume:

docker volume create myvol

Docker volume: ls

Check local volume on this Docker host.

docker volume ls
DRIVER    VOLUME NAME
local     878ad481322c843a6c2c23fbfb4dd9122b2922ffd04f0358fb14c7a7eccbe9ac
local     myvol

Docker volume: rm

Remove local docker volumes.

docker volume rm 878ad481322c843a6c2c23fbfb4dd9122b2922ffd04f0358fb14c7a7eccbe9ac
docker volume rm myvol

Check current local volumes.

docker volume ls

DRIVER    VOLUME NAME

Docker volume: anononymous

Create a container using anonymous volume

docker container run -dp 8003:80 --name n3 --rm --volume /usr/share/nginx/html/ nginx:alpine

docker container run -dp 8033:80 --name n33 --rm --mount type=volume,target=/usr/share/nginx/html/ nginx:alpine
  • Explaination

    • create a container named n3
    • mounting anonymous volume under directory /usr/share/nginx/html/nginx in the container

Docker volume: named

Create a container using named volume

docker container run -dp 8004:80 --name n4 --rm --volume myvol:/usr/share/nginx/html/ nginx:alpine

docker container run -dp 8044:80 --name n44 --rm --mount type=volume,source=myvol,target=/usr/share/nginx/html/ nginx:alpine
  • Explaination

    • create a container named n4
    • mounting a volume named myvol under directory /usr/share/nginx/html/nginx in the container

Docker network

Docker network: default

  • by default, Docker create 3 networks
    • bridge - isolated network from host
      • use bridge driver
      • separated network from host
      • has its own IP address
    • host - network is not isolated from host
      • use host driver
      • does not have its own IP-address
    • none - no networking
      • use null driver
  • by default, a container will be created and connected to network bridge

Manage networks

  • create
  • ls
  • rm

Docker network: create

Create a new network.

docker network create --driver bridge mynet
  • Explaination

    • network - management command related to network
    • create - subcommand to create a new network
    • --driver bridge - option to create network using bridge driver (the default)
    • mynet - name of the new network

Docker network: ls

Check current networks on host.

docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
791cfd735465   bridge            bridge    local
3979939accfa   docker_gwbridge   bridge    local
a8c4ebbecec3   host              host      local
yrpbzfunuhip   ingress           overlay   swarm
ed6a813642db   mynet             bridge    local
8a9fe937f5ac   none              null      local

Docker network: rm

Remove the created network.

docker network rm mynet

Check current networks on host.

docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
791cfd735465   bridge            bridge    local
3979939accfa   docker_gwbridge   bridge    local
a8c4ebbecec3   host              host      local
yrpbzfunuhip   ingress           overlay   swarm
8a9fe937f5ac   none              null      local

Docker network: test default bridge

Create 2 containers using default bridge network.

docker container run -dp 9001:80 --name a1 --rm nginx:alpine
docker container run -dp 9002:80 --name a2 --rm nginx:alpine

Docker network: test default bridge (2)

Find IP address of container a1 and a2 using inspect subcommand.

docker container inspect a1
docker container inspect a2
  • Explaination

    • inspect - display detailed information on one or more containers

Docker network: test default bridge (3)

Find IP address of container a1 using command in container.

docker container exec -it a1 ip -4 address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
13: eth0@if14: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
  • Explaination

    • ip - linux command related to ip network
    • -4 - option to show only IPv4 addresses
    • address - command argument to show ip addresses

Docker network: test default bridge (4)

Find IP address of container a2 using command in container.

docker container exec -it a2 ip -4 address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
15: eth0@if16: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

Docker network: test default bridge (5)

Test connection from container a1 to a2.

docker container exec -it a1 ping -c 3 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.199 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.294 ms
64 bytes from 172.17.0.3: seq=2 ttl=64 time=0.244 ms

--- 172.17.0.3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.199/0.245/0.294 ms

Docker network: test default bridge (6)

Test connection from container a2 to a1.

docker container exec -it a2 ping -c 3 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.181 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.315 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.244 ms

--- 172.17.0.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.181/0.246/0.315 ms

Docker network: test mynet

Create a network named mynet.

docker network create mynet

Check network address of mynet.

docker network inspect mynet

Docker network: test mynet (2)

Create 2 containers using network mynet.

docker container run -dp 7001:80 --name b1 --rm --network mynet nginx:alpine
docker container run -dp 7002:80 --name b2 --rm --network mynet nginx:alpine

Docker network: test mynet (3)

Find IP address of container b1 and b2 using inspect subcommand.

docker container inspect b1
docker container inspect b2

Note: Container b1 and b2 should be

  • on the same network
  • on different network from default bridge network

Docker network: test mynet (4)

Find IP address for container b1 using command in container.

docker container exec -it b1 ip -4 address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    inet 172.19.0.2/16 brd 172.19.255.255 scope global eth0
       valid_lft forever preferred_lft forever

Docker network: test mynet (5)

Find IP address for container b2 using command in container.

docker container exec -it b2 ip -4 address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    inet 172.19.0.3/16 brd 172.19.255.255 scope global eth0
       valid_lft forever preferred_lft forever

Docker network: test mynet (6)

Test connection from container b1 to b2.

docker container exec -it b1 sh -c "ping -c 3 172.19.0.3"
PING 172.19.0.3 (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.077 ms
64 bytes from 172.19.0.3: seq=1 ttl=64 time=0.075 ms
64 bytes from 172.19.0.3: seq=2 ttl=64 time=0.085 ms

--- 172.19.0.3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.075/0.079/0.085 ms

Docker network: test mynet (7)

Test connection from container b2 to b1.

docker container exec -it b2 sh -c "ping -c 3 172.19.0.2"
PING 172.19.0.2 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.092 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.309 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.353 ms

--- 172.19.0.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.092/0.251/0.353 ms

References