一般来说,我们所说的“构建镜像”并不是真正的从零构建一个全新的镜像,而是修改一个已有的基础镜像(如ubuntu)。
创建Docker Hub账号
(1) 如果要存储和分发我们构建的新镜像,那么可以将镜像发布到 Docker Hub 或者自己私有的Registry上面。所以需要先在 Docker Hub 创建一个账号。
(2)创建账号后,使用 docker login
命令就可以登录到Docker Hub了。
# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: sweeneys
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
如上所示,登录的个人认证信息被保存到 ~/.docker/config.json
使用commit命令创建镜像
1、创建容器
创建一个基于ubuntu镜像的容器,后续会以这个容器为基础进行修改,从而形成新的镜像。
# docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
7ddbc47eeb70: Pull complete
c1bbdc448b72: Pull complete
8c3b70e39044: Pull complete
45d437916d57: Pull complete
Digest: sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d
Status: Downloaded newer image for ubuntu:latest
2、添加Apache软件包
Apache软件包在ubuntu官方源上的名字为apache2。
root@51753edb3a70:/# apt-get update
root@51753edb3a70:/# ap