Docker常用命令

在平时开发的过程中,docker可以用来方便地获取一个纯净的开发/部署环境,本文介绍了docker的安装方法、最常用的几个命令以及在使用过程中定位问题的方式。

安装docker

使用阿里云源

sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt-get -y install docker-ce
apt-cache madison docker-ce
sudo apt-get -y install docker-ce=[VERSION]

添加docker使用权限

创建docker用户组

sudo groupadd docker

添加用户到docker

sudo usermod -aG docker ${USER}

重启docker服务

sudo systemctrl restart docker

容器镜像操作

启动镜像

启动镜像+挂载一个文件+执行/bin/bash

docker run -v /data/: /data -it image-name /bin/bash

打开容器bash

sudo docker run exec -it ${docker_id} /bin/bash

容器列表

docker ps -a

本地镜像列表

docker images

拷贝文件

docker cp

dockerfile构建镜像

docker build -f ./path_to_dockerfile ./path_to_destination 

查看容器的详细信息

docker inspect

查看容器日志

docker log

修改dockerhub镜像

打开或创建/etc/docker/daemon.json,写入

{
   "registry-mirrors": [
       "https://mirror.ccs.tencentyun.com"
  ]
}

完成以上步骤后,还需要重启docker服务来完成更新。

sudo systemctl daemon-reload
sudo systemctl restart docker

查看docker 服务的运行状态

查看运行状态

systemctl status docker

ubuntu下查看docker服务的运行日志

很多时候需要定位docker服务的失败原因,必须查看一些相关日志,这时就需要用到journalctl,可以帮助我们定位docker服务无法运行的一些奇怪/低级的问题。

journalctl -u docker.service

参考资料

https://docs.docker.com/install/linux/linux-postinstall/
https://cloud.tencent.com/document/product/457/9113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值