环境:Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-97-generic x86_64)
- 查看分发版
1 | lsb_release -cs |
更新源
1
sudo apt-get update
安装软件包
1
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
添加 Docker 的官方 GPG 密钥
1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
验证密钥指纹
1
2
3
4
5root@bogon:~# sudo apt-key fingerprint 0EBFCD88
pub 4096R/0EBFCD88 2017-02-22
Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid Docker Release (CE deb) <docker@docker.com>
sub 4096R/F273FCD8 2017-02-22写入软件源信息,用阿里云的国内快一些。
1
2
3
4
5
6
7
8aliyun
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
官方
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
(lsb_release -cs) \
stable"安装最新版社区版
1
sudo apt-get install docker-ce
查看版本
1
sudo docker version
结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18Client:
Version: 18.09.5
API version: 1.39
Go version: go1.10.8
Git commit: e8ff056
Built: Thu Apr 11 04:44:24 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.5
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: e8ff056
Built: Thu Apr 11 04:10:53 2019
OS/Arch: linux/amd64
Experimental: false验证是否正确安装了 Docker CE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:5f179596a7335398b805f036f7e8561b6f0e32cd30a32f5e19d17a3cda6cc33d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
docker run -it ubuntu bash
---------------------------------------------------------------------
docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
f476d66f5408: Pull complete
8882c27f669e: Pull complete
d9af21273955: Pull complete
f5029279ec12: Pull complete
Digest: sha256:70fc21e832af32eeec9b0161a805c08f6dddf64d341748379de9a527c01b6ca1
Status: Downloaded newer image for ubuntu:latest
root@64cf93148c4b:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@64cf93148c4b:/#
这里是已经进入到 docker 镜像 ubuntu 的bash环境里面了常用命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53拉取镜像,TAG一般是指版本,可以不指定,默认是latest
sudo docker pull REPOSITORY:TAG
root@server:~# sudo docker pull hello-world
root@server:~# sudo docker pull openjdk:8
查看本地镜像
root@server:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
openjdk 8 bec43387959a 7 days ago 625MB
ubuntu latest d131e0fa2585 2 weeks ago 102MB
hello-world latest fce289e99eb9 4 months ago 1.84kB
删除本地镜像
sudo docker rmi IMAGE_ID
root@server:~# sudo docker rmi fce28
前台运行
sudo docker run NAME
root@server:~# sudo docker run hello-world
查看运行中的容器
root@server:~# sudo docker ps -a
CONTAINER ID IMAGE COMMAND
408bd2d9fe7c hello-world "/hello"
删除容器
sudo docker rm CONTAINER_ID
root@server:~# sudo docker rm 408b
后台运行
root@server:~# sudo docker run -d ubuntu
51028c9c4d404a0b0382dd6e91b0a985980832c537ed0d02ffe932a37cb101c6
关闭(后台运行,前台的不需要stop)
sudo docker stop CONTAINER_ID
root@server:~# sudo docker stop 51028c
51028c
守护进程运行
root@server:~# sudo docker run --restart=always hello-world
端口映射
root@server:~# sudo docker run -d -p 8080:8080 hello-world
进入容器的bash
sudo docker exec -it CONTAINER_ID bash
root@server:~# sudo docker exec -it d131e0fa2585 bash搜索镜像
1
2
3
4
5root@server:~# docker search nginx
NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx. 11403 [OK]
tobi312/rpi-nginx NGINX on Raspberry Pi / ARM 26
bitnami/nginx Bitnami nginx Docker Image 66其中,
STARS
:收藏数,表示该镜像的受关注程度
OFFICIAL
:是否官方创建维护镜像加速Docker 中国官方镜像加速
1
2
3
4
5
6vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker