系统环境:CentOS Linux release 7.5.1804 (Core)、Linux 内核版本:Linux localhost.localdomain 3.10.0-862.el7.x86_64(官方推荐内核版本为3.8以上)
查看内核版本命令
1、cat /proc/version
Linux version 3.10.0-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
2、uname -a
查看linux版本:
1、lsb_release -a
2、cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
一、安装docker
[root@localhost ~]# yum -y install docker
启动docker进程:
[root@localhost ~]# systemctl start docker.service
查看docker进程:
[root@localhost ~]# ps -ef | grep docker
[root@localhost ~]# ps -ef | grep docker
root 3444 1 0 14:34 ? 00:00:00 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json --selinux-enabled --log-driver=journald --signature-verification=false --storage-driver overlay2
root 3449 3444 0 14:34 ? 00:00:00 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc --runtime-args --systemd-cgroup=true
root 3535 1455 0 14:35 pts/0 00:00:00 grep --color=auto docker
二、搜索可用docker镜像
[root@localhost ~]# docker search centos
如果出现报错
[root@localhost ~]# docker search centos
Error response from daemon: Get https://index.docker.io/v1/search?q=centos&n=25: net/http: TLS handshake timeout
用以下解决办法,没有的话略过。
- 安装dig工具
[root@localhost ~]# yum install bind-utils
[root@localhost ~]# dig @114.114.114.114 registry-1.docker.io
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> @114.114.114.114 registry-1.docker.io
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34620
;; flags: qr rd ra; QUERY: 1, ANSWER: 8, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;registry-1.docker.io. IN A
;; ANSWER SECTION:
registry-1.docker.io. 30 IN A 52.2.169.2
registry-1.docker.io. 30 IN A 54.164.50.37
registry-1.docker.io. 30 IN A 54.152.200.115
registry-1.docker.io. 30 IN A 52.87.94.70
registry-1.docker.io. 30 IN A 52.55.198.220
registry-1.docker.io. 30 IN A 52.201.142.14
registry-1.docker.io. 30 IN A 54.174.39.59
registry-1.docker.io. 30 IN A 52.202.253.107
;; Query time: 30 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: 六 4月 04 15:25:09 CST 2020
;; MSG SIZE rcvd: 177
选择上面命令执行结果中的一组解析放到本机的/etc/hosts文件里做映射
[root@docker-registry ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
52.2.169.2 registry-1.docker.io
三、下载centos镜像
[root@localhost ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
8a29a15cefae: Downloading [===========> ] 16.85 MB/73.23 MB
启动容器
[root@localhost ~]# docker run -itd centos /bin/bash
c3e865286f054d058f9f01a23ef2a86c58397b2dab8a765f8ffd838043c93005
进入容器
[root@localhost ~]# docker exec -it c3e865286f054d058f9f01a23ef2a86c58397b2dab8a765f8ffd838043c93005/bin/bash
注:删除线部分为启动容器命令后生成的串
yum -y install net-tools
四、部署项目
- docker images 查看docker下镜像文件
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 470671670cac 2 months ago 237 M
只有一个centos镜像
- 安装java镜像
[root@localhost /]# docker pull java:8
Trying to pull repository docker.io/library/java ...
8: Pulling from docker.io/library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for docker.io/java:8
- 安装mysql镜像
[root@localhost /]# docker pull mysql:5.7.24
Trying to pull repository docker.io/library/mysql ...
5.7.24: Pulling from docker.io/library/mysql
177e7ef0df69: Pull complete
cac25352c4c8: Pull complete
8585afabb40a: Pull complete
1e4af4996053: Pull complete
c326522894da: Pull complete
9020d6b6b171: Pull complete
55eb37ec6e5f: Pull complete
1a9d2f77e0e7: Pull complete
d7e648ad64aa: Pull complete
4120d828ea6b: Pull complete
3b39dc5451af: Pull complete
Digest: sha256:bf17a7109057494c45fba5aab7fc805ca00ac1eef638dfdd42b38d5a7190c9bb
Status: Downloaded newer image for docker.io/mysql:5.7.24
- 启动mysql
[root@localhost /]# docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=1234 mysql:5.7.24
836efe39a73f763401d0876bfffbba31f0eb6d08f836f531365037e2a4d65319
- 我们将容器里的配置文件都复制到服务器里面下面是代码ID是你的容器ID
docker cp ID:/etc/mysql/my.cnf /app/soft/docker-config/mysql/conf/