docker基本命令

centos 环境下安装docker 

官网:https://www.docker.com

[sudo] yum update
[sudo] yum install docker

docker run -d -i -t <imageID> /bin/bash     启动centos7

docker attach <ContainerID>                   进入容器

docker 启动/停止

dockerSer.sh

echo 'start or stop'
read cmd
service docker $cmd

 

查看docker信息

[root@localhost my.Shells]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.12.6
Storage Driver: devicemapper
 Pool Name: docker-253:0-34475014-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 10.74 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 11.8 MB
 Data Space Total: 107.4 GB
 Data Space Available: 12.35 GB
 Metadata Space Used: 581.6 kB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.147 GB
 Thin Pool Minimum Free Space: 10.74 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: true
 Deferred Deletion Enabled: true
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.140-RHEL7 (2017-05-03)
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
 Volume: local
 Network: overlay null bridge host
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Security Options: seccomp selinux
Kernel Version: 3.10.0-693.11.6.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 4
Total Memory: 984 MiB
Name: localhost
ID: DGMG:ZB6K:6WXP:XL3L:4EW6:FOES:5U3S:3WFP:HTH3:G6YU:QGER:36ND
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
 127.0.0.0/8
Registries: docker.io (secure)

 

镜像下载和查看

[root@localhost my.Shells]# docker images  //查看已下载镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    latest              2a4cca5ac898        6 days ago          111.5 MB
[root@localhost my.Shells]# docker pull redis  //下载redis镜像
Using default tag: latest
Trying to pull repository docker.io/library/redis ... 
latest: Pulling from docker.io/library/redis
c4bb02b17bb4: Pull complete 
58638acf67c5: Pull complete 
f98d108cc38b: Pull complete 
83be14fccb07: Pull complete 
5d5f41793421: Pull complete 
ed89ff0d9eb2: Pull complete 
Digest: sha256:0e773022cd6572a5153e5013afced0f7191652d3cdf9b1c6785eb13f6b2974b1
[root@localhost my.Shells]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    latest              2a4cca5ac898        6 days ago          111.5 MB
docker.io/redis     latest              1e70071f4af4        5 weeks ago         106.7 MB

 

运行镜像-->容器

[root@localhost my.Shells]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu    latest              2a4cca5ac898        6 days ago          111.5 MB
docker.io/redis     latest              1e70071f4af4        5 weeks ago         106.7 MB
[root@localhost my.Shells]# docker  run  docker.io/redis //此时redis已经启动了
1:C 22 Jan 08:46:17.090 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 22 Jan 08:46:17.091 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 22 Jan 08:46:17.091 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 22 Jan 08:46:17.095 * Running mode=standalone, port=6379.
1:M 22 Jan 08:46:17.096 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 22 Jan 08:46:17.096 # Server initialized
1:M 22 Jan 08:46:17.096 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 22 Jan 08:46:17.099 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 22 Jan 08:46:17.099 * Ready to accept connections

 

查看运行的容器进程

[root@localhost my.Shells]# docker ps  //查看运行中的
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
462a2c9aba6a        docker.io/redis     "docker-entrypoint.sh"   2 minutes ago       Up 2 minutes        6379/tcp            romantic_booth
[root@localhost my.Shells]# docker ps -a  //查看运行和停止的,即所有的
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
462a2c9aba6a        docker.io/redis     "docker-entrypoint.sh"   2 minutes ago       Up 2 minutes                6379/tcp            romantic_booth
501897fca8d5        ubuntu              "echo hello"             42 minutes ago      Exited (0) 42 minutes ago                       fervent_fermat

 

停止容器

[root@localhost my.Shells]# docker stop 462a2c9aba6a //这个id是CONTAINER ID,即容器id,注意不是镜像id(IMAGE ID)
462a2c9aba6a
[root@localhost my.Shells]# docker ps  //运行的没有了
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost my.Shells]# docker ps -a //有两个停止的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
462a2c9aba6a        docker.io/redis     "docker-entrypoint.sh"   5 minutes ago       Exited (0) 12 seconds ago                       romantic_booth
501897fca8d5        ubuntu              "echo hello"             45 minutes ago      Exited (0) 45 minutes ago                       fervent_fermat

 

登录容器

[root@localhost my.Shells]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
462a2c9aba6a        docker.io/redis     "docker-entrypoint.sh"   4 hours ago         Up 11 seconds       6379/tcp            romantic_booth
[root@localhost my.Shells]# docker  exec  -it  462a2c9aba6a  bash  //登陆容器
root@462a2c9aba6a:/data# ls
dump.rdb
root@462a2c9aba6a:/data# redis-cli -p 6379  //登陆redis(必须先登录容器,容器其实就是一个linux系统)                                                                                                                                              
127.0.0.1:6379> keys * //正常操作redis
(empty list or set)
127.0.0.1:6379> quit //退出redis
root@462a2c9aba6a:/opt# exit  //退出容器
exit

删除容器

[root@localhost my.Shells]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
462a2c9aba6a        docker.io/redis     "docker-entrypoint.sh"   5 minutes ago       Exited (0) 12 seconds ago                       romantic_booth
501897fca8d5        ubuntu              "echo hello"             45 minutes ago      Exited (0) 45 minutes ago                       fervent_fermat

[root@localhost my.Shells]# docker rm 501897fca8d5  //注意是容器ID,不是镜像ID。删除ubuntu容器,等于卸载它,但镜像还在,可以下次再运行镜像为容器
501897fca8d5

[root@localhost my.Shells]# docker ps -a  //还剩一个
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
462a2c9aba6a        docker.io/redis     "docker-entrypoint.sh"   11 minutes ago      Exited (0) 6 minutes ago                       romantic_booth

 

删除下载的镜像

docker  rmi  2a4cca5ac898 //注意:这里是镜像ID

保存容器为镜像

把配置好的容器保存为一个新的镜像文件
docker commit -m ‘xxx’ 容器ID  新名字
docker commit -m ‘fun’ e23acsasd4323  nginx-fun
Dockerfile 尽可能少RUN,合并RUN操作

centos7+jdk8+gradle+maven+docker-ce+git 镜像 ,在run.sh中git clone 代码,然后gradle build,然后java -jar启动,
这样镜像不用更新,每次启动容器都会拉代码打包运行。 gitlab
-ci中,可以配置远程登录到启动的容器中,然后git pull 代码,然后gradle build,然后java -jar启动,这样原先环境的数据还在,只是重启了一下服务。
如何远程登录到docker容器 https://www.jianshu.com/p/c4d4ee6f3663
source /etc/profile
echo "evn: centos7"
java -version  # jdk8
git --version  # git version 1.8.3.1
mvn -v         # Apache Maven 3.1.1
gradle -v      # gradle 4.10.3
echo hosts >> /etc/hosts
echo /etc/hosts
#git clone xxx

#java -jar xxx.jar
原文地址:https://www.cnblogs.com/wwzyy/p/8331369.html