This page looks best with JavaScript enabled

Docker Operations Practices

 ·  ☕ 2 min read

Compiled from the “Development Tips” series, a roundup of common problems and solutions in Docker usage and operations.

1. Basic Docker Machine Commands

  • Create the my-vm-name virtual machine
1
docker-machine create --driver virtualbox my-vm-name
  • List all docker-machine instances
1
2
3
4
docker-machine ls
NAME         ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
default      *        virtualbox   Running   tcp://192.168.99.101:2376           v18.06.1-ce
my-vm-name   -        virtualbox   Running   tcp://192.168.99.102:2376           v18.06.1-ce
  • Log in to a docker-machine
1
2
docker-machine ssh default
docker@default:~$
  • Get the host IP of a docker-machine
1
2
docker-machine ip default
192.168.99.101
  • Other commands
1
docker-machine  [命令] [主机名]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
active  查看活跃的 Docker 主机
config  输出连接的配置信息
create  创建一个 Docker 主机
env  显示连接到某个主机需要的环境变量
inspect  输出主机更多信息
kill  停止某个主机
regenerate-certs  为某个主机重新生成 TLS 认证信息
restart  重启主机
rm  删除某台主机
ssh  SSH 到主机上执行命令
scp  在主机之间复制文件
start  启动一个主机
stop  停止一个主机
upgrade  更新主机 Docker 版本为最新
url  获取主机的 URL

2. Installing Software on Docker Machine

On Windows or OS X, using Docker requires a virtual machine created by VirtualBox from the Boot2Docker image. Boot2Docker is based on Tiny Linux and provides the tce-load package management tool.

Taking Python as an example:

1
2
tce-load -wi python
curl https://bootstrap.pypa.io/get-pip.py | sudo python -

List of installable packages for tce-load:

http://distro.ibiblio.org/tinycorelinux/tcz_2x.html

Documentation for the tce-load command:

http://wiki.tinycorelinux.net/wiki:tce-load

3. Docker Commands

  • Start a container; port mapping format: hostPort:containerPort
1
2
3
4
5
docker container run \
  -d \
  -p 127.0.0.1:8080:80 \
  --name nginxname \
  nginx
  • Log in to a container with a shell
1
docker exec -it containerID bash
  • View a container’s logs
1
docker logs -f containerID

4. Configuring a Docker Accelerator on VirtualBox

Edit the .docker\machine\machines\default\config.json file and add:

1
2
3
"RegistryMirror": [
    "http://f1361db2.m.daocloud.io"
],

5. Accessing Host Services from Inside Docker

Inside Docker, using localhost directly to reach a host service fails with a network error; you need to go through docker0.

  • On Linux, to view the IP address of docker0 on the host, run:
1
ip addr show docker0
  • On OS X, you can use this address directly.
1
docker.for.mac.host.internal

6. Migrating or Expanding the /var/lib/docker Directory

  • Stop the relevant services to ensure data consistency
1
service docker stop
  • Back up the data
1
mv /var/lib/docker /var/lib/docker_bk
  • Migrate the data
1
2
3
4
mkdir -p /data/docker/
rsync -avz /var/lib/docker_bk/ /data/docker/
du -h --max-depth=0 /data/docker
ln -s /data/docker /var/lib/docker
  • Restart the relevant services
1
service docker start

7. Problems Running Java Projects in Docker

The JVM does not know it is running in a container and mistakenly treats physical resources as the container’s available resources; this was only fixed in Java 10,

Workarounds for other versions:

  • java5/6/7/8u131-, add a startup parameter
1
-Xmx`cat /sys/fs/cgroup/memory/memory.limit_in_bytes`
  • java8u131+ and java9+, add a startup parameter
1
-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap

Reference link: A few small issues to watch when running Java in a container (docker)

8. Multiple From Instructions in a Dockerfile

For an image generated from multiple From instructions, the last From prevails.

Multiple From instructions exist to distinguish different build stages, for example separating the build environment from the runtime environment. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
FROM golang:1.12 as controller-manager-builder

COPY / /go/src/myapp
WORKDIR /go/src/myapp

RUN CGO_ENABLED=0 GO111MODULE=on GOOS=linux GOARCH=amd64 GOFLAGS=-mod=vendor go build --ldflags "-extldflags -static" -o controller-manager ./cmd/controller-manager/

FROM alpine:3.7
RUN apk add --update ca-certificates && update-ca-certificates
COPY --from=controller-manager-builder /go/src/myapp/controller-manager /usr/local/bin/
CMD controller-manager

The golang image is used to build the source code and the alpine image is used to run the program; the link between the two is that COPY --from= copies the binary from one image to the other.


微信公众号
WRITTEN BY
微信公众号