CIS 527



Lab 5.A - Resources

Container Resources

  • Mapped Network Ports
  • Networks
  • Environment Variables
  • Storage Volumes

Mapped Ports

Image Source: Docker

Mapped Ports

docker run -d -p 8080:80 --name nginx1 nginx:alpine
services:
  nginx:
    image: nginx:alpine
    container_name: nginx1
    ports:
      - "8080:80"

curl localhost:8080

Inspecting Ports

docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                   NAMES
a1ef8911ce52   nginx:alpine   "/docker-entrypoint.…"   8 minutes ago   Up 8 minutes   0.0.0.0:8080->80/tcp    nginx1

Docker Networks

Image Source: Docker

Docker Networks

docker run -d -p 8080:80 --name nginx1 nginx:alpine
docker network create -d bridge --internal redis_network
docker network connect redis_network nginx1
docker run -d --name redis1 --network redis_network redis:latest

Docker Networks

services:
  nginx:
    image: nginx:alpine
    container_name: nginx1
    ports:
      - "8080:80"
    networks:
      - redis_network
      - default
  redis:
    image: redis:latest
    container_name: redis1
    networks:
      - redis_network
networks:
  redis_network:
    internal: true
    

Inspecting Networks

docker network inspect [network]

[ {
    "Name": "docker_default",
    ...
    "Containers": {
        "a1ef8911ce524b8345c0e047798072c4cba2c6bb1a2dafd4a37e9b577b2c34d0": {
            "Name": "nginx1",
            "EndpointID": "716dec048ad8b8ccdab793cceb93881c775af5335671555b3c437b395fde35dd",
            "MacAddress": "02:42:ac:15:00:02",
            "IPv4Address": "172.21.0.2/16",
            "IPv6Address": ""
} } } ]
[ {
    "Name": "docker_redis_network",
    // ...
    "Containers": {
        "a1ef8911ce524b8345c0e047798072c4cba2c6bb1a2dafd4a37e9b577b2c34d0": {
            "Name": "nginx1",
            "EndpointID": "af8f3f616491df592fbc21d665e51e7a7f6989abc557e76ea56bf7c71158db00",
            "MacAddress": "02:42:ac:14:00:03",
            "IPv4Address": "172.20.0.3/16",
            "IPv6Address": ""
        },
        "bfa602ef48b0fe744d59d2c450f271ad108cae23c7cb5f2b4f021961db25a978": {
            "Name": "redis1",
            "EndpointID": "a0a761e9c91282bbb7715428385182f2dddcf0022c1f66731a91135a243bde19",
            "MacAddress": "02:42:ac:14:00:02",
            "IPv4Address": "172.20.0.2/16",
            "IPv6Address": ""
} } } ]

Environment Variables

Image Source: Vladislav Supalov

Environment Variables

docker network create -d bridge --internal mysql_network
docker network create -d bridge mysql_default
docker run -d --name mysql1 --network mysql_network 
        -e MYSQL_ROOT_PASSWORD=password mysql:latest
docker run -d --name mysqladmin1 --network mysql_default 
        -e PMA_HOST=mysql1 -p 8080:80 phpmyadmin/phpmyadmin:latest
docker network connect mysql_network mysqladmin1

Environment Variables

services:
  mysql:
    image: mysql:latest
    container_name: mysql1
    networks:
      - mysql_network
    environment:
      MYSQL_ROOT_PASSWORD: password
  mysqladmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: mysqladmin1
    ports:
      - "8080:80"
    networks:
      - default
      - mysql_network
    environment:
      PMA_HOST: mysql1
networks:
  mysql_network:
    internal: true

Inspecting Environments

docker inspect [container]

[ {
    // ... 
    "Name": "/mysql1",
    // ...
    "Config": {
        // ...
        "Env": [
            "MYSQL_ROOT_PASSWORD=password",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "GOSU_VERSION=1.14",
            "MYSQL_MAJOR=8.0",
            "MYSQL_VERSION=8.0.29-1debian10"
        ],
        // ...
        "Image": "mysql:latest",
        // ...
    } 
} ]

Storage Volumes

  • Add something to database
  • Stop using docker compose down
  • Restart using docker compose up -d
  • Check database again - is it there?

Volumes & Mounts

Image Source: Docker

Bind Mount

mkdir /home/cis527/mysql
docker run -d --name mysql1 --network mysql_network 
        -e MYSQL_ROOT_PASSWORD=password 
        -v /home/cis527/mysql:/var/lib/mysql
        mysql:latest
services:
  mysql:
    image: mysql:latest
    container_name: mysql1
    networks:
      - mysql_network
    environment:
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - /home/cis527/mysql:/var/lib/mysql
  # ...

Docker Volume

docker volume create mysql_data
docker run -d --name mysql1 --network mysql_network 
        -e MYSQL_ROOT_PASSWORD=password 
        -v mysql_data:/var/lib/mysql
        mysql:latest
services:
  mysql:
    image: mysql:latest
    container_name: mysql1
    networks:
      - mysql_network
    environment:
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - mysql_data:/var/lib/mysql
  # ...
volumes:
  mysql_data:    

Inspecting Volumes

docker inspect [container]

[ {
    // ...
    "Name": "/mysql1",
    // ...
    "Mounts": [
        {
            "Type": "volume",
            "Name": "docker_mysql_data",
            "Source": "/var/lib/docker/volumes/docker_mysql_data/_data",
            "Destination": "/var/lib/mysql",
            "Driver": "local",
            "Mode": "z",
            "RW": true,
            "Propagation": ""
        }
    ]
} ]