文档与安装
基本安装 官方的文档走就行了,
本文罗列一些简单步骤
https://docs.docker.com/install/linux/docker-ce/ubuntu/#prerequisites
1,安装 前如果之前有安装 则删除
sudo apt-get remove docker docker-engine docker.io
2,查看自己的是哪个版本,官网会有不同的命令
sudo lsb_release -a
我是ubuntu16.04 ,
3,文档提供了几种安装 方案
-1,设置 docker的仓库后 apt-get
-2,用deb包
-3,用 convenience scripts 来安装
一般都用 1,这里也使用1
sudo apt-get update
4 安装 其它
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
5,增加docker的官方GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
6,检验下fingerprint
sudo apt-key fingerprint 0EBFCD88
7 增加仓库,(这里也有选择是什么cpu架构,一般都是x86_64位)
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
8 安装
sudo apt-get install docker-ce
9 测试
sudo docker run hello-world
10 查看 版本
sudo docker version
例子1,用hub.docker.com拉取一个nginx
查看本机有多少image
sudo docker image ls
我们尝试云hub.docker.com 中拉一个出来测试下
https://hub.docker.com/_/nginx/
docker pull nginx
hub.docker.com 是一个docker 镜像的仓库,里面有包括 ubuntu, redhat 这样的 系统镜像,也有在之上的 nginx,redis等,也包含 python之类的各版本镜像, 即拉即用,牛逼轰轰.
启动:
docker run --name some-nginx -d -p 8080:80 nginx
查看进程
ps -ef | grep docker
/usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8080 -container-ip 172.17.0.2 -container-port 80
测试
wget http://127.0.0.1:8080
例子2,自建一个Dockerfile 后build,后去执行
1,写一个python文件
新建一个文件 hello.py
print("hello,world,first cmd")
测试,执行, python hello.py OK,
2, 新建一个Dockerfile
vi Dockerfile
内容如下
FROM python:3.6.7
ADD hello.py /
WORKDIR /
CMD ["python","hello.py"]
3, 因为依赖了 python3.6.7,这里要和hub.docker.com连接,进行登陆
ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ 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: xxxxxx
Password:
WARNING! Your password will be stored unencrypted in /home/ming/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
4,把Dockerfile build成image
ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker build -t xxxxxx/hellopy .
Sending build context to Docker daemon 15.87kB
Step 1/4 : FROM python:3.6.7
3.6.7: Pulling from library/python
54f7e8ac135a: Pull complete
d6341e30912f: Pull complete
087a57faf949: Pull complete
5d71636fb824: Pull complete
0c1db9598990: Pull complete
2eeb5ce9b924: Pull complete
a8c530378055: Pull complete
687ed2fb2a0d: Pull complete
620aea26e853: Pull complete
Digest: sha256:fc34d5b6cf5d00a6139a74370dc27ddc9ce18303e2210d0f199a6050cc29aa45
Status: Downloaded newer image for python:3.6.7
---> 1ec4d11819ad
Step 2/4 : ADD hello.py /
---> ac96cabf064f
Step 3/4 : WORKDIR /
---> Running in d3565c0d40eb
Removing intermediate container d3565c0d40eb
---> 7635082ac9d2
Step 4/4 : CMD ["python","hello.py"]
---> Running in 062edb8e18b4
Removing intermediate container 062edb8e18b4
---> 55c88d812066
Successfully built 55c88d812066
Successfully tagged xxxxxx/hellopy:latest
5,查看image一些状态
ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
xxxxxx/hellopy latest 55c88d812066 45 seconds ago 918MB
<none> <none> f8d761b2ce56 11 minutes ago 31B
<none> <none> 175510e82d22 12 minutes ago 31B
<none> <none> a3b03c0ad5c3 14 minutes ago 31B
<none> <none> 9c7e982eeba3 17 minutes ago 31B
<none> <none> 551d0f216a27 18 minutes ago 31B
nginx latest 568c4670fa80 9 days ago 109MB
python 3.6.7 1ec4d11819ad 2 weeks ago 918MB
hello-world latest 4ab4c602aa5e 3 months ago 1.84kB
ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker history 551d0f216a27
IMAGE CREATED CREATED BY SIZE COMMENT
551d0f216a27 19 minutes ago /bin/sh -c #(nop) CMD ["python hello.py"] 0B
8e315e5e1fde 19 minutes ago /bin/sh -c #(nop) ADD file:69b0acbe51cbcdafd… 31B
6,运行
ming@ming-CW35S:~/dgame/project/docker_pratise/c33$ docker run xxxxxx/hellopy
hello,world,first cmd
其它
把 当前用户加到docker组中,
方便一些权限 问题(很多命令,如docker image ls等不用sudo了)
主要就是 gpasswd -a ming docker
然后重启docker服务
然后重新进shell
如下:
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ docker image ls
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/images/json: dial unix /var/run/docker.sock: connect: permission denied
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ groupadd docker
groupadd:“docker”组已存在
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ gpasswd -a ming docker
gpasswd:没有权限。
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ sudo gpasswd -a ming docker
[sudo] ming 的密码:
正在将用户“ming”加入到“docker”组中
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$ sudo service docker restart
ming@ming-CW35S:~/dgame/project/docker_pratise/c32$
然后再重新进入shell即可,
测试:执行 docker image ls