默认分类

Docker 容器常用操作以及迁移

一:下载镜像操作

docker pull NAME:TAG

二:查看已经下载好的镜像

docker images

三:使用已经下载好的镜像创建container

docker create -p 80:80 --name typecho 80x86/typecho
-p 参数为把docker容器的80端口映射到虚拟机 --name 参数是给新建立的containe的名字 80x86/typecho为docker images镜像名

四:启动,停止,重启container操作

1:docker ps -a 查看所有创建的container,不加参数-a是查看状态为运行的container  
root@iZj6ce8gtknecijg0zq0axZ:~# docker ps -a
CONTAINER ID   IMAGE            COMMAND                  CREATED        STATUS                    PORTS                               NAMES
070e21d46822   80x86/typecho2   "/entrypoint.sh /app…"   14 hours ago   Up 14 hours (healthy)     0.0.0.0:80->80/tcp, :::80->80/tcp   typeecho
909dcf29bcaf   tt:latest        "/bin/ls /app/"          14 hours ago   Exited (0) 14 hours ago                                       brave_pascal
3a2dc997fcb8   tt:latest        "/bin/ls /app/u"         14 hours ago   Exited (1) 14 hours ago                                       agitated_nobel
108ec7d88258   tt:latest        "/bin/ls /app/usr"       14 hours ago   Exited (0) 14 hours ago                                       gifted_shaw
a40e2fde66b7   tt:latest        "/bin/ls"                14 hours ago   Exited (0) 14 hours ago                                       exciting_hodgkin
e6da4fa044b7   tt:latest        "/bin/ash"               14 hours ago   Exited (0) 14 hours ago                                       optimistic_easley
19af990997e2   tt:latest        "bin"                    14 hours ago   Created                                                       happy_lehmann
2:查看docker ps -a 查看需要运行的container id,使用docker start container id ,docker stop container id,docker restart container id,启动,停止,和重启container

五:docker exec 进入container

docker exec -it container id /bin/ash
-it 参数使用交互式tty操作,不然不能使用shell进入

六:docker inspect 查看镜像挂载的数据卷

oot@VM-0-8-ubuntu:~#  docker inspect --format=’{{.Mounts}}’ nostalgic_brahmagupta
’[{volume 8d3d003f6942018572c5ea1ab857bed54c897036e363116cfc3eda32f3d93c47 /var/lib/docker/volumes/8d3d003f6942018572c5ea1ab857bed54c897036e363116cfc3eda32f3d93c47/_data /data local  true }]’

七:运行中的container迁移

1:docker ps 查看CONTAINER ID

CONTAINER ID   IMAGE            COMMAND                  CREATED        STATUS                  PORTS                               NAMES
070e21d46822   80x86/typecho2   "/entrypoint.sh /app…"   14 hours ago   Up 14 hours (healthy)   0.0.0.0:80->80/tcp, :::80->80/tcp   typeecho

2:docker commit 根据CONTAINER ID创建新的Docker images

1:进入CONTAINER将/data数据打包到根目录,docker commit操作默认不会备份CONTAINER的数据目录,数据目录在/data/,docker镜像的用户数据一般通过软链接到/data目录


root@iZj6ce8gtknecijg0zq0axZ:~# docker exec -it 070e21d46822 /bin/ash
/app # ls -lh
total 8396
drwxr-sr-x    1 www      www         4.0K Jan 24 19:39 2023-01-24
-rw-r--r--    1 www      www        14.6K May  8  2019 LICENSE.txt
-rw-r--r--    1 www      www          318 May  8  2019 README.md
drwxr-xr-x    1 www      www         4.0K May  8  2019 admin
-rw-r--r--    1 www      www         2.1K May  8  2019 changelog.txt
lrwxrwxrwx    1 www      www           20 May 12  2020 config.inc.php -> /data/config.inc.php
-rw-r--r--    1 www      www         8.1M Jan 24 21:00 data.tar
-rw-r--r--    1 www      www        15.0K May  9  2019 favicon.ico
-rw-r--r--    1 www      www          721 May  8  2019 index.php
drwxr-xr-x    1 www      www         4.0K May  8  2019 install
-rw-r--r--    1 www      www        47.8K May  9  2019 install.php
drwxr-xr-x    1 www      www         4.0K May  8  2019 tools
lrwxrwxrwx    1 www      www            5 May 12  2020 usr -> /data
drwxr-xr-x    1 www      www         4.0K May  8  2019 var

2:打包/data目录到CONTAINER 根目录,这样操作docker commit操作后可以把/data/文件也打包
/ # cd /data
/data # ls
5ebabdc562747.db  config.inc.php    data.tar          log               plugins           themes            uploads
/data # tar czvf /data.tar *
5ebabdc562747.db
config.inc.php
tar: data.tar: file is the archive; skipping
log/
log/php7/
log/php7/fpm-php.www.err.log
log/nginx/
log/nginx/error.log
log/nginx/access.log
plugins/
plugins/Pio/
plugins/Pio/models/
plugins/Pio/models/pio/
plugins/Pio/models/pio/textures/
plugins/Pio/models/pio/textures/school-costume.png
plugins/Pio/models/pio/textures/default-costume.png
plugins/Pio/models/pio/textures/pajamas-costume.png
plugins/Pio/models/pio/model.json
plugins/Pio/models/pio/model.moc
plugins/Pio/models/pio/motions/
plugins/Pio/models/pio/motions/Breath Dere3.mtn
plugins/Pio/models/pio/motions/Touch Dere3.mtn
plugins/Pio/models/pio/motions/Touch Dere4.mtn
plugins/Pio/models/pio/motions/Touch1.mtn
plugins/Pio/models/pio/motions/Sleeping.mtn
plugins/Pio/models/pio/motions/Breath1.mtn
plugins/Pio/models/pio/motions/Sukebei1.mtn
plugins/Pio/models/pio/motions/Touch2.mtn
plugins/Pio/models/pio/motions/Touch Dere6.mtn
plugins/Pio/models/pio/motions/Sukebei3.mtn



3:docker commit -p 070e21d46822  80x86/typecho-bak
sha256:2fc4f45bb06b9b1baa02f9f326601223970453a40092121fa06375ba2e6dc76f
-p 参数为CONTAINER ID 80x86/typecho-bak是新建立的镜像,通过docker images 可以看到

3:docker images 查看刚刚生成的Images

root@iZj6ce8gtknecijg0zq0axZ:~# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
80x86/typecho2-bak   latest    2fc4f45bb06b   4 seconds ago   79.3MB
80x86/typecho2       latest    33a92425a442   14 hours ago    66.6MB

4:docker save 保存images为tar文件

docker save -o typecho-bak.tar 80x86/typecho2-bak
使用80x86/typecho2-bak -o 输出为typecho-bak.tar 
使用tar 压缩以减少网络传输时间 tar czvf typecho-bak.lz.tar typecho-bak.tar

5:使用http,ftp等工具将备份的typecho-bak.lz.tar下载到新服务器

6:docker load 加载刚刚备份的镜像

已经压缩的typecho.lz.tar 使用tar xvf typecho.lz.tar 先解压
root@iZj6ce8gtknecijg0zq0axZ:~# docker load  -i typecho.tar 
6023595b7077: Loading layer [==================================================>]  5.057MB/5.057MB
The image 80x86/typecho2:latest already exists, renaming the old one with ID sha256:33a92425a442665688283bbb95efa96403ba89cf1e1bc8d9f10de7d87607c782 to empty string
Loaded image: 80x86/typecho2:latest
root@iZj6ce8gtknecijg0zq0axZ:~# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
80x86/typecho2-bak   latest    2fc4f45bb06b   8 minutes ago   79.3MB
80x86/typecho2       latest    e61c1bcbda3c   16 hours ago    58.1MB

7:docker create创建container,docker ps -a 查看container id,docker start container id运行container,docker exec 进入container 解压data.tar到/data目录下,还原用户数据

root@iZj6ce8gtknecijg0zq0axZ:~# docker create -p 88:80 --name ty 80x86/typecho2-bak
ac720985a5ae023f5d9785a9649875687f16539aee695ad13b1e5fedc2290939
root@iZj6ce8gtknecijg0zq0axZ:~# docker exec -it 7edc3b095367 /bin/sh
/app # cd /
/ # ls
app            bin            data.tar       entrypoint.sh  home           lib            media          opt            root           sbin           sys            usr
app-init.sh    data           dev            etc            inject.sh      libexec        mnt            proc           run            srv            tmp            var
/ # cd data
/data # tar xvf /data.tar 
5ebabdc562747.db
config.inc.php
data.tar
log/
log/php7/
log/php7/fpm-php.www.err.log
log/nginx/
log/nginx/error.log
log/nginx/access.log
plugins/
plugins/Pio/
plugins/Pio/models/
plugins/Pio/models/pio/
plugins/Pio/models/pio/textures/

回复

This is just a placeholder img.