hrk1l2x

2 minute read

Docker Basic

docker commands

# process list
docker ps -a

# connect the container
# -i interactive mode
# -t activate virtual device
# `$ docker attach {container}` can attach also but you exit container then container are stopped at the same time. because you attach PID=1 with standard input and output in container.
docker exec -it --user root {container_id} /bin/bash

# exec command on container
docker exec {container_id} {command}

# stop container
docker stop {container_id}

# run all containers
docker restart $(docker ps -q)

# list of images
docker image list

# launch container
docker run -it {image_id} /bin/bash

# check container resources (CPU, Memory, Disk I/O)
docker stats

# check health status
docker inspect --format="{{ .State.Health.Status }}" {container_id}

# check container ip address (internal)
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' {container_id}
docker inspect {container_id} | grep '"IPAddress"'
docker inspect `docker ps -ql`|grep "IPAddress"
ip a show docker0
docker ps | awk 'NR>1&&$0=$1' | xargs -n 1 docker inspect -f "{{.Name}} {{.NetworkSettings.IPAddress }}"
docker ps | awk 'NR>1&&$0=$1' | xargs -n 1 docker inspect -f "{{.Name}} {{.NetworkSettings.IPAddress }}" | grep zabbix-server | sed '/^$/d'

# check port
docker ps -q | xargs docker inspect -format='{{ if index .NetworkSettings.Ports "10051/tcp" }}{{(index (index .NetworkSettings.Ports "10051/tcp") 0).HostPort}}{{ end }}' | sed '/^$/d'
ormat=33076

docker inspect --format='{{(index (index .NetworkSettings.Ports "10051/tcp") 0).HostPort}}' {container_id}
33076

# delete container
docker rm {container_id}

# all delete images (stopped container, unattached volumes, images, network)
# before use `docker rm $(docker ps -aq)`
docker images -a
docker system prune -fa

# delete each layer
docker container prune -f
docker volume prune -f
docker network prune -f
docker image prune -f

# AWS ECR
# build, set tag, push to ECR
aws ecr get-login --profile staging --no-include-email --region ap-northeast-1
docker build --no-cache=true -t test .
docker tag {image_name}:{tag} xxx.dkr.ecr.ap-northeast-1.amazonaws.com/test:trusty
docker push xxx.dkr.ecr.ap-northeast-1.amazonaws.com/test:trusty

you can separates the environment variable depends on environtment (dev, stg, prod..).

you can define environment variables on docker-compose.yml or task-definition.json (AWS ECS).

terraform

  {
    "name": "",
    "hostname": "",
    "image": "",
    "network_mode": "bridge",
    "dnsSearchDomains": [],
    "entryPoint": [],
    "linuxParameters": {
      "capabilities": {
        "add": null,
        "drop": null
      },
      "sharedMemorySize": null,
      "tmpfs": null,
      "devices": null,
      "initProcessEnabled": null
    },
    "environment": [
      {
        "name": "ENV",
        "value": "staging"
      }

scripts which set on docker image.

if [[ $ENV = staging ]]; then
  ...
fi
comments powered by Disqus