docker

基础使用

docker build时会把Dockerfile目录下的所有东西发往docker daemon, 可以增加一个’.dockerignore’文件来排除某些目录或文件。

docker run -d -p 80:80 --name webserver nginx
docker stop webserver
docker start webserver
docker rm -f webserver

docker run ubuntu /bin/echo 'Hello world'
docker run -it ubuntu /bin/bash

docker run -it --rm --entrypoint bash --rm openresty/openresty:xenial

images

docker images
docker rmi nginx # rm image

logs

docker logs -f b572ef09601c

inspect

docker inspect b572ef09601c

commit

docker commit -m 'add ruby' a2086b366560 notsobad/sinatra:v2

docker build -t notsobad/sinatra2 .

create tag

docker tag 0ec894288b89 notsobad/sinatra2:test

data volumn

使用data valumn

docker run -P -d --name web -v /webapp training/webapp  python app.py
docker inspect web

mount dir

docker run -P -d --name web2 -v /opt/webapp:/opt/webapp training/webapp python app.py

mount local sources.list to docker container.

docker run -rm -it -v /Users/xxx/sources.list:/etc/apt/sources.list ubuntu /bin/bash

share volumes

docker create -v /dbdata --name dbstore training/postgres /bin/true
docker run -d --volumes-from dbstore --name db1 training/postgres

Execute a command in a running container

docker-compose exec nginx bash
>nginx -s reload

cleanup

delete unuesd container.

docker ps -q -f status=exited | xargs docker rm -f

remove all untagged images by combining docker rmi with the recent dangling=true query

docker images -q --filter "dangling=true" | xargs docker rmi  

清理磁盘

docker system prune 
docker volume prune
docker container prune

create service

docker service create --replicas 1 --name helloworld alpine ping www.baidu.com

docker service inspect helloworld
docker service ps helloworld
docker service logs helloworld
docker service rm helloworld

docker registry

创建registry

docker run -d -p 5000:5000 --restart=always --name registry -v /home/docker-registry:/var/lib/registry registry:2
docker tag ynm3k localhost:5000/ynm3k
docker push localhost:5000/ynm3k

list all images

从docker registry中列出所有镜像

curl -k https://gen8:5000/v2/_catalog -u admin:admin

curl -vv -u user:pass https://xxx:5000/v2/_catalog -k
curl -vv -u user:pass https://xxx:5000/v2/ynm3k/manifests/latest -k

restart policy

如果一个镜像是用docker up -d --restart=always方式启动的, 那么docker stop会停止镜像,但是下次系统重启依然会启动,

docker stop 98b2841e2ce5

# 查看重启策略
docker inspect -f "{{.HostConfig.RestartPolicy.Name}}" 98b2841e2ce5

# 调整重启策略
docker update --restart=no 72fd248a2a4f

docker-compose

create image from dockerfile

docker build -t web .
docker-compose up
docker-compose stop

run daemon service bash

docker-compose run daemon bash
docker-compose ps
docker exec -it gitlab bash