Ubuntu 18.04.5安装docker


安装docker

# 安装依赖包
apt-get install -y apt-transport-https ca-certificates curl software-properties-common
# 设置GPG密钥、stable存储库
# 方案1:该方案可能会报错,建议方案2
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# 方案2:采用镜像源
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# 更新apt包索引
apt-get update
# 安装Docker CE
apt-get install -y docker-ce

安装镜像、容器操作

# 拉取centos7镜像
$ sudo docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
# 查看系统镜像
$ sudo docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       7         eeb6ee3f44bd   3 weeks ago   204MB
# 启动镜像centos7,如果不指定 /bin/bash,容器运行后会自动停止
$ sudo docker run -d -i -t eeb6ee3f44bd /bin/bash
0c44c130a3ea1793a4e63987d5ce585aa07d90c18e0376d50ae9b87e6f760c4d
# 进入容器
$ sudo docker exec -it 0c44c130a3ea1793a4e63987d5ce585aa07d90c18e0376d50ae9b87e6f760c4d bash
[root@0c44c130a3ea /]#

# 挂载目录,直接进入容器,其中7为TAG
$ sudo docker run -it -v  /home/demo/:/home/demo/ --name=centos70 centos:7
[root@975fb7213b9e /]# 

挂载目录到docker容器,挂载到镜像系统的/opt/user/使用参数:-v

$ docker run -it -v <source>:/opt/user/ --name=<container_name> <REPOSITORY>:<TAG>

容器操作

# 启动/停止/重启容器
sudo docker start/stop/restart centos7
# 进入docker容器
sudo docker exec -it centos7 bash
# 删除所有退出的容器
sudo docker rm $(sudo docker ps -a -q)
# 查询所有的容器
sudo docker ps -a
# 查询正在运行的容器
sudo docker ps