编程语言
首页 > 编程语言> > docker安装与启动,java工程师面试突击第一季

docker安装与启动,java工程师面试突击第一季

作者:互联网

docker pull centos:laster

慢慢的等待镜像文件的下载。下载完可以通过如下命令查看镜像

docker images //查看镜像信息

REPOSITORY TAG IMAGE ID CREATED SIZE

docker.io/centos 7.2.1511 686672a1d0cc 5 weeks ago 194.6 MB

通过docker run来启动镜像,同时会创建一个容器,看下docker run的启动命令:

[root@localhost /]# docker run --help

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

Run a command in a new container

-a, --attach=[] Attach to STDIN, STDOUT or STDERR

–add-host=[] Add a custom host-to-IP mapping (host:ip)

–blkio-weight Block IO (relative weight), between 10 and 1000

–blkio-weight-device=[] Block IO weight (relative device weight)

–cpu-shares CPU shares (relative weight)

–cap-add=[] Add Linux capabilities

–cap-drop=[] Drop Linux capabilities

–cgroup-parent Optional parent cgroup for the container

–cidfile Write the container ID to the file

–cpu-period Limit CPU CFS (Completely Fair Scheduler) period

–cpu-quota Limit CPU CFS (Completely Fair Scheduler) quota

–cpuset-cpus CPUs in which to allow execution (0-3, 0,1)

–cpuset-mems MEMs in which to allow execution (0-3, 0,1)

-d, --detach Run container in background and print container ID

–detach-keys Override the key sequence for detaching a container

–device=[] Add a host device to the container

–device-read-bps=[] Limit read rate (bytes per second) from a device

–device-read-iops=[] Limit read rate (IO per second) from a device

–device-write-bps=[] Limit write rate (bytes per second) to a device

–device-write-iops=[] Limit write rate (IO per second) to a device

–disable-content-trust=true Skip image verification

–dns=[] Set custom DNS servers

–dns-opt=[] Set DNS options

–dns-search=[] Set custom DNS search domains

-e, --env=[] Set environment variables

–entrypoint Overwrite the default ENTRYPOINT of the image

–env-file=[] Read in a file of environment variables

–expose=[] Expose a port or a range of ports

–group-add=[] Add additional groups to join

-h, --hostname Container host name

–help Print usage

-i, --interactive Keep STDIN open even if not attached

–ip Container IPv4 address (e.g. 172.30.100.104)

–ip6 Container IPv6 address (e.g. 2001:db8::33)

–ipc IPC namespace to use

–isolation Container isolation level

–kernel-memory Kernel memory limit

-l, --label=[] Set meta data on a container

–label-file=[] Read in a line delimited file of labels

–link=[] Add link to another container

–log-driver Logging driver for container

–log-opt=[] Log driver options

-m, --memory Memory limit

–mac-address Container MAC address (e.g. 92:d0:c6:0a:29:33)

–memory-reservation Memory soft limit

–memory-swap Swap limit equal to memory plus swap: ‘-1’ to enable unlimited swap

–memory-swappiness=-1 Tune container memory swappiness (0 to 100)

–name Assign a name to the container

–net=default Connect a container to a network

–net-alias=[] Add network-scoped alias for the container

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

oom-kill-disable Disable OOM Killer

–oom-score-adj Tune host’s OOM preferences (-1000 to 1000)

-P, --publish-all Publish all exposed ports to random ports

-p, --publish=[] Publish a container’s port(s) to the host

–pid PID namespace to use

–privileged Give extended privileges to this container

–read-only Mount the container’s root filesystem as read only

–restart=no Restart policy to apply when a container exits

–rm Automatically remove the container when it exits

–security-opt=[] Security Options

–shm-size Size of /dev/shm, default value is 64MB

–sig-proxy=true Proxy received signals to the process

–stop-signal=SIGTERM Signal to stop a container, SIGTERM by default

–sysctl=map[] Sysctl options

-t, --tty Allocate a pseudo-TTY

–tmpfs=[] Mount a tmpfs directory

-u, --user Username or UID (format: <name|uid>[:<group|gid>])

–ulimit=[] Ulimit options

–uts UTS namespace to use

-v, --volume=[] Bind mount a volume

–volume-driver Optional volume driver for the container

–volumes-from=[] Mount volumes from the specified container(s)

-w, --workdir Working directory inside the container

启动并创建一个交互式的docker容器:

[root@localhost /]# docker run -ti -d 686672a1d0cc

//-d为后台启动

通过docker ps 来查看当前运行的容器,看下docker ps的相关指令:

[root@localhost /]# docker ps --help

Usage: docker ps [OPTIONS]

List containers

-a, --all Show all containers (default shows just running)

-f, --filter=[] Filter output based on conditions provided

–format Pretty-print containers using a Go template

–help Print usage

-l, --latest Show the latest created container (includes all states)

-n=-1 Show n last created containers (includes all states)

–no-trunc Don’t truncate output

-q, --quiet Only display numeric IDs

-s, --size Display total file sizes

[root@localhost /]# docker ps//查看当前正在运行的容器

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7d95b3df8674 686672a1d0cc “/bin/bash” 5 hours ago Up About an hour admiring_kowalevski

[root@localhost /]# docker ps -a//查看所有的容器信息

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7d95b3df8674 686672a1d0cc “/bin/bash” 5 hours ago Up About an hour admiring_kowalevski

1b4ac575eda0 686672a1d0cc “/bin/bash” 23 hours ago Exited (137) About an hour ago elated_turing

通过docker start,docker stop来启动和停止容器:

[root@localhost /]# docker start --help

Usage: docker start [OPTIONS] CONTAINER [CONTAINER…]

Start one or more stopped containers

-a, --attach Attach STDOUT/STDERR and forward signals

–detach-keys Override the key sequence for detaching a container

–help Print usage

-i, --interactive Attach container’s STDIN

[root@localhost /]# docker stop --help

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER…]

Stop a running container.

Sending SIGTERM and then SIGKILL after a grace period

–help Print usage

-t, --time=10 Seconds to wait for stop before killing it

[root@localhost /]# docker restart --help

Usage: docker restart [OPTIONS] CONTAINER [CONTAINER…]

Restart a container

–help Print usage

-t, --time=10 Seconds to wait for stop before killing the container

使用docker exec 可以进入到已经启动的容器中,低版本的docker可能不行。

[root@localhost Desktop]# docker exec -ti 7d95b3df8674 /bin/bash

[root@7d95b3df8674 /]#

容器的工作是建立在镜像的基础之上的,如果需要删除镜像的话,需要先删除使用该镜像的容器,然后才能删除镜像,否则删除镜像的时候,会有如下的类似的错误信息提示:Failed to remove image

(e7b): Error response from daemon: conflict: unable to delete e7b2de517efa (must be forced) - image is being used by stopped container 4fbc3cd00987,可以通过docker rm删除容器,docker rmi 删除镜像。

[root@localhost /]# docker rm --help

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER…]

Remove one or more containers

-f, --force Force the removal of a running container (uses SIGKILL)

–help Print usage

-l, --link Remove the specified link

-v, --volumes Remove the volumes associated with the container

[root@localhost /]# docker rmi --help

Usage: docker rmi [OPTIONS] IMAGE [IMAGE…]

Remove one or more images

-f, --force Force removal of the image

–help Print usage

–no-prune Do not delete untagged parents

删除停止的容器

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

删除没有使用的镜像

docker rmi -f $(docker images | grep “” | awk “{print $3}”)

批量删除容器

docker ps -a | awk ‘{print $1}’ | xargs docker rm

批量删除镜像

docker images | awk ‘{print $3}’ | xargs docker rmi

持久化容器与镜像

1.通过容器生成新的镜像

运行中的镜像称为容器。你可以修改容器(比如删除一个文件),但这些修改不会影响到镜像。不过,你使用docker commit 命令可以把一个正在运行的容器变成一个新的镜像。

docker commit [repo:tag] 将一个container固化为一个新的image,后面的repo:tag可选。

2.持久化容器

docker export用于持久化容器。

[root@localhost /]# docker export --help

Usage: docker export [OPTIONS] CONTAINER

Export a container’s filesystem as a tar archive

–help Print usage

-o, --output Write to a file, instead of STDOUT

[root@localhost /]# docker export container.tar

3.持久化镜像

======================================================================

docker save用于持久化镜像:

[root@localhost /]# docker save --help

Usage: docker save [OPTIONS] IMAGE [IMAGE…]

Save an image(s) to a tar archive (streamed to STDOUT by default)

–help Print usage

-o, --output Write to a file, instead of STDOUT

[root@localhost /]# docker save image.tar

4.导入持久化镜像,容器:

============================================================================

使用docker import,docker load导入镜像容器:

[root@localhost /]# docker import --help

Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

-c, --change=[] Apply Dockerfile instruction to the created image

–help Print usage

-m, --message Set commit message for imported image

[root@localhost /]# docker load --help

Usage: docker load [OPTIONS]

Load an image from a tar archive or STDIN

–help Print usage

-i, --input Read from a tar archive file, instead of STDIN

5.对镜像打tag

[root@localhost /]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

docker.io/centos 7.2.1511 686672a1d0cc 5 weeks ago 194.6 MB

[root@localhost /]# docker tag 686672a1d0cc centos:base

[root@localhost /]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

centos base 686672a1d0cc 5 weeks ago 194.6 MB

docker.io/centos 7.2.1511 686672a1d0cc 5 weeks ago 194.6 MB

6.export-import与save-load的区别

===========================================================================================

导出后再导入(export-import)的镜像会丢失所有的历史,而保存后再加载(save-load)的镜像没有丢失历史和层(layer)。这意味着使用导出后再导入的方式,你将无法回滚到之前的层(layer),同时,使用保存后再加载的方式持久化整个镜像,就可以做到层回滚。(可以执行docker tag 来回滚之前的层)。

docker logs $CONTAINER_ID #查看docker实例运行日志,确保正常运行

docker inspect $CONTAINER_ID #docker inspect <image|container> 查看image或container的底层信息

docker build 寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image

docker build -t repo[:tag] 同上,可以指定repo和可选的tag

docker build - < 使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image

docker port 查看本地哪个端口映射到container的指定端口,其实用docker ps 也可以看到。

7.docker文件存放目录

=============================================================================

Docker实际上把所有东西都放到/var/lib/docker路径下了。

至此一个简单干净的docker环境搭建完毕。

docker images //查看本地镜像

docker ps -a //查看所有容器

docker ps //查看当前有哪些容器正在运行

docker rmi 镜像名称/镜像ID 删除镜像

docker rm 容器名称/容器ID 删除容器(删除前必须先停止容器的运行)

docker start 容器名称/容器ID 启动一个容器

docker restart 容器名称/容器ID 重启一个容器

docker stop 容器名称/容器ID 停止一个在运行的容器

docker run -d -p 8081:8080 --name tomcat01 tomcat 利用镜像创建一个容器

-d: 在后台运行

-p:映射端口号 这里将tomcat01的端口8080映射到宿主机的8081端口

–name: 为容器取名字

tomcat: 本地镜像仓库的镜像

ctrl+d 退出容器且关闭,

标签:容器,container,--,第一季,镜像,java,docker,root
来源: https://blog.csdn.net/m0_63174811/article/details/121994893