Docker初步
0、检查系统
[root@5a25c7e57895 ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
1、安装docker环境
yum install -y docker
[root@5a25c7e57895 ~]# systemctl start docker.service
Failed to get D-Bus connection: Operation not permitted
[root@d96182175e58 ~]# systemctl enable docker.service
Operation failed: No such file or directory
### 问题原因
### 因为本人使用的是docker容器安装的docker
### 解决方案---我没有解决了,可能还需要将CMD或者entrypoint设置为/usr/sbin/init
### 启动时设置参数 --privileged
### 使用该参数,Container内的root拥有真正的root权限
### 否则,container内的root只是外部的一个普通用户权限。
### docker run --privileged=true -it --name centosSSH centos:7 /bin/bash
### docker run --privileged -d -p 30001:12007 -p 31001:22 --name jedi-smq hfq/jedi:v6
2、docker之Hello World
#从镜像仓库中拉去hello-world镜像
root@5a25c7e57895:~# docker pull hub.c.163.com/library/hello-world:latest
#运行hello-world images
root@5a25c7e57895:~# docker run hub.c.163.com/library/hello-world
#输出的内容
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.
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://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
root@5a25c7e57895:~#
3、Docker基础命令
##从镜像仓库中拉去image镜像
root@5a25c7e57895:~# docker pull hub.c.163.com/library/hello-world:latest
##查看当前有的image镜像
root@5a25c7e57895:~# docker images
REPOSITORY TAG IMAGE ID hello-world latest 48b5124b2768 8 weeks ago 1.84 kB
hub.c.163.com/library/hello-world latest 48b5124b2768 8 weeks ago 1.84 kB
##启动一个镜像
root@5a25c7e57895:~# docker run hub.c.163.com/library/hello-world
Hello from Docker!
##docker run 常用命令参数
-it 交互式
--name 给Container起名字
-d 后台启动
-p 端口
-P 大写p,随机端口
-v 磁盘映射
eg: docker run -it -p 8080:80 -v /local/dir:/docker/container/dir ubuntu:16.04 /bin/bash
##查看container状态
root@5a25c7e57895:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
root@5a25c7e57895:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6979117aa3d8 hub.c.163.com/library/hello-world "/hello" 31 minutes ago Exited (0) 31 minutes ago
##其他##
#删除container
rm <container id>
#删除image
docker rmi <image id>
#修改tag名称
docker tag old-image[:old-tag] new-image[:new-tag]
#退出后台运行:在交互式情况下执行
Ctrl + P ;Ctrl + Q
#批量删除container,其中"xxx/jedi:v5"是过滤内容项
docker rm $(docker ps -a | grep "xxx/jedi:v5" | awk '{print $1}')
通过Dockerfile构建docker镜像
4、编辑Dockerfile文件
FROM #基础镜像来源
MAINTAINER #作者
ADD && COPY #拷贝物理机文件到Docker容器中
RUN #配置image环境的软件等
ENV #配置环境变量
EXPOSE #暴露端口
VOLUME #挂载磁盘
ENTRYPOINT && CMD #容器启动的时候执行的命令
Dockerfile样例: http://blog.csdn.net/lsziri/article/category/6785100
5、构建image镜像
## -t 表示tag .代表Dockerfile中的相对路径,绝对路径需要加 -f
docker build -t my/ubuntu:14.04 .