Docker basic commands
systemctl start docker
: Start Docker
systemctl stop docker
: Stop Docker
systemctl restart docker
: Restart Docker
systemctl enable docker
: Boot Docker
docker info
: View summary information Docker
docker --help
: View Docker help documentation
docker version
: View version information Docker
Docker mirrored basic commands
. 1, docker images
or docker image ls
: List all the unit image
Optional parameters options
Optional parameters | Parameter Description |
---|---|
-a | All mirrored (including the intermediate layer) |
-q | Show only mirrored Id |
-qa | It can be used in combination |
--digests | Mirror can display summary information |
--no-trunc | You can display the full image information |
2 docker search
: Search Mirror
Optional parameters options
Optional parameters | Parameter Description |
---|---|
--no-trunc | Full description of the image display |
-s | Lists the number of collection mirror is not less than the specified value |
--automated | Lists only mirror type automatic construction Docker Hub |
docker pull
: Pulling mirroring
dockers pull mirroring Name:
Note: Do not add TAG, the default download the latest version of the latest
3 docker rmi
: Remove Mirror
To delete a single
docker rmi 镜像名称:[TAG] 如果不写 TAG 则默认删除最新版 latest
There mirror container generated at run time, it will delete fails, we need to add -f Forces deletion
[root@iZbp17khuqdfkef3nl1db3Z ~]# docker rmi rabbitmq Error response from daemon: conflict: unable to remove repository reference "rabbitmq" (must force) - container 99693943e972 is using its referenced image 84bc4895f175
docker rmi -f 镜像名称1:[TAG] 镜像名称2:[TAG] 多个镜像之间空格隔开
Docker container basic commands
docker run [OPTIONS] image [COMMAND][ARG...]
: Create and launch containerOptional parameters options
Optional parameters Parameter Description --name = 'new container name' Specify a name for the container -i Running in interactive mode containers, usually used simultaneously -t -t A pseudo input terminal reassign a container, and is typically used in conjunction -i -d Background container and return the container id docker run --name 别名 镜像id
: Start ordinary container
docker run -it --name 别名 镜像Id
To run a container, alias, interactive mode, and assigns a pseudo-terminal: Start Interactive containerdocker run -di --name 别名 镜像Id
: Create and launch container in order to protect type way
docker ps [OPTIONS]
: List containerOptional parameters options
Optional parameters Parameter Description -a Show all containers, including not running -f The content display filter conditions --format The return value of the specified template file -l Display container recently created -n N containers display recently created --no-trunc Do not cut output -q Silent mode, only the container ID -s Displays the total file size -qa List all containers id exit
: Container stop exitctrl + p + q
: Container does not stop exitdocker attach 容器Id or 容器名
: Into the containerdockr start 容器id or 容器名
: Start containerdockr restart 容器id or 容器名
: Restart containerdocker stop 容器id or 容器名
: Stop the containerdocker kill 容器id or 容器名
: Stop the violence, directly kill the process (not recommended)docker rm 容器id
: If you remove the container is running, will complain, if you want to delete, you need to force the removal ofdocker rm 容器Id -f
: Force Deletedocker rm -f $(docker ps -qa)
: Remove all containers
Docker containers and host port mapping
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
: Docker RUN: create a new container and run a command
docker run -i -t -p 宿主机端口:容器应用端口 --name 别名 镜像id
: Docker container host port mapping
Optional parameters options
Optional parameters | Parameter Description |
---|---|
-d | Background container and return the container ID |
-i | Running in interactive mode containers, usually used in conjunction with -t |
-P (uppercase p) | Random port mapping, inside the container ports randomly mapped to the host port of the high |
-p (lowercase p) | Designated port mapping format: a host (host) port: the port of the container |
--name | Specify a name for the container |
Docker modify the default storage location, Docker container migration
Docker in the default directory to store images and containers are: / var / lib / Docker / , / var easier under full recommended migration.
My system is Centos7, concrete steps to address the following:
Stop Docker services:
systemctl stop docker; //每个liunx版本的命令不一样。
Create a new directory in the docker sufficient disk space
mkdir -p /data/docker; //在根目录下新建
Migration / var / lib / docker directory of the file to just the new good / data / docker below
cp -rvf /var/lib/docker/* /data/docker
Docker modify configuration
vim /etc/systemd/system/docker.service.d/devicemapper.conf
In the end of the file add:
--graph=/data/docker
Devicemapper.conf file contents are as follows:
[Service] ExecStart= ExecStart=/usr/bin/dockerd --insecure-registry=私服地址 --graph=/home/docker/lib
注意:如果/etc/systemd/system/docker.service.d/devicemapper.conf,这个路径找不到的话,就新建,新建之后加入内容,没有私服地址的话就可以去掉”--insecure-registry=私服地址”。
重载配置,重启docker
systemctl daemon-reload; systemctl restart docker; systemctl enable docker;
启动成功后,确定容器没问题后删除/var/lib/docker/目录中的文件,然后就OK了。
查看容器日志
docker logs
: 获取容器的日志
语法 :docker logs [OPTIONS] CONTAINER
options 可选参数
可选参数 | 参数描述 |
---|---|
-f | 跟踪日志输出 |
--since | 显示某个开始时间的所有日志 |
-t | 显示时间戳 |
--tail | 仅列出最新N条容器日志 |
Docker 容器目录挂载
我们 在创建容器的时候,将宿主机的目录与容器内的目录进行映射,这样我们就可以实现宿主机和容器目录的双向数据自动同步。
我们可以通过容器目录挂载,能够轻松实现代码上传、配置修改、日志同步等需求。
语法 : docker run -i -t -v /宿主机目录:/容器目录 镜像id
多目录挂载 :docker run -i -t -v /宿主机目录:/容器目录 -v/宿主机目录2:/容器目录2 镜像id
注意 :如果同步多级目录,可能会出现权限不足的提示,只需要添加 --privileged=true 来解决挂载的目录没有权限的提示问题。
Docker容器启动的时候,如果要挂载宿主机的一个目录,可以用-v参数指定。
譬如我要启动一个centos容器,宿主机的/data/hData目录挂载到容器的/cData目录,可通过以下方式指定:
docker run -i -t -v /data/hData:/cData 470671670cac
注意点
- 容器目录不可以为相对路径
- 宿主机目录如果不存在,则会自动生成