首先查看下Ubuntu的版本
lsb_release -a
我的版本信息如下
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.6 LTS
Release: 18.04
Codename: bionic
然后按照如下步骤安装即可
首先,更新系统软件库索引。
sudo apt update
Docker需要使用curl、apt-transport-https、ca-certificates、software-properties-common
。
安装这些软件包:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
sudo systemctl start docker
sudo systemctl enable docker
验证Docker是否成功安装并运行:
sudo docker --version
sudo usermod -aG docker ${USER}
这一步需要重启系统才能生效,不然会显示没有权限
我的服务器显示了如下内容
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:4f53e2564790c8e7856ec08e384732aa38dc43c52f02952483e3f003afbf23db
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
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
docker run -it ubuntu bash
这将会启动一个基于Ubuntu的容器,并给一个bash shell,可以在容器内部进行操作。输入exit可以退出。
sudo docker run -d -p 80:80 --name mynginx nginx
可以通过Web浏览器访问
使用curl命令
/$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
579ee93b319f nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
/$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
579ee93b319f nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
f260a7d0df2a ubuntu "bash" 32 minutes ago Exited (0) 31 minutes ago eager_roentgen
5b1d541d529d hello-world "/hello" 33 minutes ago Exited (0) 33 minutes ago frosty_shaw
上面两条命令会列出容器的ID、创建时间、状态、端口映射等信息,下面这个可以只查看容器名称
sudo docker ps -a --format "{{.Names}}"
停止容器:
使用docker stop
命令停止容器。
sudo docker stop mynginx
删除容器:
如果只是想暂时停止容器,那么第一步就足够了
sudo docker rm mynginx
如果想运行一个Nginx容器,需要重新使用docker run
命令。但由于您之前已经下载了Nginx镜像,所以这次启动容器时不再需要下载镜像。