Hugh's Blog

Docker 笔记(持续更新)

安装

# docker
curl -sSL https://get.docker.com/ | sh
docker -v
sudo groupadd docker
sudo usermod -aG docker $USER
groups $USER
# 重新登录或重启机器之后生效

# docker-compose
# https://docs.docker.com/compose/install/#install-compose
sudo curl -L https://github.com/docker/compose/releases/download/1.15.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

常用命令

# 容器
docker ps -a
docker start {id|name}
docker restart {id|name}
docker stop {id|name}
docker stop $(docker ps -aq)
docker rm {id|name}
docker rm -f {id|name}
docker rm $(docker ps -aq)
docker kill {id|name}
docker kill $(docker ps -aq)
docker inspect {id|name}
docker logs {id|name}
docker stats {id|name}

# 容器操作
docker run -it ubuntu /bin/bash
docker run --rm -it ubuntu /bin/bash
docker run --rm -it -e MYSQL_ROOT_PASSWORD=root mysql
docker run --name my_ubuntu -it ubuntu /bin/bash
docker exec -it {id|name} /bin/bash
docker exec my_nginx nginx -v

# 根据 Dockerfile 建立镜像并运行
touch Dockerfile
docker build -t test/myapp .
docker run --name myapp-test -d -p 80:80 -v /data:/data test/myapp

# 从正在运行的容器将文件复制到本机
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp nginx-test:/etc/nginx/conf.d .
docker cp nginx-test:/etc/nginx/conf.d/default.conf .

# 日志输出到文件
docker logs container_name > your.log 2>&1

# 镜像
docker images
docker rmi image_id
docker rmi $(docker images -q)

# 只允许本地 host 访问
docker run -d --rm -p 127.0.0.1:80:80 nginx:alpine

# 查看容器大小
docker ps -s

报错记录

2017-09-24

在配置 MySQL 的时候总是一启动容器就挂掉重启,或者发生连接之后马上就挂掉,查看日志如下:

2017-09-25T15:18:49.001014Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-09-25T15:18:49.004510Z 0 [Note] mysqld (mysqld 5.7.19) starting as process 1 ...
2017-09-25T15:18:49.010118Z 0 [Note] InnoDB: PUNCH HOLE support available
2017-09-25T15:18:49.010212Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-09-25T15:18:49.010241Z 0 [Note] InnoDB: Uses event mutexes
2017-09-25T15:18:49.010289Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2017-09-25T15:18:49.010312Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-09-25T15:18:49.010340Z 0 [Note] InnoDB: Using Linux native AIO
2017-09-25T15:18:49.010947Z 0 [Note] InnoDB: Number of pools: 1
2017-09-25T15:18:49.011280Z 0 [Note] InnoDB: Using CPU crc32 instructions
2017-09-25T15:18:49.014318Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2017-09-25T15:18:49.014440Z 0 [ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12
2017-09-25T15:18:49.014568Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2017-09-25T15:18:49.014609Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2017-09-25T15:18:49.014657Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2017-09-25T15:18:49.014679Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2017-09-25T15:18:49.014698Z 0 [ERROR] Failed to initialize plugins.
2017-09-25T15:18:49.014734Z 0 [ERROR] Aborting

2017-09-25T15:18:49.014754Z 0 [Note] Binlog end
2017-09-25T15:18:49.014848Z 0 [Note] Shutting down plugin 'CSV'
2017-09-25T15:18:49.016819Z 0 [Note] mysqld: Shutdown complete

看起来有一串 ERROR 信息,内存不足、InnoDB 配置错误等等,Google 之后发现也有人出现问题,但是根据解答修改了之后问题依然存在,后来看到说是因为机器内存不足 1GB 所引起的,解决办法是换台至少 1GB 内存的机器,刚好测试机器的内存是 512MB,于是换了机器,果然可以了,不知道这个是 BUG 还是机器限制的问题,不过问题总算是解决了。

2017-12-10

今天在升级网站的时候,运行 docker-compose up -d 之后碰到这样的错误 Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running?,Google 发现有人说用 sudo 解决,觉得可能是之前网站的有些文件是 root 用户的原因(比如日志或者缓存文件),修改所有文件为当前用户之后,问题解决了。

2018-03-17

今天在配置 php-fpm alpine 镜像的时候,Nginx 连接 PHP 报错:Connection refused,在 docker-compose 中 PHP 与 Nginx 应该是同一网络的,很奇怪为什么不能连接,在 stackoverflow 看到有人说把 php-fpm.conf 中的 listen = 127.0.0.1:9000 改成 listen = 0.0.0.0:9000,改了之后确实可以了,可能是 127.0.0.1 只监听本机端口,而 0.0.0.0 则监听所有本机或者网络。

2018-03-23

今天在配置 MySQL 的时候,查看日志出现报错:Operation CREATE USER failed for 'root'@'%',比如开启一个测试容器:

docker run -d -p 3306:3306 -e MYSQL_USER="root" -e MYSQL_DATABASE="test" -e MYSQL_PASSWORD="123456" -e MYSQL_ROOT_PASSWORD="123456" mysql:latest

查了下,是用户初始化的问题,当使用 root 作为默认的时候,只需要设置 MYSQL_ROOT_PASSWORD 就行,其他变量是不需要的,否则 MYSQL_USER 要改为其他用户,例如:

docker run -d -p 3306:3306 -e MYSQL_USER="test" -e MYSQL_DATABASE="test" -e MYSQL_PASSWORD="123456" -e MYSQL_ROOT_PASSWORD="123456" mysql:latest

具体可以看这里:ERROR 1396 (HY000): Operation CREATE USER failed for ‘root’@‘%’ #129


参考

Docker Cloud - Fail to Deploy MySQL Service