> 以下所有的命令都是基于 Windows 下 WSL 创建的 Ubuntu 18.04 ,如果你在尝试下面的命令中出错的话可以自行搜索解决 ,也欢迎留言讨论交流
Basic command explanation
Enter docker directly on the command line to see the following information. At present, we only pay attention to some commonly used commands.
$ docker run # 启动一个实例
$ docker stop # 停止
$ docker restart # 重启
$ docker ps # 查看所有的实例
$ docker images # 查看已经下载安装的 images
At present, we only need to understand these commands. Let's try to run the first example.
A simple example
First we need to change the mirror source, otherwise the download speed will be very slow
Domestic accelerated service
- NetEase: https://hub-mirror.c.163.com/
- Alibaba Cloud: https://<your ID>.mirror.aliyuncs.com
- Qiniu Cloud Accelerator: https://reg-mirror.qiniu.com
After configuring an accelerator address, if you find that the mirror cannot be pulled, please switch to another accelerator address. Major domestic cloud service providers have provided Docker image acceleration services. It is recommended to select the corresponding image acceleration service according to the cloud platform running Docker.
Since we just registered an Alibaba Cloud account in the previous article, we choose Alibaba Cloud here
Log in to the official website of Alibaba Cloud that we registered last time , and copy the website he provided in Mirror Tools> Mirror Accelerator
Right-click the Docker icon in the lower right corner> setting> Docker Engine, and copy the URL just now to registry-mirrors
Other acceleration methods can refer to this website
Run HelloWorld
We create a new Ubuntu virtual machine and let this virtual machine run in one sentence
$ docker run ubuntu:15.10 /bin/echo "Hello world"
At this time, "Hello world" will be output on the screen
We enter the Ubuntu virtual machine and execute some Linux commands
$ docker run -i -t ubuntu:15.10 /bin/bash
-
-t: Specify a pseudo terminal or terminal in the new container.
-
-i: Allows you to interact with the standard input (STDIN) in the container
At this time we have entered the newly created virtual machine and can execute some Linux commands such as
root@3a3051447e22:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@3a3051447e22:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=15.10
DISTRIB_CODENAME=wily
DISTRIB_DESCRIPTION="Ubuntu 15.10"
Then we can directly ctrl + D or enter exit to exit the virtual machine
Run a virtual machine in the background
The above method will not leave a process in the background after you exit, which is equivalent to running a program and exiting after completion
In most scenarios, we want the docker service to run in the background. We can specify the running mode of the container with **-d **.
$ docker run -itd --name ubuntu-test ubuntu /bin/bash
At this point, we want to enter the virtual machine in two ways
$ docker exec -it 243c32535da7 /bin/bash
or
$ docker attach 1e560fca3906
If you exit the virtual machine after using attach, the virtual machine will not continue to run in the background
After using exec, the virtual machine will continue to run in the background
Some other commands
Import and Export
$ docker export 1e560fca3906 > ubuntu.tar
$ cat docker/ubuntu.tar | docker import - test/ubuntu:v1
export export
import import
delete
$ docker rm -f dockerID