在这里,我们将建立一个运行Apache Web服务器的简单Docker容器,然后从笔记本电脑的Web浏览器访问它。
我们将使用官方Ubuntu Docker镜像构建我们的Docker。
首先,通过运行来下拉ubuntu镜像:
$ docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu ae79f2514705: Pull complete 5ad56d5fc149: Pull complete 170e558760e8: Pull complete 395460e233f5: Pull complete 6f01dc62e444: Pull complete Digest: sha256:506e2d5852de1d7c90d538c5332bd3cc33b9cbd26f6ca653875899c505c82687 Status: Downloaded newer image for ubuntu:latest
然后确认我们现在有此镜像:
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 747cb2d60bbe 4 days ago 122MB
接下来,我们需要创建一个在交互模式下运行Bash终端的新容器。
我们需要将端口80公开为此容器,然后我们需要将我们的主机端口(例如8080)映射到新的暴露容器端口80。
$ docker run -p 8080:80 -it ubuntu /bin/bash root@f5842a8d0c33:/#
如果要退出容器,则只需执行以下操作:
root@f5842a8d0c33:/# exit exit
然而,这最终完全停止了容器:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
要启动此特定容器,我们首先找到它的ID:
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f5842a8d0c33 ubuntu "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago naughty_shockley
然后通过运行启动它:
$ docker start f5842a8d0c33 f5842a8d0c33
现在确认容器正在运行:
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f5842a8d0c33 ubuntu "/bin/bash" 11 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp naughty_shockley
请注意,这里还定义了端口转发。
现在让我们登录Docker容器:
$ docker exec -it f5842a8d0c33 /bin/bash root@f5842a8d0c33:/#
接下来我们做一个apt-get更新:
root@f5842a8d0c33:/# apt-get update Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB] Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB] Get:3 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [50.1 kB] Get:4 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB] Get:5 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB] Get:6 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB] Get:7 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [471 kB] Get:8 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.8 kB] Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [219 kB] Get:10 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [2930 B] Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB] Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB] Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB] Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB] Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [221 kB] Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [831 kB] Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.5 kB] Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [691 kB] Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [17.5 kB] Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [5176 B] Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [6354 B] Fetched 24.5 MB in 17s (1408 kB/s) Reading package lists... Done
现在安装apache2:
root@f5842a8d0c33:/# apt-get install apache2 -y
现在开始apache:
$ root@f5842a8d0c33:/# apachectl start AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
还启用服务:
root@f5842a8d0c33:/# systemctl enable apache2 apache2.service is not a native service, redirecting to systemd-sysv-install Executing /lib/systemd/systemd-sysv-install enable apache2
接下来转到笔记本电脑的Web浏览器并打开http//localhost:8080 /。
我们应该看到Ubuntu / Apache欢迎页面。
日期:2020-07-07 20:57:10 来源:oir作者:oir