【Docker】容器使用和镜像制作
Docker安装
我是在Ubuntu 16上安装的docker,linux安装docker只需要一个命令:
sudo apt-get install docker.io
运行完后,可以在终端输入docker看到以下信息证明我们安装成功了
注:提示权限问题就添加sudo
docker
Usage: docker [OPTIONS] COMMAND [arg…]
docker daemon [ –help | … ]
docker [ –help | -v | –version ]A self-sufficient runtime for containers.
Options:
–config=~/.docker Location of client config files
-D, –debug Enable debug mode
-H, –host=[] Daemon socket(s) to connect to
-h, –help Print usage
-l, –log-level=info Set the logging level
–tls Use TLS; implied by –tlsverify
–tlscacert=~/.docker/ca.pem Trust certs signed only by this CA
–tlscert=~/.docker/cert.pem Path to TLS certificate file
–tlskey=~/.docker/key.pem Path to TLS key file
–tlsverify Use TLS and verify the remote
-v, –version Print version information and quitCommands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container’s changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container’s filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container’s filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on a container or image
kill Kill a running container
load Load an image from a tar archive or STDIN
login Register or log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
network Manage Docker networks
pause Pause all processes within a container
port List port mappings or a specific mapping for the CONTAINER
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart a container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save an image(s) to a tar archive
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop a running container
tag Tag an image into a repository
top Display the running processes of a container
unpause Unpause all processes within a container
update Update resources of one or more containers
version Show the Docker version information
volume Manage Docker volumes
wait Block until a container stops, then print its exit codeRun ‘docker COMMAND –help’ for more information on a command.
安装完以后,也可运行以下命令查看版本信息:
docker -v
docker version
docker info
当利用 docker run 来创建容器时,Docker 在后台运行的标准操作包括:
- 检查本地是否存在指定的镜像,不存在就从公有仓库下载
- 利用镜像创建并启动一个容器
- 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
- 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
- 从地址池配置一个 ip 地址给容器
- 执行用户指定的应用程序
- 执行完毕后容器被终止
运行容器
安装好之后,我们就可以来开始Docker之旅了,
我们现在的Docker还是一个”裸”Docker,上面没有容器,等一下,什么式容器?所谓容器就是Docker中用来运行应用的,Docker的容器很轻量级,但功能却强悍的很。也没有镜像。镜像?镜像简单理解就是容器的只读版本,用来方便存储与交流。此时,我们可以通过官方提供给我们的镜像来进行学习。比如我们想在Docker中运行一个Ubuntu系统,很简单,Docker中得pull命令是用来获取镜像的,执行下面的命令,就会从官方仓库里获取Ubuntu 14.04版本的系统:
docker pull ubuntu:14.04
images命令用来查看本机Docker中存在哪些镜像,运行 docker images
就会看到我们刚才获取的Ubuntu14.04系统:
现在,我们把刚刚的镜像运行起来,运行起来的镜像就叫做容器了,容器是可读写的,这样我们就可以在容器里做很多有意思的事情了。run 命令就是将镜像运行起来的,运行:
docker run -it ubuntu:14.04
仔细看,你会发现终端交互的用户名变掉了,说明我们进入到了容器的内部,效果如下:
现在我们所做的任何操作都是针对于目前容器而言的,不会影响到原来的系统,例如,我们在里面安装下nginx服务器,运行如下命令:
sudo apt-get install -y nginx
完成后执行nginx -v
就会发现我们已经将nginx安装成功:
将容器转化为镜像
在上一小节中,我们已经在容器里安装好了nginx,接下来我们希望将这个容器内容保存下来,这样我们下次就无需再次安装了。这就是Docker中将容器转换为镜像的技术。
如果您还在刚刚的安装了nginx的终端里,执行exit退出此终端,回到系统本身的终端:
ps
命令可以查看我们当前都运行了哪些容器,加上-a
参数后就表示运行过哪些容器,因为我们刚刚已经退出了安装nginx的容器,因此我现在想查看它的话,需要使用-a
参数,执行如下命令:
docker ps -a
此时,就会显示出我们刚刚运行的容器,并且Docker会很贴心的随机给每个容器都起个Names方便标示。效果如下:
commit
命令用来将容器转化为镜像,运行下面的命令,我们可以讲刚刚的容器转换为镜像:
sudo docker commit -m “Added nginx from ubuntu14.04” -a “saymagic” 79c761f627f3 saymagic/ubuntu-nginx:v1
其中,-m
参数用来来指定提交的说明信息;-a
可以指定用户信息的;79c761f627f3代表的时容器的id;saymagic/ubuntu-nginx:v1指定目标镜像的用户名、仓库名和 tag 信息。创建成功后会返回这个镜像的 ID 信息。注意的是,你一定要将saymagic改为你自己的用户名。因为下文还会用到此用户名。
这是我们再次使用docker images
命令就会发现此时多出了一个我们刚刚创建的镜像:
此时,如果运行docker run -it saymagic/ubuntu-nginx:v1
就会是一个已经安装了nginx的容器:
存储镜像
我们刚刚已经创建了自己的第一个镜像,尽管它很简单,但这已经非常棒了,现在,我们希望它能够被更多的人使用到,此时,我们就需要将这个镜像上传到镜像仓库,Docker的官方Docker Hub应该是目前最大的Docker镜像中心,所以,我们就将我们的镜像上传到Docker Hub。
首先,我们需要成为Docker Hub的用户,前往https://hub.docker.com/进行注册。需要注意的是,为了方便下面的操作,你需要将你的用户名设为和我刚刚在上文提到的自定义用户名相同,例如我的刚刚将镜像的名字命名为是saymagic/ubuntu-nginx:v2,所以我的用户名为saymagic、注册完成后记住用户名、密码、邮箱。
login默认是用来登陆Docker Hub的,因此,输入如下命令来尝试登陆Docker Hub:
docker login
此时,就会输出交互,让我们输入Username、Password、Email,成功输入我们刚才注册的信息后就会返回Login Success提示:
运行命令:
docker push saymagic/ubuntu-nginx:v1
这就是我们为什么将刚刚的镜像命名为saymagic/ubuntu-nginx:v1
的原因,如果你上面步骤都操作正确的正确的话,是会得到下面的内容:
此时,不出意外的话,我们的镜像已经被上传到Docker Hub上面了,去Docker Hub上面看看:
果然,我们在Docker Hub上有了我们的第一个镜像,此时,其它的用户就可以通过命令docker pull saymagic/ubuntu-nginx来直接获取一个安装了nginx的ubuntu系统了。不信?那就自己实践一下吧!