官網:https://www.docker.comjava
[sudo] yum update
[sudo] yum install docker
docker run -d -i -t <imageID> /bin/bash 啓動centos7linux
docker attach <ContainerID> 進入容器nginx
dockerSer.sh echo 'start or stop' read cmd service docker $cmd
[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