linux 环境 ubuntu-18.04.1-desktop-amd64
一、docker 安装
oly@ubuntu-desktop-amd64:~$ sudo apt-get update
oly@ubuntu-desktop-amd64:~$ sudo apt-get install docker.io
oly@ubuntu-desktop-amd64:~$ docker -v
Docker version 18.06.1-ce, build e68fc7a
看到docker版本信息表示安装成功!
二、docker 权限
安装之后当然体验一下docker cmd了。先看一下镜像本地是否有镜像文件: docker images 列出本地镜像
oly@ubuntu-desktop-amd64:~$ docker images
Got permission denied while trying to connect to the Docker daemon socket at ...... connect: permission denied
看信息提示当前用户没有权限。有两种方法
1、docker安装之后会自动创建docker用户组,添加当前用户到docker用户组即可
#创建docker组:
oly@ubuntu-desktop-amd64:~$ sudo groupadd docker
#将当前用户添加到docker组:
oly@ubuntu-desktop-amd64:~$ sudo gpasswd -a $(USER) docker
#重启docker服务:
oly@ubuntu-desktop-amd64:~$ sudo server docker restart
#
oly@ubuntu-desktop-amd64:~$ newgrp docker
2、更改 docker.sock 文件权限
oly@ubuntu-desktop-amd64:~$ ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 1月 11 09:30 /var/run/docker.sock
只需 sudo chmod 666 /var/run/docker.sock 即可
三、docker pull 镜像
解决了权限问题,当然是下载一个自己熟悉的镜像。
oly@ubuntu-desktop-amd64:~$ docker pull ubuntu:16.04
oly@ubuntu-desktop-amd64:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 b0ef3016420a 13 days ago 117MB
也可以通过 docker search 查找所需的镜像,这里就不介绍了!docker rmi ubuntu:16.04 删除镜像
四、docker run
oly@ubuntu-desktop-amd64:~$ docker run --name oly-ubuntu -ti ubuntu:16.04 /bin/bash
root@6c306f61255e:/#
docker run,强烈建议启动加上选项 --name oly-ubuntu【oly-ubuntu 指定容器名】。已经进入docker ubuntu:16.04 镜像系统!e退出:
1、exit 关闭容器
root@6c306f61255e:/# exit
exit
2、ctrl+p+q 不关闭容器
docker ps -a 可以看到当前系统存在的docker容器
oly@ubuntu-desktop-amd64:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9e5b6f8075b ubuntu:16.04 "/bin/bash" 9 seconds ago Exited (0) 8 seconds ago oly-ubuntu
如果不加选项--name呢?也可以启动镜像。
oly@ubuntu-desktop-amd64:~$ docker run -ti ubuntu:16.04 /bin/bash
root@c173c08b3c48:/#
新手注意
oly@ubuntu-desktop-amd64:~$ docker run -ti ubuntu:16.04 /bin/bash 发现是一个新容器
oly@ubuntu-desktop-amd64:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9623cc4bc53 ubuntu:16.04 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago practical_lumiere
c173c08b3c48 ubuntu:16.04 "/bin/bash" 5 minutes ago Exited (0) 2 minutes ago confident_brown
6c306f61255e ubuntu:16.04 "/bin/bash" About an hour ago Exited (0) About an hour ago oly-ubuntu
而 oly@ubuntu-desktop-amd64:~$ docker run --name oly-ubuntu -ti ubuntu:16.04 /bin/bash 防止误操作
oly@ubuntu-desktop-amd64:~$ docker run --name oly-ubuntu -ti ubuntu:16.04 /bin/bash
docker: Error response from daemon: Conflict. The container name "/oly-ubuntu" is already in use by container "6c306f61255e48b8ba16b140dfdc28dcc40ce4e0077dc2fafde902a7ccdeba37". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
Argument 可以是容器别名NAMES 也可以是CONTAINER ID
五、容器删除
容器的删除,docker rm Argument
oly@ubuntu-desktop-amd64:~$ docker rm practical_lumiere
practical_lumiere
oly@ubuntu-desktop-amd64:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c173c08b3c48 ubuntu:16.04 "/bin/bash" 19 minutes ago Exited (0) 16 minutes ago confident_brown
6c306f61255e ubuntu:16.04 "/bin/bash" About an hour ago Exited (0) About an hour ago oly-ubuntu
oly@ubuntu-desktop-amd64:~$ docker rm c173c08b3c48
c173c08b3c48
oly@ubuntu-desktop-amd64:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c306f61255e ubuntu:16.04 "/bin/bash" About an hour ago Exited (0) About an hour ago oly-ubuntu
当我们用ctrl+p+q退出容器时,要想删除容器,需先关闭容器 docker stop Argument 然后 docker rm Argument
六、重启
1、exit 退出容器: docker restart Argument 、docker attach Argument
oly@ubuntu-desktop-amd64:~$ docker restart oly-ubuntu
oly-ubuntu
oly@ubuntu-desktop-amd64:~$ docker attach oly-ubuntu
root@6c306f61255e:/#
2、ctrl+p+q 退出容器:
oly@ubuntu-desktop-amd64:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c306f61255e ubuntu:16.04 "/bin/bash" 2 hours ago Up 2 minutes oly-ubuntu
oly@ubuntu-desktop-amd64:~$ docker attach 6c306f61255e
root@6c306f61255e:/#
STATUS 可以看到不是Exit状态。只需 docker attach Argument 即可