Skip to main content

Docker study (3)- Docker container and image

·3 mins
Docker
Table of Contents

Docker 容器相关操作
#

#docker container run -d nginx
#docker container run -ti ubuntu  <<< 安全退出 Ctrl+P && Ctrl+Q
#docker container attach <CONTAINER ID>
#docker container exec -it <CONTAINER ID>
#docker ps -l //查看最新创建的容器
#docker ps -q //查看容器ID
#docker ps -a //查看所有容器, 包括已经停止的
#docker logs <CONTAINER ID>    //查看log
#docker container stop  //发送TERN信号等待10s后, 如果容器没停止发送KILL信号
#docker container kill  //发送KILL信号,容器立刻停止
#docker container rm    //删除已停止的容器, --force 强制删除
#docker container rm $(docker ps -aq) --force

#docker container inspect <CONTAINER ID>  //会打印出容器的详细信息
#docker container diff <CONTAINER ID>  //查看容器

#docker run -it busybox

//容器之间共享namespace

#docker container run –d --name infra --ipc=shareable google/pause
#docker container run –tid --name busybox01 \
--pid=container:infra \
--ipc=container:infra \
--net=container:infra busybox
#docker container run –tid --name busybox02 \
--pid=container:infra \
--ipc=container:infra \
--net=container:infra busybox

查看 namespace

//查看namespace
[root@localhost ~]# docker container top 6a
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                85420               85403               0                   12:07               pts/0               00:00:00            sh
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# lsns -p 85420
        NS TYPE  NPROCS   PID USER COMMAND
4026531837 user     160     1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 22
4026532680 ipc        3 85208 root /pause
4026532681 pid        3 85208 root /pause
4026532683 net        3 85208 root /pause
4026532753 mnt        1 85420 root sh
4026532754 uts        1 85420 root sh
[root@localhost ~]#

Docker 文件系统

//docker 文件系统:

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  4.8G   46G  10% /
devtmpfs                 7.8G     0  7.8G   0% /dev
tmpfs                    7.8G     0  7.8G   0% /dev/shm
tmpfs                    7.8G   20M  7.8G   1% /run
tmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/sda1               1014M  152M  863M  15% /boot
/dev/mapper/centos-home  142G   33M  142G   1% /home
overlay                   50G  4.8G   46G  10% /var/lib/docker/overlay2/7fd4fe6c93f7996c9751098d0f7288bd911f4d5cdd474f09a3f45e94f48ec67a/merged
overlay                   50G  4.8G   46G  10% /var/lib/docker/overlay2/6ef794bb4b4961c4006f0a239e68feab742609291777b08631c4665d851195c4/merged
overlay                   50G  4.8G   46G  10% /var/lib/docker/overlay2/4fadac2b7b13aff99ffed42990e293270a7aec57f80fe42d4b14736fec84bd4b/merged
overlay                   50G  4.8G   46G  10% /var/lib/docker/overlay2/27e3827db6450f1a6e98f9b735ab8addfa3007147385ab4e8a12f787d35c9253/merged
tmpfs                    1.6G     0  1.6G   0% /run/user/0
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cd /var/lib/docker/overlay2/27e3827db6450f1a6e98f9b735ab8addfa3007147385ab4e8a12f787d35c9253/merged
[root@localhost merged]# ls
bin  dev  entrypoint.sh  etc  home  lib  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@localhost merged]# pwd
/var/lib/docker/overlay2/27e3827db6450f1a6e98f9b735ab8addfa3007147385ab4e8a12f787d35c9253/merged
[root@localhost merged]# exot
-bash: exot: command not found
[root@localhost merged]#

Docker 镜像相关操作
#

#docker search <NAME>
#docker image pull
	#docker image run
#docker image ls
#docker image history
	# docker image history 1e4467b07108 --no-trunc
#docker image save
	#docker image save nginx –o /mnt/nginx.tar.gz
	#docker image save nginx > /mnt/nginx.tar.gz
#docker image load
	#docker image load –i nginx.tar.gz
	#docker image load < nginx.tar.gz
#docker image rm
#docker image tag

Docker container commit 手动构建 Image

  • Docker container run 运行容器
  • 修改容器
  • Docker commit 将容器保存为新的镜像
[root@localhost merged]# docker container commit e13a9d6dfded myubuntu
sha256:9de078569932b64a4b07879692fd13fed2b4bd0eaace4f15befa1ee67180909c
[root@localhost merged]#
[root@localhost merged]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myubuntu            latest              9de078569932        10 seconds ago      97.8MB

镜像的 tag

  • docker image tag 命令用于给镜像打标签
  • 标签一般标记 image 版本,开发者等相关的信息
  • 一个镜像可以有多个标签,它们最终指向同一个 image id
  • 标签的特殊用途 – “物流信息”
  • docker image tag aa20 quay.io/alice/clock:v1
  • docker image push quay.io/alice/clock:v1
  • quay.io 是 registry 的地址,在使用 docker hub 以外的 registry 时,必须加上
  • 这里面的 alice,称为 namespace,一般是和用户名关联
  • clock 是 alice namespace 下的其中一个仓库,一个仓库中一般含有一个应用的多个版本
  • v1 标记仓库中的某一个版本的应用的镜像
  • Namespace 可以为空,我们称这样的仓库为顶层仓库,一般预留给像 centos, ubuntu 等官方镜像使用

推送镜像到 Docker hub

[root@localhost merged]# docker container commit 23950da8e736 testbusybox
sha256:aaf33ea2c57b7cceeb52524e877834f3ec0bdbd8eb19fb4566782af4adb1257d
[root@localhost merged]#

[root@localhost merged]# docker image tag testbusybox xuxing123/docker_test:v1
[root@localhost merged]#
[root@localhost merged]# 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: xuxing123
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
[root@localhost merged]# docker image push xuxing123/docker_test:v1
The push refers to repository [docker.io/xuxing123/docker_test]
4f42a2914972: Pushed
514c3a3e64d4: Mounted from library/busybox
v1: digest: sha256:80abd9df785cc13a34a10148ba1a45389b215431110ceb2b75e43049d5d9bc8d size: 734
[root@localhost merged]# docker image tag --help

Usage:	docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[root@localhost merged]#



Comments