Docker 常用操作

Docker Commands

常用操作

搜索docker仓库

docker search [OPTIONS] TERM

下载镜像

docker pull [OPTIONS] IMAGE[:TAG]

将本地的镜像上传到镜像仓库,要先登陆到镜像仓库

docker push [OPTIONS] NAME[:TAG]

登陆/登出Docker镜像仓库

docker login/logout [OPTIONS] [SERVER]

启动镜像

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

启动/停止/重启容器

docker start/stop/restart CONTAINER

在运行的容器中执行命令 【-d,-i,-t

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

查看容器中运行的进程信息,支持ps命令参数

docker top [OPTIONS] CONTAINER [ps OPTIONS]

将指定的容器创建为新的镜像,若镜像名和版本重复则覆盖原有镜像 【-m, -a, tag缺省】

docker commit -m "change sth" -a "somebody" CONTAINER IMAGE:TAG

查看在运行的容器

docker ps

查看被修改的容器

docker ps -l

查看所有容器

docker ps -a

查看容器配置和状态信息

docker inspect CONTAINER

退出容器交互界面并关闭容器

ctrl + dexit

退出容器交互界面但不关闭容器

ctrl + p + q

删除已退出的容器

docker rm $(docker ps -qf status=exited)

删除所有容器。所有未运行的容器都被删除,正在运行的会删除失败,达到删除不用容器的目的。 (危险操作)

docker container prune

示例

redis

下载redis镜像
docker pull redis
在后台启动redis镜像
[root@VM_32_161_centos ~]# docker run -d -p 6379:6379 docker.io/redis:latest
ecdedee43022066baf04592b7749397c4bb383f92ac0080efe40f7a978493858
查看应用配置和状态信息
[root@VM_32_161_centos ~]# docker inspect ecde
[
    {
        "Id": "ecdedee43022066baf04592b7749397c4bb383f92ac0080efe40f7a978493858",
        "Created": "2019-04-17T06:33:57.453089857Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "redis-server"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 10326,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-04-17T06:33:57.664264501Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:a55fbf438dfd878424c402e365ef3d80c634f07d0f5832193880ee1b95626e4e",
        "ResolvConfPath": "/var/lib/docker/containers/ecdedee43022066baf04592b7749397c4bb383f92ac0080efe40f7a9784  93858/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/ecdedee43022066baf04592b7749397c4bb383f92ac0080efe40f7a978493  858/hostname",
        "HostsPath": "/var/lib/docker/containers/ecdedee43022066baf04592b7749397c4bb383f92ac0080efe40f7a978493858  /hosts",
        "LogPath": "/var/lib/docker/containers/ecdedee43022066baf04592b7749397c4bb383f92ac0080efe40f7a978493858/e  cdedee43022066baf04592b7749397c4bb383f92ac0080efe40f7a978493858-json.log",
        "Name": "/elegant_bardeen",
        "RestartCount": 0,
        "Driver": "overlay2",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
......

centos

下载centos镜像

docker pull centos

启动centos镜像,并进入模拟交互界面

docker run -it centos

后台启动centos并打印字符串,返回container_id(系统镜像的缺省命令是 bash)

docker run -d centos /bin/bash -c "echo hello world"

docker run -d centos echo "hello world"

启动centos镜像,并调用python解释器执行容器内部代码

docker run -d centos python /root/app.py

查看指定容器的输出

docker logs container_id

保存修改的内容,将指定容器创建为新的镜像,若镜像名重复则覆盖

docker commit -m "change sth" -a "somebody" container_id image_name


python

下载python指定版本的镜像

docker pull python:3.6.8

启动python镜像,将宿主机的/root/src数据卷挂载到容器的/usr/local/src,并使用python命令执行容器中的/usr/local/src/app.py

docker run -v /root/src:/usr/local/src python:3.6.8 python /usr/local/src/app.py

启动python镜像,执行 pip list 命令

docker run python:3.6.8 pip list


制作简单项目镜像

编写Dockerfile
# 引用 python:3.6.8镜像,直接安装在生成的新镜像内
FROM python:3.6.8

MAINTAINER cn.Dixon
MAINTAINER [email protected]

# 工作目录
WORKDIR /root/

# 将宿主机当前路径下的.ssh文件夹复制到容器内部/root/.ssh
COPY .ssh/ /root/.ssh/

# git克隆项目
RUN apt-get install git ; git clone [email protected]:dmp/xxx.git

# 镜像启动命令,通常为项目启动命令
CMD /bin/sh
创建镜像
[root@VM_32_161_centos ~]# docker build -f Dockerfile -t IMAGE:TAG .
Sending build context to Docker daemon 343.6 kB
Step 1/7 : FROM python:3.6.8
 ---> d2f1c567199a
Step 2/7 : MAINTAINER cn.Dixon
 ---> Using cache
 ---> fcd79cf4fd4b
Step 3/7 : MAINTAINER [email protected]
 ---> Using cache
 ---> 3c2ed9f2186e
Step 4/7 : WORKDIR /root/
 ---> Using cache
 ---> 8a73dcd920f3
Step 5/7 : COPY .ssh/ /root/.ssh/
 ---> 9111eb8786e8
Removing intermediate container 7363dd035ee1
Step 6/7 : RUN apt-get install git ; git clone [email protected]:dmp/xxx.git
 ---> Running in 603e84afaf74

Reading package lists...
Building dependency tree...
Reading state information...
git is already the newest version (1:2.11.0-3+deb9u4).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Cloning into 'xxx'...
 ---> 8de54af9e744
Removing intermediate container 603e84afaf74
Step 7/7 : CMD /bin/sh
 ---> Running in b4ffc5f157e8
 ---> 5786d0622e6a
Removing intermediate container b4ffc5f157e8
Successfully built 5786d0622e6a
使用镜像
# 调用原 Dockerfile 中的 CMD 命令
docker run IMAGE[:TAG]

# 调用容器内 python 解释器执行容器内代码
docker run IMAGE[:TAG] python xxx/xxx.py

# 指定工作目录,更新 git 项目
docker run -w /root/xxx IMAGE[:TAG] git pull

你可能感兴趣的:(Docker,Docker,常用操作)