Docker 使用总结

警告
本文最后更新于 2020-06-24,文中内容可能已过时。

Docker 一些实用技巧。

安装

Ubuntu

Ubuntu 操作系统的安装步骤可以参考官网说明

  1. 卸载旧版本

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    sudo apt-get remove docker docker-engine docker.io containerd runc
    
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Package 'docker-engine' is not installed, so not removed
    Package 'docker' is not installed, so not removed
    Package 'containerd' is not installed, so not removed
    Package 'docker.io' is not installed, so not removed
    Package 'runc' is not installed, so not removed
    0 upgraded, 0 newly installed, 0 to remove and 43 not upgraded.
  2. 添加软件源

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    sudo apt-get update
    
    sudo apt-get install \
       apt-transport-https \
       ca-certificates \
       curl \
       gnupg-agent \
       software-properties-common
    
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
    echo $(lsb_release -cs)
    Linux Mint Releases
    Version	Codename	Package base
    19.1	Tessa	Ubuntu Bionic
    19	Tara	Ubuntu Bionic
    18.3	Sylvia	Ubuntu Xenial
    18.2	Sonya	Ubuntu Xenial
    
    # sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu Bionic stable"
  3. 开始安装

    1
    2
    
    sudo apt-get update
    sudo apt install docker.io
  4. 设置启动服务

    1
    2
    3
    4
    
    sudo systemctl start docker
    
    ## 添加开机启动
    sudo systemctl enable docker
  5. 查看 Docker 版本

    1
    2
    3
    
    docker --version
    
    Docker version 19.03.6, build 369ce74a3c
  6. Docker 后台服务需要具有 sudo 权限。为了避免每次命令都输入sudo,可以把用户加入 Docker 用户组(官方文档)。

    1
    2
    3
    4
    
    ## 建立 docker 组
    sudo groupadd docker
    ## 把当前用户添加到 docker 组
    sudo usermod -aG docker $USER

    然后先退出账户(logout),再次登录(login)即可使用 docker 命令了

  7. 运行试试看

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    docker run hello-world
    
    Unable to find image 'hello-world:latest' locally
    
    latest: Pulling from library/hello-world
    0e03bdcc26d7: Pull complete
    Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
    Status: Downloaded newer image for hello-world:latest
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
    1. The Docker client contacted the Docker daemon.
    2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
       (amd64)
    3. The Docker daemon created a new container from that image which runs the
       executable that produces the output you are currently reading.
    4. The Docker daemon streamed that output to the Docker client, which sent it
       to your terminal.
    
    To try something more ambitious, you can run an Ubuntu container with:
    $ docker run -it ubuntu bash
    
    Share images, automate workflows, and more with a free Docker ID:
    https://hub.docker.com/
    
    For more examples and ideas, visit:
    https://docs.docker.com/get-started/

    如果还有出现以下的报错,需要修改权限

    1
    2
    3
    
    WARNING: Error loading config file: /home/william/.docker/config.json: stat /home/william/.docker/config.json: permission denied
    
    sudo chown william:william /home/william/.docker -R
  8. 由于某些原因,国内访问 Docker 的软件源速度是比较慢的。幸好,我们可以更改指定的源,使用国内阿里云或者网易可以大大的加速访问速度。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    ## 修改 docker 配置文件
    sudo vim /etc/docker/daemon.json
    
    {
     "registry-mirrors" : [
       "http://ovfftd6p.mirror.aliyuncs.com",
       "http://registry.docker-cn.com",
       "http://docker.mirrors.ustc.edu.cn",
       "http://hub-mirror.c.163.com"
     ],
     "insecure-registries" : [
       "registry.docker-cn.com",
       "docker.mirrors.ustc.edu.cn"
     ],
     "debug" : true,
     "experimental" : true
    }
    
    sudo systemctl restart docker.service

CentOS

  1. 删除旧版本

    1
    2
    3
    4
    5
    6
    7
    8
    
    sudo yum remove docker \
                     docker-client \
                     docker-client-latest \
                     docker-common \
                     docker-latest \
                     docker-latest-logrotate \
                     docker-logrotate \
                     docker-engine
  2. 添加软件源

    1
    2
    3
    4
    
    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
    sudo yum update
  3. 安装

    1
    
    sudo yum install docker.io
  4. 也可以使用国内 daocloud 一键安装命令:

    1
    2
    3
    4
    5
    
    ## 阿里
    curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    
    ## daocloud
    curl -sSL https://get.daocloud.io/docker | sh
  5. 加入 docker 用户组命令

    1
    
    sudo usermod -aG docker trader
  6. 添加启动

    1
    2
    
    sudo systemctl enable docker
    sudo systemctl start docker
  7. 运行测试

    1
    
    sudo docker run hello-world

基础概念

编写 Dockerfile

可以使用一下的模板来编写一个测试使用的 CentOS7 开发环境

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
FROM centos:7

MAINTAINER WilliamFang
LABEL Remarks="CentOS7.5 Develop&Testing Environment"

RUN yum -y install vim git sudo && yum -y install make && \
    yum -y install gcc gcc-c++ kernel-devel && \
    yum -y install cmake bzip2 htop tldr pigz pbzip2 && \
    yum -y install bzip2-devel.x86_64 && \
    yum -y install libxslt-devel libffi-devel openssl-devel libcurl-devel

ENV PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
ENV LANG=en_US.UTF-8
ENV BASH_ENV=~/.bashrc  \
    ENV=~/.bashrc  \
    PROMPT_COMMAND="source ~/.bashrc"

然后使用命令开始搭建

1
sudo docker build -t myctp:v1.0 .

然后就可以看到

1
2
3
4
5
docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myctp               v1.0                91b0c32f2935        2 minutes ago       564MB
centos              7                   b5b4d78bc90c        7 weeks ago         203MB

现在,我们就可以愉快的使用 Docker 进行测试了

1
2
3
4
## 查看当前运行的 docker container
docker ps -a

CONTAINER ID    IMAGE    COMMAND    CREATED    STATUS    PORTS    NAMES

说明当前还没有生成相应的实例。我们可以启动使用命令启动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
## 使用 REPOSITORY:TAG
## 使用 -v 可以挂载主机文件
docker run -dit -v /home/william:/mnt myctp:v1.0 /bin/bash

CONTAINER ID    IMAGE       COMMAND         CREATED         STATUS          PORTS   NAMES
2b40845d0309    myctp:v1.0  "/bin/bash"     4 seconds ago   Up 3 seconds            upbeat_montalcini

docker run --name rshiny -dit -e USER=rshiny -e PASSWORD=ilovewuya -p 58787:8787 -p 53838:3838 wuya-centos7:v1.0
## 添加用户,需要进入 docker 添加用户
docker exec -it rshiny /bin/bash
sudo adduser tester
sudo passwd tester

然后开始启动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## 挂载到实例
docker exec -it b3c220b3c9c6 /bin/bash

[root@b3c220b3c9c6 /]# whoami
root

[root@b3c220b3c9c6 /]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
[root@b3c220b3c9c6 /]#

退出会依然可以看到程序在运行中

1
2
CONTAINER ID    IMAGE       COMMAND         CREATED         STATUS          PORTS   NAMES
b3c220b3c9c6    myctp:v1.0  "/bin/bash"     2 minutes ago   Up 2 minutes            gallant_bell

删除

1
2
3
4
5
6
## 列出所有
docker ps -aq
## 删除指定 id
docker rm
## 删除 image
docker rmi

列出所有容器 ID

1
docker ps -aq

查看所有运行或者不运行容器

1
docker ps -a

停止所有的 container(容器),这样才能够删除其中的 images:

1
docker stop $(docker ps -a -q) 或者 docker stop $(docker ps -aq)

如果想要删除所有 container(容器)的话再加一个指令:

1
docker rm $(docker ps -a -q) 或者 docker rm $(docker ps -aq)

查看当前有些什么 images

1
docker images

删除 images(镜像),通过 image 的 id 来指定删除谁

1
docker rmi <image id>

想要删除 untagged images,也就是那些 id 为的 image 的话可以用

1
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

要删除全部 image(镜像)的话

1
docker rmi $(docker images -q)

强制删除全部 image 的话

1
docker rmi -f $(docker images -q)

从容器到宿主机复制

1
2
docker cp tomcat:/webapps/js/text.js /home/admin
 docker  cp 容器名:  容器路径       宿主机路径

从宿主机到容器复制

1
2
docker cp /home/admin/text.js tomcat:/webapps/js
 docker cp 宿主路径中文件      容器名  容器路径

删除所有停止的容器

1
docker container prune

删除所有不使用的镜像

1
docker image prune --force --all或者docker image prune -f -a

停止、启动、杀死、重启一个容器

1
2
3
4
docker stop Name或者ID
docker start Name或者ID
docker kill Name或者ID
docker restart name或者ID

发布 docker

为了方便移植,Docker 允许我们通过两个方式来分享配置好的运行环境。

Docker Hub

通过使用 Docker Hub 来发布。

提交

1
docker commit -a "william" -m "myctp:v0.1" -p myctp.new myctp:v0.1

导出

1
docker save -o myctp.v0.1.tar myctp:v0.1

导入

1
docker run -d --name myctp -it -v /home/william:/mnt myctp:v0.1 /bin/zsh

运行

1
docker exec -it myctp.new /bin/zsh

提交 Docker Hub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
docker login
UserName: williamlfnag
Password: ************

## 标记需要处理的image
docker tag myctp:v0.1.2 williamlfang/myctp


REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
myctp                v0.1.2              9c41f991a440        13 minutes ago      4.57GB
williamlfang/myctp   latest              9c41f991a440        13 minutes ago      4.57GB
myctp                v0.1.1              59c7bc923b02        3 days ago          4.02GB
myctp                v0.1                e8a31774a8c6        4 days ago          2.49GB
centos               7                   b5b4d78bc90c        8 weeks ago         203MB

docker push williamlfang/myctp

docker pull williamlfang/myctp

使用 williamlfang

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## 显示当前可用镜像
docker image ls

## 如果没有 williamlfang/myctp
docker pull williamlfang/myctp

## 再次确认已经下载到本地
docker image ls

## 基于此镜像生成 container
docker run -d --name myctp.dockerhub -it williamlfang/myctp /bin/zsh
## 查看 docker 目前的所有 container
docker ps -a


CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                       PORTS               NAMES
dcc7924f4c1a        williamlfang/myctp   "/bin/zsh"          4 seconds ago       Up 3 seconds                                     myctp.dockerhub
c57d4de5205b        f03a8c4cf617         "/bin/zsh"          12 days ago         Exited (255) 4 minutes ago                       myctp
b50399b23d5b        myctp:v0.1           "/bin/zsh"          13 days ago         Exited (137) 13 days ago                         myctp.test
11953e12a6b5        e2c0099752c8         "/bin/zsh"          13 days ago         Exited (137) 13 days ago                         myctp.new

## 进入 container 操作
docker exec -it myctp.dockerhub /bin/zsh

以下就是进入我们的操作系统后的截图显示

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
⚡ root@centos7  /  j myctp
anaconda-post.log  bin  data  dev  etc  home  lib  lib64  log  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

/root/myCTP
build  CMakeLists.txt  config  CTP踩坑记.md  data  deps  include  libs  log  scripts  src

⚡ root@centos7  myCTP  ll
build  CMakeLists.txt  config  CTP踩坑记.md  data  deps  include  libs  log  scripts  src

total 64K
drwxr-xr-x 11 root root 4.0K Jul  4 17:06 .
dr-xr-x---  1 root root 4.0K Jul 13 17:27 ..
drwxr-xr-x  4 root root 4.0K Jul  4 16:59 build
-rw-r--r--  1 root root 5.6K Jul  4 16:51 CMakeLists.txt
drwxr-xr-x  2 root root 4.0K Jul  4 16:37 config
-rw-r--r--  1 root root 5.0K Jul  4 16:37 CTP踩坑记.md
drwx------  3 root root 4.0K Jul  4 17:06 data
drwxr-xr-x  5 root root 4.0K Jul  4 16:42 deps
drwxr-xr-x 10 root root 4.0K Jul  4 16:37 include
drwxr-xr-x 11 root root 4.0K Jul  4 16:59 libs
drwx------  4 root root 4.0K Jul  4 17:06 log
drwxr-xr-x  2 root root 4.0K Jul  4 16:39 scripts
drwxr-xr-x 10 root root 4.0K Jul  4 16:37 src

使用技巧

可以执行变量名称

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## 增加额外运行参数
docker run -d \
	--name ctpmd -it \
	--restart=always \
	--log-driver json-file \
	--log-opt max-size=1000m \
	--log-opt max-file=30 \
	--network="host" \
	--ipc="host" \
	-e ACCOUNT=local_simnow \
	-v /home/william/mkdata:/data \
	registry.corp.highfortfunds.com/highfort/ctpmd:v0.1

## 重新打 tag
docker tag 8557026cb47e[原来的image id] registry.corp.highfortfunds.com/highfort/ctpmd:v0.1

## 报错无法删除
## Ref: https://stackoverflow.com/questions/38118791/can-t-delete-docker-image-with-dependent-child-images
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
docker rmi c565603bc87f

# 设置日志文件

docker tag 29db0d77705f  registry.corp.highfortfunds.com/highfort/ctpmd:v0.1

docker push registry.corp.highfortfunds.com/highfort/ctpmd:v0.1
docker pull registry.corp.highfortfunds.com/highfort/ctpmd:v0.1

docker run -d \
	--name ctpmd -it \
	--restart=always \
	--log-driver json-file \
	--log-opt max-size=1000m \
	--log-opt max-file=30 \
	--network="host" \
	--ipc="host" \
	-e ACCOUNT=colo_gtja \
	-v /data:/data \
	registry.corp.highfortfunds.com/highfort/ctpmd:v0.1

docker run -d \
	--name ctpmd -it \
	--restart=always \
	--log-driver json-file \
	--log-opt max-size=1000m \
	--log-opt max-file=30 \
	--network="host" \
	--ipc="host" \
	-e ACCOUNT=local_hf \
	-v /data:/data \
	registry.corp.highfortfunds.com/highfort/ctpmd:v0.1

docker run -d \
	--name ctpmd -it \
	--restart=always \
	--log-driver json-file \
	--log-opt max-size=1000m \
	--log-opt max-file=30 \
	--network="host" \
	--ipc="host" \
	-e ACCOUNT=local_zz \
	-v /data:/data \
	registry.corp.highfortfunds.com/highfort/ctpmd:v0.1

docker exec -it ctpmd /bin/bash

相关内容

william 支付宝支付宝
william 微信微信
0%