Docker作为现在常用的服务部署方式,日常工作中免不了要做些build
、push
之类的操作。为了避免每次都要查文档,这里将一些常用的命令和技巧做一些总结。
以下以Ubuntu 16.04 x64
系统作为演示环境。
$ apt install docker
$ docker --version
Docker version 18.06.1-ce, build e68fc7a
$ systemctl daemon-reload
$ systemctl restart docker
此代理主要用于docker访问镜像仓库
创建配置文件:/etc/systemd/system/docker.service.d/http-proxy.conf
,写入以下内容:
[Service]
Environment="HTTP_PROXY=http://web-proxy.com:8080" "HTTPS_PROXY=http://web-proxy.com:8080" "NO_PROXY=internal.com"
重启docker服务,配置生效
Dockerfile
要编译docker镜像,需要先编写Dockerfile
文件。以下是一个Dockerfile
的例子:
FROM python:2.7
# 基于python2.7镜像
RUN apt install python-pip -y \
&& pip install uwsgi \
&& pip install tornado \
&& mkdir /data
# 编译时执行的命令,需要尽量写成一条命令
WORKDIR /data
# 设置工作目录
COPY adb /usr/bin/
# 拷贝文件
COPY api/ /data/api/
# 拷贝目录
ENV TZ="Asia/Shanghai"
# 设置时区
EXPOSE 80/tcp 8080/tcp
# 暴露服务端口
ENTRYPOINT sh /data/start.sh
# 指定入口命令,如果这个命令退出,docker容器也会一起退出
可以看出,格式还是相对比较简单的,照着例子,基本都能写出来。
$ docker build -t demo .
表示使用当前目录的Dockerfile构建镜像demo
。
如果要指定编译时的HTTP代理,可以使用--build-arg
参数:
$ docker build -t demo . --build-arg http_proxy=http://web-proxy.com:8080
如果要指定编译时的dns服务器,可以修改docker的配置文件:/etc/docker/daemon.json
,增加以下配置,并重启docker服务。
"dns": ["10.0.0.1"]
如果Dockerfile
文件不在当前目录下,可以在docker build
命令中使用-f /path/to/Dockerfile
参数来制定Dockerfile
文件的路径。
$ docker run -i demo
$ docker run -i --dns=10.0.0.1 demo
$ docker run -i --env http_proxy=http://web-proxy.com:8080 demo
$ docker run -i -v /home/ubuntu/data:/data demo
这样可以把主机上的/home/ubuntu/data
目录挂载到容器里的/data
下。
默认情况下无法从外部访问容器内的服务,但是可以通过在启动容器时加上-p port1:port2
参数,将容器内的端口port2
映射到本机的port1
端口。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e6ae7638a58d demo "/bin/sh -c '/data/start.sh'" 8 seconds ago Up 7 seconds 80/tcp, 8080/tcp loving_neumann
$ docker exec -i -t e6ae7638a58d sh
# id
uid=0(root) gid=0(root) groups=0(root)
如果默认进去的不是root
权限,可以增加-u root
参数。
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: drunkdream
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
$ docker login hub.tencentyun.com
Username: drunkdream
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
如果仓库使用的不是HTTPS的443端口,需要在主机名后面加上:port
。
如果提示以下错误:
Error response from daemon: Get https://hub.private.com:8080/v2/: http: server gave HTTP response to HTTPS client
说明Server不支持HTTPS协议,需要在配置文件:/etc/docker/daemon.json
中增加以下配置:
"insecure-registries": ["hub.private.com:8080"]
重启docker服务,再登录就可以了
抓包可得以下结果:
GET /v2/ HTTP/1.1
Host: hub.private.com:8080
User-Agent: docker/18.06.1-ce go/go1.10.4 git-commit/e68fc7a kernel/4.10.0-28-generic os/linux arch/amd64 UpstreamClient(Docker-Client/18.06.1-ce \(linux\))
Authorization: Basic YWRtaW46YWRtaW4=
Accept-Encoding: gzip
Connection: close
HTTP/1.1 200 OK
Content-Length: 2
Content-Type: application/json; charset=utf-8
Date: Mon, 25 Feb 2019 04:09:50 GMT
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Connection: close
{}
说明: docker是使用了HTTP协议的Authorization
头进行了身份认证,很容易获取明文密码。因此,为了安全,一定要使用HTTPS协议。
$ docker pull python
Using default tag: latest
latest: Pulling from library/python
741437d97401: Pull complete
34d8874714d7: Pull complete
0a108aa26679: Pull complete
7f0334c36886: Pull complete
65c95cb8b3be: Pull complete
9107d7193263: Pull complete
dd6f212ec984: Pull complete
43288b101abf: Pull complete
89dd65885f16: Pull complete
Digest: sha256:a570ef00b7348c85b546c0e67955fa3be233c27bc2379d0f87fc8e4ff25aa006
Status: Downloaded newer image for python:latest
$ docker tag python:latest hub.tencentyun.com/drunkdream/python:latest
如果要push到非官方仓库,需要在打tag时加上仓库的地址。
$ docker push hub.tencentyun.com/drunkdream/python:latest
The push refers to repository [hub.tencentyun.com/drunkdream/python:latest]
b2f7bd391363: Pushed
08a5b66845ac: Pushed
88a85bcf8170: Pushed
65860ac81ef4: Pushed
a22a5ac18042: Pushed
6257fa9f9597: Pushed
578414b395b9: Pushed
abc3250a6c7f: Pushed
13d5529fd232: Pushed
latest: digest: sha256:35a3001b1defafa4611f764a9c6d07c2146aefc17be2c24ee0200fd37b19b1c7 size: 2218
$ docker save -o demo.tar demo:latest
$ docker load -i demo.tar