Docker

Container Native (CN) - Docker

RELATED: SETUP > Dev Environment > Container - Docker | EXAMPLES > Cloud Solutions#Docker |

Awesome References

Explore further

Useful Commands

Container

# start container
$ docker run [image_name][:image_version]
$ docker stop <container_name>
# restart container
$ docker start <container_name> 
Start container and enter the running container
$ docker run -it <imageName> /bin/bash
root@47383646>:/# pwd
Enter already running container
$ docker exec -it <container_id_or_name> /bin/bash
Inspecting running container
$ docker inspect <containerName> | less
[
  {
     "Id": [...]
  }
]

$  docker inspect --format='{{.NetworkSettings.IPAddress}}' <containerName> 
docker run environment vars
sudo docker run -d -t -i -e REDIS_NAMESPACE='staging' \ 
-e POSTGRES_ENV_POSTGRES_PASSWORD='foo' \
-e POSTGRES_ENV_POSTGRES_USER='bar' \
-e POSTGRES_ENV_DB_NAME='mysite_staging' \
-e POSTGRES_PORT_5432_TCP_ADDR='docker-db-1.hidden.us-east-1.rds.amazonaws.com' \
-e SITE_URL='staging.mysite.com' \
-p 80:80 \
--link redis:redis \  
--name container_name dockerhub_id/image_name
Remove all stop containers
$ docker rm $(docker ps -a -q)

Image

Transfer image between host without using docker image repository

Ideally, the docker image is shared using the docker repository. However, if docker repository is not readily available, use the following docker save and docker load to transfer the image between host. To rebuild using the provided scripts is also an option, but some build could take a long time.

# export to the intended folder; first change directory to the folder
#docker save -o <path for generated tar file> <image name>
cd ~
docker save -o ./arm32v7-alpine_3-9-3.tar arm32v7-alpine:3.9.3

# transfer file to destination host, usinf SCP for example

# import
#docker load -i <path to image tar file>
cd ~
docker load -i ./arm32v7-alpine_3-9-3.tar
Enable --squash feature

Examples

Create image - basic; basic website hosted by nginx
Docker compose; website - API - ElasticSearch
Docker compose; elasticsearch and python flask app
version: "3"
services:
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
    container_name: es
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    volumes:
      - esdata1:/usr/share/elasticsearch/data
  web:
    image: gabepublic/sffoodtruck-site-flask:0.1.0-linux-amd64
    command: python3 app.py
    depends_on:
      - es
    ports:
      - 5000:5000
    volumes:
      - ./flaskapp:/opt/flaskapp
volumes:
    esdata1:
      driver: local
  • build -

  • environment - passing environment variables into the container

  • image - image name; can be replaced with build to build the image on the fly (build: .)

  • volumes - TBD

Docker Images - ready to use

Docker Networking

  • Docker Networking types:

    • bridge - the default network driver; used by in standalone container applications to communicate

    • host - used in docker swarm only; standalone container connects to the host network directly

    • overlay - used in docker swarm to connect multiple docker daemon

    • macvlan - used with legacy application, where a MAC address can be assigned to a container to have it appear as if it were a physical device on the network

    • none - completely disables network that is useful if a custom network driver is used

  • Docker container network topology is typically: 172.17.0.0/16. The docker bridge (docker0) connects to the docker network (172.17.0.1), the virtual Ethernet interfaces (i.e., veth0, veth1, etc.), as well as to the physical network (e.g., 192.168.1.10). All the containers (i.e., 172.17.0.2, 172.17.0.3, etc.) are connected to the docker network, and the virtual Ethernet interfaces (i.e., veth0, veth1, etc.) as well so that they can plug into the network switch. The docker bridge uses NAT to route the containers' traffic into the physical network.

  • Use nmap to inspect the network (docker run nmap)

  • Use ifconfig to show docker0, vethxxx

Last updated