欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Docker下载安装运行Nginx服务

时间:2023-07-07
文章目录

下载安装在 Docker 中使用镜像运行 Docker 容器Docker运行Nginx

1、获取Nginx镜像2、运行Nginx容器 下载安装

https://docs.docker.com/engine/install/ubuntu/

检查 Docker 是否正在运行:

sudo systemctl status docker

类似以下内容,说明该服务处于活动状态并且正在运行:

root@VM-16-5-ubuntu:/home/ubuntu# sudo systemctl status docker● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2022-02-08 00:03:36 CST; 10h ago Docs: https://docs.docker.com Main PID: 15585 (dockerd) Tasks: 9 CGroup: /system.slice/docker.service └─15585 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

默认情况下,docker命令只能由 root 用户或由 docker 组中的用户运行,docker 组用户是在Docker安装过程中自动创建的。

在 Docker 中使用镜像

Docker 容器(containers)是从 Docker 镜像生成出来的。默认情况下,Docker 从Docker Hub下载这些镜像,Docker 公司在运营这个Docker Hub。

要检查是否可以访问 Docker Hub 和从这个网站下载镜像,请输入:

docker run hello-world

如果你得到以下结果,说明你的机器访问 Docker hub 一切顺畅:

$ docker run hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world0e03bdcc26d7: Pull complete Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9Status: Downloaded newer image for hello-world:latestHello 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.

执行此命令时,Docker 首先在本地查找hello-world,如没有,它会从 Docker Hub(默认版本库)下载了该镜像。下载镜像后,Docker 会根据镜像创建一个容器,并执行该容器中的应用程序。

您可以通过将docker命令与search子命令配合使用来搜索 Docker Hub 上可用的镜像。

例如,要搜索 Ubuntu 的镜像,请输入:

docker search ubuntu

此命令会在 Docker Hub 上搜索并返回名称与搜索字符串匹配的所有镜像列表。

执行后显示的结果如下:

NAME DEscriptION STARS OFFICIAL AUTOMATEDubuntu Ubuntu is a Debian-based Linux operating sys… 11102 [OK] dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 443 [OK]rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 245 [OK]consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 220 [OK]ubuntu-upstart Upstart is an event-based replacement for th… 110 [OK] neurodebian NeuroDebian provides neuroscience research s… 68 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]ubuntu-debootstrap debootstrap --variant=minbase --components=m… 44 [OK] nuagebec/ubuntu Simple always updated Ubuntu docker images w… 24 [OK]i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 21 1and1internet/ubuntu-16-apache-php-5.6 ubuntu-16-apache-php-5.6 14 [OK]1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK]1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 11 [OK]1and1internet/ubuntu-16-nginx-php-5.6 ubuntu-16-nginx-php-5.6 8 [OK]1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 7 [OK]1and1internet/ubuntu-16-apache-php-7.1 ubuntu-16-apache-php-7.1 6 [OK]darksheer/ubuntu base Ubuntu Image -- Updated hourly 5 [OK]1and1internet/ubuntu-16-nginx-php-7.0 ubuntu-16-nginx-php-7.0 4 [OK]pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 4 pivotaldata/ubuntu16.04-build Ubuntu 16.04 image for GPDB compilation 2 1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]smartentry/ubuntu ubuntu with smartentry 1 [OK]1and1internet/ubuntu-16-php-7.1 ubuntu-16-php-7.1 1 [OK]pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 1 pivotaldata/ubuntu16.04-test Ubuntu 16.04 image for GPDB testing 0

“ OFFICIAL” 列(即官方)对应的镜像,就是出自官方的镜像了,大家可放心使用。

接下来,我们可以用pull子命令,将你需要的镜像下载到计算机。

本教程我们使用ubuntu的官方镜像。

docker pull ubuntu

你将看到以下执行结果:

Using default tag: latestlatest: Pulling from library/ubuntu692c352adcf2: Pull complete 97058a342707: Pull complete 2821b8e766f4: Pull complete 4e643cc37772: Pull complete Digest: sha256:55cd38b70425947db71112eb5dddfa3aa3e3ce307754a3df2269069d2278ce47Status: Downloaded newer image for ubuntu:latestdocker.io/library/ubuntu:latest

下载镜像后,可以用run来运行镜像。

从上面这个案例我们可以发现,使用run来执行本地不存在的hello-world镜像时, docker 发现本地没有这个镜像,会直接在 Docker hub 上查找并下载对应的镜像。

要查看已下载到计算机的镜像:

docker images

输出结果如下:

root@VM-16-5-ubuntu:/home/ubuntu# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEubuntu latest 54c9d81cbb44 6 days ago 72.8MBhello-world latest feb5d9fea6a5 4 months ago 13.3kB

当镜像执行时,它会生成一个容器,我们在容器中添加所需的软件后,可以把这个容器再次打包成新镜像。

运行 Docker 容器

在上一步的例子中,我们执行了hello-world并学习了如何查看镜像和容器。但其实容器并不仅仅是这样,它可比这有用的多。接下来我们来看看容器能做些什么。

作为示例,让我们用 Ubuntu 最新镜像来运行。使用-i -t 这两个参数,可以让你通过 shell 来管理他们。

docker run -it ubuntu

执行命令后,提示符会变为你正在使用镜像的容器id:

root@7896ef8f403f:/#

Docker运行Nginx 1、获取Nginx镜像

要运行容器,首先需要有相应的镜像,使用下面的命令拉取NGINX镜像:

docker pull nginx

如图所示

2、运行Nginx容器

获取Nginx镜像之后,我们就可以根据镜像来运行容器

docker run --name=nginx -d -p 4030:80 nginx

上面命令的解释如下:

–name:设置容器的名称。-d:表示在后台运行容器。-p:指定端口映射。4030是宿主机的端口,80是Nginx容器内部的端口。nginx:表示根据nginx镜像运行容器。

如图所示

然后在浏览器里面访问:

出现上面的截图,就说明Nginx容器运行成功。

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。