Docker还允许我们在隔离的容器中安装任何操作系统。在下面的例子中,将演示如何在Docker中安装Ubuntu系统。
1.创建一个目录用来组织安装的操作系统文件。
代码语言:javascript复制mkdir -p ~/Downloads/code/docker/ubuntu-in-docker
1.进入工作目录。
代码语言:javascript复制cd ~/Downloads/code/docker/ubuntu-in-docker
1.创建一个 Dockerfile 文件。
代码语言:javascript复制FROM ubuntu
1.创建Docker映像
代码语言:javascript复制sudo docker build -t ubuntu-in-docker .
[ ] Building 29.9s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 55B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:latest 16.1s
=> [1/1] FROM docker.io/library/ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 13.7s
=> => resolve docker.io/library/ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 0.0s
=> => sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1 1.46kB / 1.46kB 0.0s
=> => sha256:7b1a6ab2e44dbac178598dabe7cff59bd67233dba0b27e4fbd1f9d4b3c877a54 28.57MB / 28.57MB 12.0s
=> => sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 1.42kB / 1.42kB 0.0s
=> => sha256:7cc0576c7c0ec2384de5cbf245f41567e922aab1b075f3e8ad565f508032df17 529B / 529B 0.0s
=> => extracting sha256:7b1a6ab2e44dbac178598dabe7cff59bd67233dba0b27e4fbd1f9d4b3c877a54 1.5s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:657922eb2d64b0a34fe7339f8b48afb9f2f44635d7d6eaa92af69591d29b3330 0.0s
=> => naming to docker.io/library/ubuntu-in-docker
1.运行Docker映像
现在,创建这个Ubuntu系统的Docker映像,以下命令用于创建Docker映像。
代码语言:javascript复制sudo docker run -td ubuntu-in-docker
e4da12002e66485319e0d1541b821979be9e4b7fd61e97a6fdcc657f40089e46
1.查看当前运行的 Docker映像
代码语言:javascript复制sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4da12002e66 ubuntu-in-docker "bash" 35 seconds ago Up 34 seconds admiring_ishizaka
d2e51129a2ea python-app "python /Users/micha…" 2 days ago Exited (0) 2 days ago pensive_tereshkova
a791526f3ed6 logstash:7.6.2 "/usr/local/bin/dock…" 12 months ago Created 5044/tcp, 0.0.0.0:4560-4563->4560-4563/tcp, 9600/tcp logstash
cc8c6fa62339 elasticsearch:7.6.2 "/usr/local/bin/dock…" 12 months ago Exited (255) 11 months ago 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch
398815981b4b rabbitmq:3.7.15 "docker-entrypoint.s…" 12 months ago Exited (255) 11 months ago 4369/tcp, 0.0.0.0:5672->5672/tcp, 5671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq
49cac9c723c4 nginx:1.10 "nginx -g 'daemon of…" 12 months ago Exited (1) 12 months ago nginx
4a0f335e94d8 redis:5 "docker-entrypoint.s…" 12 months ago Exited (0) 12 months ago redis
908bcd97c28e mysql:5.7 "docker-entrypoint.s…" 12 months ago Exited (0) 11 months ago
1.进入Docker容器
可以进入运行容器,并可以检查容器内的文件和应用程序。
代码语言:javascript复制sudo docker exec -it e4da12002e66 bash
root@e4da12002e66:/#
1.执行Ubuntu命令
代码语言:javascript复制apt-get update
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:2 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [39.4 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [355 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.8 kB]
Get:6 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [170 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
0% [7 InRelease 55.6 kB/102 kB 54%] [6 Packages 129 kB/170 kB 76%]
....