Docker快速手册

Docker命令小结

查看命令提示

$ docker

查看docker版本

$ docker version

查看详情,如容器数量等

$ docker info

使用容器

创建容器在前台运行:

Create an run a container in foreground
-it : --interactive --tty
host_port:container_port

$ docker container run -it -p 80:80 nginx

创建容器在后台运行: -d

Create an run a container in background

$ docker container run -d -p 80:80 nginx

容器命名

$ docker container run -d -p 80:80 --name nginx-server nginx

TIP: WHAT RUN DID

  • Looked for image called nginx in image cache
  • If not found in cache, it looks to the default image repo on Dockerhub
  • Pulled it down (latest version), stored in the image cache
  • Started it in a new container
  • We specified to take port 80- on the host and forward to port 80 on the container
  • We could do "$ docker container run --publish 8000:80 --detach nginx" to use port 8000
  • We can specify versions like "nginx:1.09"

查看正在运行的容器

$ docker container ls

OR

$ docker ps

查看所有容器 (即使不是运行状态)

$ docker container ls -a

停止运行容器

$ docker container stop [ID]

停止所有运行中的容器

$ docker stop $(docker ps -aq)

删除一个容器 (不能删除正在运行的容器,必须先停止容器运行)

$ docker container rm [ID]

强制删除运行中的容器 (-f)

$ docker container rm -f [ID]

删除多个容器

$ docker container rm [ID] [ID] [ID]

删除所有容器

$ docker rm $(docker ps -aq)

获取日志 (使用容器名称或ID)

$ docker container logs [NAME]

查看容器内运行的进程

$ docker container top [NAME]

TIP: 关于容器

Docker containers are often compared to virtual machines but they are actually just processes running on your host os. In Windows/Mac, Docker runs in a mini-VM so to see the processes youll need to connect directly to that. On Linux however you can run "ps aux" and see the processes directly

镜像命令

查看所有拉取的镜像

$ docker image ls

拉取镜像

$ docker pull [IMAGE]

删除镜像

$ docker image rm [IMAGE]

删除所有镜像

$ docker rmi $(docker images -a -q)

TIP: ABOUT IMAGES

  • Images are app bianaries and dependencies with meta data about the image data and how to run the image
  • Images are no a complete OS. No kernel, kernel modules (drivers)
  • Host provides the kernel, big difference between VM

容器创建示例

NGINX:

$ docker container run -d -p 80:80 --name nginx nginx (-p 80:80 is optional as it runs on 80 by default)

APACHE:

$ docker container run -d -p 8080:80 --name apache httpd

MONGODB:

$ docker container run -d -p 27017:27017 --name mongo mongo

MYSQL:

$ docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql

容器信息

查看容器信息

$ docker container inspect [NAME]

查看指定特性 (--format)

$ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]

查看性能状态 (cpu, mem, network, disk, etc)

$ docker container stats [NAME]

访问容器

创建nginx容器并进入bash窗口访问

Create new nginx container and bash into

$ docker container run -it --name [NAME] nginx bash
  • i = interactive Keep STDIN open if not attached
  • t = tty - Open prompt

For Git Bash, use "winpty"

$ winpty docker container run -it --name [NAME] nginx bash

创建并运行Ubuntu

$ docker container run -it --name ubuntu ubuntu

(no bash because ubuntu uses bash by default)

退出是自动停止容器(-rm)

You can also make it so when you exit the container does not stay by using the -rm flag

$ docker container run --rm -it --name [NAME] ubuntu

启用已经存在的容器(start -ai)

Access an already created container, start with -ai

$ docker container start -ai ubuntu

使用exec修改配置

Use exec to edit config, etc

$ docker container exec -it mysql bash

Alpine是适合docker的小巧的linux发行版本

Alpine is a very small Linux distro good for docker

$ docker container run -it alpine sh

(use sh because it does not include bash)
(alpine uses apk for its package manager - can install bash if you want)

网络

"bridge" or "docker0" is the default network

查看端口

$ docker container port [NAME]

查看网络

List networks

$ docker network ls

检查网络

$ docker network inspect [NETWORK_NAME]
("bridge" is default)

创建网络

$ docker network create [NETWORK_NAME]

创建容器并指定网络

Create container on network

$ docker container run -d --name [NAME] --network [NETWORK_NAME] nginx

容器连接网络

Connect existing container to network

$ docker network connect [NETWORK_NAME] [CONTAINER_NAME]

网络移除容器

Disconnect container from network

$ docker network disconnect [NETWORK_NAME] [CONTAINER_NAME]

容器移除网络

Detach network from container

$ docker network disconnect

镜像打标签 & 推送DockerHub

IMAGE TAGGING & PUSHING TO DOCKERHUB

tags are labels that point to an image ID

$ docker image ls

Youll see that each image has a tag

Retag existing image

$ docker image tag nginx btraversy/nginx

Upload to dockerhub

$ docker image push bradtraversy/nginx

If denied, do

$ docker login

Add tag to new image

$ docker image tag bradtraversy/nginx bradtraversy/nginx:testing

DOCKERFILE PARTS

  • FROM - The os used. Common is alpine, debian, ubuntu
  • ENV - Environment variables
  • RUN - Run commands/shell scripts, etc
  • EXPOSE - Ports to expose
  • CMD - Final command run when you launch a new container from image
  • WORKDIR - Sets working directory (also could use 'RUN cd /some/path')
  • COPY # Copies files from host to container

Build image from dockerfile (reponame can be whatever)

From the same directory as Dockerfile

$ docker image build -t [REPONAME] .

TIP: CACHE & ORDER

  • If you re-run the build, it will be quick because everythging is cached.
  • If you change one line and re-run, that line and everything after will not be cached
  • Keep things that change the most toward the bottom of the Dockerfile

EXTENDING DOCKERFILE

Custom Dockerfile for html paqge with nginx

FROM nginx:latest # Extends nginx so everything included in that image is included here
WORKDIR /usr/share/nginx/html
COPY index.html index.html

Build image from Dockerfile

$ docker image build -t nginx-website

Running it

$ docker container run -p 80:80 --rm nginx-website

Tag and push to Dockerhub

$ docker image tag nginx-website:latest btraversy/nginx-website:latest
$ docker image push bradtraversy/nginx-website

VOLUMES

Volume - Makes special location outside of container UFS. Used for databases

Bind Mount -Link container path to host path

Check volumes

$ docker volume ls

Cleanup unused volumes

$ docker volume prune

Pull down mysql image to test

$ docker pull mysql

Inspect and see volume

$ docker image inspect mysql

Run container

$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql

Inspect and see volume in container

$ docker container inspect mysql

TIP: Mounts

  • You will also see the volume under mounts
  • Container gets its own uniqe location on the host to store that data
  • Source: xxx is where it lives on the host

Check volumes

$ docker volume ls

There is no way to tell volumes apart for instance with 2 mysql containers, so we used named volumes

Named volumes (Add -v command)(the name here is mysql-db which could be anything)

$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql

Inspect new named volume

docker volume inspect mysql-db

BIND MOUNTS

  • Can not use in Dockerfile, specified at run time (uses -v as well)
  • ... run -v /Users/brad/stuff:/path/container (mac/linux)
  • ... run -v //c/Users/brad/stuff:/path/container (windows)

TIP: Instead of typing out local path, for working directory use $(pwd):/path/container - On windows may not work unless you are in your users folder

Run and be able to edit index.html file (local dir should have the Dockerfile and the index.html)

$ docker container run  -p 80:80 -v $(pwd):/usr/share/nginx/html nginx

Go into the container and check

$ docker container exec -it nginx bash
$ cd /usr/share/nginx/html
$ ls -al

You could create a file in the container and it will exiost on the host as well

$ touch test.txt

DOCKER COMPOSE

  • Configure relationships between containers
  • Save our docker container run settings in easy to read file
  • 2 Parts: YAML File (docker.compose.yml) + CLI tool (docker-compose)

1. docker.compose.yml - Describes solutions for

  • containers
  • networks
  • volumes

2. docker-compose CLI - used for local dev/test automation with YAML files

Sample compose file (From Bret Fishers course)

version: '2'

# same as
# docker run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve

services:
  jekyll:
    image: bretfisher/jekyll-serve
    volumes:
      - .:/site
    ports:
      - '80:4000'

To run

docker-compose up

You can run in background with

docker-compose up -d

To cleanup

docker-compose down

复制容器内到文件到本地

docker cp $(docker inspect -f '{{.Id}}' 容器名):容器路径 本地路径

参考:
docker-help

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,924评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,781评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,813评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,264评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,273评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,383评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,800评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,482评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,673评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,497评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,545评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,240评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,802评论 3 304
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,866评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,101评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,673评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,245评论 2 341

推荐阅读更多精彩内容