docker基础命令

From: https://docker-curriculum.com/

 

一、docker常见的命令

  1. 从远程Pull image:

$ docker pull busybox

 

  1. List images:

$ docker images

 

  1. Docker Run:

#  When you call run, the Docker client finds the image (busybox in this case), loads up the container and then runs a command in that container. When we run docker run busybox, we didn't provide a command, so the container booted up, ran an empty command and then exited.

$ docker run busybox

$ docker run busybox echo "hello from busybox"

# -it flags attaches us to an interactive tty in the container

$ docker run -it busybox sh

$ docker run --help
 

# -d will detach our terminal ,  -P will publish all exposed ports to random ports

$ docker run -d -P --name static-site prakhar1989/static-site

$  docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest

 

  1. List all containers that are currently running

$ docker ps

#和 docker ps一样

$ docker container ls

# list all containers including exited status.

$ docker ps -a

 

  1. Stop 容器:

# 停止某个container

$ docker container stop es 

$ docker stop [containerID or name]

 

  1. Docker Start容器

当container退出后,状态为Exited, 这时可以重启这个container。如果docker rm 这个container,则container中所作的变更和数据都没了,所以如果打算继续用这个container中的数据,就不要rm container然后重新run,而要重新Start这个容器。如:

$ docker ps -a

$ docker run [container ID or Name]


7.  Delete Containers 容器

虽然container退出了,但是还是可能占用磁盘空间的。

$ docker rm 305297d7a235 ff0a5c3750b9

$ docker rm $(docker ps -a -q -f status=exited)

$ docker container prune

 

  1. Delete Images 镜像:

$ docker rmi [OPTIONS] IMAGE [IMAGE...]

$ docker rmi -f prakhar1989/static-site

-f :强制删除, 即便被container引用了

 

  1. Docker pull http不安全镜像

默认docker会用Https连接镜像的,但是如果是自己私下搭建的http仓库,就需要在docker中配置下,可以配置:daemon.json 文件,或打开Docker Desktop,点击setting齿轮,选择Docker Engine,在配置中添加insecure-registries,如:"insecure-registries": ["10.0.2.86"]

另外如果是私有的,还需要先登录。先在terminal命令行窗口执行:docker login [harbor地址], 输入用户名密码。

 

  10.  Docker image 导出

     $ docker save [Image name] 或 :docker save busybox > busybox.tar

 

 

二、docker network:
 

    $ docker network ls

     其中有个bridge的网络,The bridge network is the network in which containers are run by default.

    $ docker network inspect bridge

    In terms of Docker, a bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network. 除了bridge网络,还有别的类型的网络可以创建。

     对于新创建的网络,containers can not only communicate by IP address, but can also resolve a container name to an IP address. This capability is called automatic service discovery

    

 

三、docker的类型:

 

  • Base images are images that have no parent image, usually images with an OS like ubuntu, busybox or debian.
  • Child images are images that build on base images and add additional functionality.
  • Official images: 名称上通常是一个单词,而User Images名称上通常是 user/image-name.

 

四、build my Image:

  • 创建 Dockerfile 文件
  • docker build -t yourusername/catnip .

 

五、Docker Compose:

    Compose is a tool that is used for defining and running multi-container Docker apps in an easy way. It provides a configuration file called docker-compose.yml that can be used to bring up an application and the suite of services it depends on with just one command.  这个yaml文件其实就是描述了怎么从image运行container,service之间的依赖,一些运行参数等。Docker-compose 启动的时候会自动创建一个network,并且把service都加入到这个网络中,可以上面的命令inspect这个网络查看其中包含的containers。

 

    # 启动,-d代表是detached方式

    $ docker-compose up -d 

     $  docker-compose down -v

 

六、Docker的数据共享模式:
参考:Guide to Docker Volumes | Baeldung on Ops
     1. bind mount: A Docker bind mount is a high-performance connection from the container to a directory on the host machine. It allows the host to share its own file system with the container, which can be made read-only or read-write.

     2. Docker Volumes
       常见的命令有:
   $ docker volume create data_volume
   $ docker volume ls
   $ docker volume inspect ca808e6fd82590dd08
   $ docker volume rm data_volume 或者 $ docker volume prune

 3. Start a Container用-v的方式启动
  The -v option contains three components, separated by colons:

  • Source directory or volume name
  • Mount point within the container
  •  (Optional) ro if the mount is to be read-only

  用bind mount的方式启动
  $ docker run -v $(pwd):/var/opt/project bash:latest bash -c "ls /var/opt/project"
  用volume的方式启动
  $ docker run -v data-volume:/var/opt/project bash:latest bash -c "ls /var/opt/project"

      4. 使用mount的方式带有volume的启动container
          如:$ docker run --mount 'type=volume,src=data-volume, dst=/var/opt/project,volume-driver=local, readonly' bash -c "ls /var/opt/project"

  • type – as volume to indicate a volume mount
  • src – to the name of the volume, though this could have been a source directory if we’d been making a bind mount
  • dst – as the destination mount point in the container
  • volume-driver – the local driver in this case
  • readonly – to make this mount read-only; we could have chosen rw for read/write

 

 

Docker相关的文章:

 

posted @ 2023-12-05 15:48  saaspeter  阅读(12)  评论(0编辑  收藏  举报