[daily] docker

 

what docker is? https://www.docker.com/what-docker 

 

一: archlinux 安装docker:

我之前装了一个, 然后好久没用, 今天发现不能用了, 启动不了. 于是重装:

1. pacman -Rsun docker

2. rm -rf /var/lib/docker

 

/var/lib/docker 目录下面是所以的image和containner.  是他们的原因导致了不能启动.  深层原因不知道, 反正删掉了.

 

3.  pacman -S docker

 

二:  配置

1.  建立一个用户组docker, 把tong用户加入docker组.

2.  systemctl start docker.service

3.  systemctl enable docker.service

 

三:  安装image

1.  docker pull debian

2.  docker pull centos:7

 所有可以用这个命令的image都在这个地方 https://hub.docker.com/explore/

 

四: 运行

/home/tong [tong@T7] [17:19]
> docker run hello-world

 

/home/tong [tong@T7] [17:20]
> docker run -it centos

 

/home/tong [tong@T7] [17:20]
> docker ps -a         
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
1a467c1d4756        centos              "/bin/bash"         29 seconds ago       Exited (127) 11 seconds ago                         reverent_wescoff
b8fd51d75418        hello-world         "/hello"            About a minute ago   Exited (0) About a minute ago                       boring_sammet

 

--------------------------------------------  我是不太会分割线  ------------------------------------------

五:  剩下的就不太会了, 看文档:

https://docs.docker.com/get-started/

 

六: 亲手做一个app

https://docs.docker.com/get-started/part2/#apppy

/home/tong/docker/test [tong@T7] [18:45]
> ls
app.py  Dockerfile  requirements.txt

 

/home/tong/docker/test [tong@T7] [18:45]
> docker build -t friendlyhello .

 

/home/tong/docker/test [tong@T7] [18:49]
> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              57de680506f7        34 seconds ago      194MB

 

/home/tong/docker/test [tong@T7] [18:49]
> docker run -p 4000:80 friendlyhello
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

 

/home/tong/docker/test [tong@T7] [18:52]
> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
bf3a6159a05f        friendlyhello       "python app.py"     2 minutes ago       Up 2 minutes        0.0.0.0:4000->80/tcp   elegant_shaw

/home/tong/docker/test [tong@T7] [18:52]
> docker stop bf        
bf

 

/home/tong/docker/test [tong@T7] [10:03]
> cat app.py 
from flask import Flask
from redis import Redis, RedisError

import os
import socket

redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
        try:
                visits = redis.incr("counter")
        except RedisError:
                visits = "<i>cannot connect to Redis, counter disabled</i>"

        html = "<h3>Hello {name}!</h3>" \
                "<b>Hostname:</b> {hostname}<br/>" \
                "<b>Visits:</b> {visits}"
        return html.format(name=os.getenv("NAME", "world"),
                hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
        app.run(host='0.0.0.0', port=80)
app.py
/home/tong/docker/test [tong@T7] [10:04]
> cat Dockerfile 
FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
EXPOSE 80
ENV NAME World
CMD ["python", "app.py"]
Dockerfile
/home/tong/docker/test [tong@T7] [10:05]
> cat requirements.txt 
Flask
Redis
requirements.txt

 

 

七, 把六定制成一个服务

1.  安装 docker-compose

2.  写一个yaml

/home/tong/docker/test [tong@T7] [10:05]
> cat test.yml 
version: "3"
services:
  web:
    image: friendlyhello:latest
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"  
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:
test.yaml

 

3.  docker swarm init

4.  deploy

/home/tong/docker/test [tong@T7] [10:02]
> docker stack deploy -c test.yml testapp
Creating network testapp_webnet
Creating service testapp_web

5.  ps

/home/tong/docker/test [tong@T7] [10:11]
> docker stack ps testapp  

6.  take down

/home/tong/docker/test [tong@T7] [10:11]
> docker stack rm testapp
Removing service testapp_web
Removing network testapp_webnet

7.  swarm leave

/home/tong/docker/test [tong@T7] [10:12]
> docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
b22in8aax0kpy3x07n0comw1x *   T7                  Ready               Active              Leader

/home/tong/docker/test [tong@T7] [10:14]
> docker swarm leave --force
Node left the swarm.

 

八, docker与host共享存储

https://docs.docker.com/engine/admin/volumes/

 

九, 作为一个chroot, 我是这样用的

1. 创建

/home/tong/docker/test [tong@T7] [16:12]
> docker run -d -it --mount type=bind,source=/home/tong/Src,target=/root/Src/ centos
778a7be01a7dc40ce58b1d701dd318785404a0b97086333ea1f0ebb3aa571939

另一种也好用的写法:

/home/tong/docker/test [tong@T7] [18:06]
> docker run -d -it -v /home/tong/Src/:/root/src --name centos7-dev centos 

 

2.  登进去

/home/tong/docker/test [tong@T7] [16:15]
> docker exec -it 77 /bin/bash 
[root@778a7be01a7d /]# cd /root/

 

十:  定制一个image

https://docs.docker.com/engine/reference/builder/#format

/home/tong/docker/centos7-dev [tong@T7] [18:33]
> cat Dockerfile                      
FROM centos:latest
RUN yum install -y gcc man vim gcc-c++
ENTRYPOINT ["/usr/bin/bash"]

 

/home/tong/docker/centos7-dev [tong@T7] [18:33]
> docker build -t centos7-custom-dev .

 

/home/tong/docker/centos7-dev [tong@T7] [18:33]
> docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos7-custom-dev   latest              e7297082992b        9 seconds ago       427MB

 

十一, 在docker里使用gdb

1.  添加如下行到 docker-compose.yml

cap_add:
    - SYS_PTRACE

2. 添加如下参数到run命令

--cap-add=SYS_PTRACE

https://stackoverflow.com/questions/42029834/gdb-in-docker-container-returns-ptrace-operation-not-permitted

 

posted on 2017-08-21 17:00  toong  阅读(361)  评论(0编辑  收藏  举报