Docker常用基本命令

中文文档:https://www.runoob.com/docker/docker-command-manual.html

INSTALL DOCKER ENGINE

Note: This procedure works for Debian on x86_64 / amd64, Debian ARM, or Raspbian.

  1. Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:
 $ sudo apt-get update
 $ sudo apt-get install docker-ce docker-ce-cli containerd.io

Got multiple Docker repositories?
If you have multiple Docker repositories enabled, installing or updating without specifying a version in the apt-get install or apt-get update command always installs the highest possible version, which may not be appropriate for your stability needs.

  1. To install a specific version of Docker Engine, list the available versions in the repo, then select and install:
    a. List the versions available in your repo:
$ apt-cache madison docker-ce

  docker-ce | 5:18.09.1~3-0~debian-stretch | https://download.docker.com/linux/debian stretch/stable amd64 Packages
  docker-ce | 5:18.09.0~3-0~debian-stretch | https://download.docker.com/linux/debian stretch/stable amd64 Packages
  docker-ce | 18.06.1~ce~3-0~debian        | https://download.docker.com/linux/debian stretch/stable amd64 Packages
  docker-ce | 18.06.0~ce~3-0~debian        | https://download.docker.com/linux/debian stretch/stable amd64 Packages
  ...

b. Install a specific version using the version string from the second column, for example, 5:18.09.1~3-0~debian-stretch .

$ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
  1. Verify that Docker Engine is installed correctly by running the hello-world image.
$ sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

Docker Engine is installed and running. The docker group is created but no users are added to it. You need to use sudo to run Docker commands. Continue to Linux postinstall to allow non-privileged users to run Docker commands and for other optional configuration steps.

UNINSTALL DOCKER ENGINE

sudo apt-get remove docker docker-engine docker.io containerd runc

常用命令

  • 从仓库拉取镜像
docker pull python
  • 运行一个容器
docker run -it -d --name python -p 82:80 -v /data/www/python/:/usr/src/myapp/ python bash
# -d:后台运行
# --name python:命名容器名称
# -p 82:80:端口映射
# -v /data/www/python/:/usr/src/myapp/:目录映射
#  python:镜像名称
  • 查看镜像
docker images
  • 查看所有容器
docker ps -a
  • 停止、启动、删除、重启容器
# 停止容器
docker stop 758cfe1c133f
# 删除容器
docker rm 758cfe1c133f
# 启动容器
docker start 758cfe1c133f
# 重启容器
docker restart 758cfe1c133f
  • 进入容器
docker exec -it 758cfe1c133f bash
  • 容器与宿主机之间数据拷贝
# 将容器 ee2494b3e14d 的 /var/www/swoft/ 目录拷贝到宿主机 /data/www/test/ 目录
docker cp ee2494b3e14d:/var/www/swoft/ /data/www/test/

# 将宿主机 /data/www/test/ 目录拷贝到容器 ee2494b3e14d 的 /var/www/swoft/ 目录
docker cp /data/www/test/ ee2494b3e14d:/var/www/swoft/