[root@localhost opt]# mkdir apache
[root@localhost opt]# cd apache
[root@localhost apache]# vim Dockerfile
基于基础镜像
FROM centos:7
维护镜像的用户信息
MAINTAINER This is hui
镜像操作指令安装Apache软件
RUN yum -y update
RUN yum -y install httpd
开启80端口
EXPOSE 80 (内端口过会要映射一个端口)
复制网站首页文件
ADD index.html /var/www/html/index.html
将执行脚本复制到镜像中
ADD run.sh /run.sh
RUN chmod 755/run.sh
启动容器时执行脚本
CMD ["/run.sh"][root@localhost apache]# vim index.html
<h1>this is hui </h1>[root@localhost apache]# vim run.sh
#!/bin/bash
rm -rf /run/httpd/* 删除多余的pid文件相当于初始化
exec /usr/sbin/apachectl -D FOREGROUND 启动Apache
执行
[root@localhost apache]# docker build -t httpd:centos . (最后有一个空格和点)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
加载每一层,每执行一步 会创建一个临时容器 运行完后就会删除
[root@localhost apache]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos c1b1b6ac3906 20 minutes ago 419MB
centos 77e6257c9f8d8 5 weeks ago 203MB
[root@localhost apache]# docker run -d -p 1123:80 httpd:centos
[root@localhost apache]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f05a37c2ba6 httpd:centos "/run.sh"7 minutes ago Up 7 minutes 0.0.0.0:1123->80/tcp epic_mayer
-P 随机映射一个端口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
容器开启ssh
[root@localhost ~]# cd /opt
[root@localhost opt]# mkdir sshd
[root@localhost opt]# cd sshd
[root@localhost sshd]# vim Dockerfile
FROM centos:7
MAINTAINER The CentOS Project <cloud-centos>
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo '123456'| passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g'/etc/ssh/sshd_config
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/'/etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700/root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"][root@localhost sshd]# docker build -t sshd -t sshd:new .[root@localhost sshd]# docker run -d -P sshd:new
6684b7a32ed3c8cde55751659024ef7f5419007411ff13401208e767536064df
[root@localhost sshd]# docker ps -
a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6684b7a32ed3 sshd:new "/usr/sbin/sshd -D"32 seconds ago Up 32 seconds 0.0.0.0:32769->22/tcp romantic_shamir
[root@localhost sshd]# ssh localhost -p 32769
The authenticity of host '[localhost]:32769 ([::1]:32769)' can't be established.
RSA key fingerprint is SHA256:rRDAbTc+TPcRGn/ZINgwQwNlOb1wubfdhM8B4MXvAq0.
RSA key fingerprint is MD5:23:20:48:d9:67:38:17:79:82:8d:70:85:35:9a:b1:6c.
Are you sure you want to continue connecting (yes/no)? yes //输入yes
Warning: Permanently added '[localhost]:32769'(RSA) to the list of known hosts.
root@localhost's password://输入设置的密码123456[root@6684b7a32ed3 ~]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
容器使用systemctl
[root@localhost opt]# mkdir systemctl
[root@localhost opt]# cd systemctl
[root@localhost systemctl]# vim Dockerfile
FROM sshd:new
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/;for i in *;do[$i == \
systemd-tmpfiles-setup.service ]|| rm -f $i;done);\
rm -f /lib/systemd/multi-user.target.wants
/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/local-fs.target.wants/*;\
rm -f /lib/systemd/sockets.target.wants/*udev*;\
rm -f /lib/systemd/sockets.target.wants/*initctl*;\
rm -f /lib/systemd/basic.target.wants/*;\
rm -f /lib/systemd/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]
[root@localhost systemctl]# docker build -t systemd:new .
[root@localhost systemctl]# docker run --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init &
[1] 99516
[root@localhost systemctl]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fccd7f83d50 systemd:new "/sbin/init" 48 seconds ago Up 46 seconds 22/tcp happy_merkle
6684b7a32ed3 sshd:new "/usr/sbin/sshd -D" 39 minutes ago Up 39 minutes 0.0.0.0:32769->22/tcp romantic_shamir
[root@localhost systemctl]# docker exec -it 3fccd7f83d50 /bin/bash
[root@3fccd7f83d50 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:sshd(8)
man:sshd_config(5)
[root@3fccd7f83d50 /]# systemctl start sshd
[root@3fccd7f83d50 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2020-09-21 08:01:09 UTC; 2s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 55 (sshd)
CGroup: /docker/3fccd7f83d5015141f9648c047e37d77bf3a8b7b0fb54191ccbc56a97d76721e/system.slice/sshd.service
└─55 /usr/sbin/sshd -D
Sep 21 08:01:09 3fccd7f83d50 systemd[1]: Starting OpenSSH server daemon...
Sep 21 08:01:09 3fccd7f83d50 sshd[55]: WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and...lems.
Sep 21 08:01:09 3fccd7f83d50 sshd[55]: Server listening on 0.0.0.0 port 22.
Sep 21 08:01:09 3fccd7f83d50 sshd[55]: Server listening on :: port 22.
Sep 21 08:01:09 3fccd7f83d50 systemd[1]: Started OpenSSH server daemon.
Hint: Some lines were ellipsized, use -l to show in full.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
3.nginx容器(手工编译)
ADD 除了能复制还能对压缩包进行解压 解压到添加目录
[root@localhost opt]# mkdir nginx
[root@localhost opt]# cd nginx
[root@localhost nginx]# ll
总用量 960-rw-r--r--.1 root root 9808319月 217:14 nginx-1.12.0.tar.gz
[root@localhost nginx]# vim Dockerfile
FROM centos:7
MAINTAINER this is nginx images
RUN yum -y update
RUN yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.12.0/
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
ADD run.
sh /run.sh
RUN chmod 755/run.sh
CMD ["/run.sh"][root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
[root@localhost nginx]# docker build -t nginx:new .[root@localhost nginx]# docker run -d -P nginx:new
99527449f21649b01002b1cb42a8f8dda529e88c5b125a309a6ecedb4dcb887c
[root@localhost nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa44e6f0d3f1 nginx:new "/run.sh"13 seconds ago Up 12 seconds 0.0.0.0:32769->80/tcp,0.0.0.0:32768->443/tcp hardcore_wescoff
3fccd7f83d50 systemd:new "/sbin/init"58 minutes ago Up 58 minutes 22/tcp happy_merkle
6684b7a32ed3 sshd:new "/usr/sbin/sshd -D"2 hours ago Up 2 hours 0.0.0.0:32769->22/tcp romantic_shamir
[root@localhost nginx]# setenforce 0[root@localhost nginx]# systemctl stop firewalld
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Tomcat容器
[root@localhost nginx]# cd /opt
[root@localhost opt]# mkdir tomcat
[root@localhost opt]# cd tomcat/[root@localhost tomcat]# ll
总用量 187652-rw-r--r--.1 root root 107830808月 1217:25 apache-tomcat-9.0.16.tar.
gz
-rw-r--r--.1 root root 1813679429月 316:12 jdk-8u91-linux-x64.tar.gz
[root@localhost tomcat]# vim Dockerfile
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-9.0.16.tar.gz /usr/local
WORKDIR /usr/local
RUN mv apache-tomcat-9.0.16/usr/local/tomcat8
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat8/bin/catalina.sh","run"][root@localhost tomcat]# docker run -d --name tomcat01 -p 1123:8080 tomcat:new
c8c3ee509d1808224cf4edfcddc227de94bc357b4352cc0e5521b4fc2d04cd3c
[root@localhost tomcat]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8c3ee509d18 tomcat:new "/usr/local/tomcat8/…"9 seconds ago Up 8 seconds 0.0.0.0:1123->8080/tcp tomcat01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
启动时报错
docker: Error response from daemon: driver failed programming external connectivity on endpoint tomcat01 (2fb9c01a021d437c4ba3d01cb0437719d9efc00903dc2c545a77a5f89ab62ae5):(iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0--dport 1123-j DNAT --to-destination 172.17.0.3:8080!-i docker0: iptables: No chain/target/match by that name.(exit status 1)
).
解决
[root@localhost tomcat]# systemctl restart docker
删除容器或者重命名
[root@localhost tomcat]# docker ps -a | awk '{print "docker rm " $1 }'| bash
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
mysql容器(手工编译)
[root@localhost tomcat]# cd /opt
[root@localhost opt]# mkdir mysql
[root@localhost opt]# cd mysql
[root@localhost mysql]# vim my.cnf
[client]
port =3306default-character-set=utf8
socket =/usr/local/mysql/mysql.sock
[mysql]
port =3306default-character-set=utf8
socket =/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir =/usr/local/mysql
datadir =/usr/local/mysql/data
port =3306
character_set_server=utf8
pid-file =/usr/local/mysql/mysqld.pid
socket =/usr/local/mysql/mysql.sock
server-id =1#
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZER
O_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@localhost mysql]# ll
总用量 47696-rw-r--r--.1 root root 4883314510月 232017 mysql-boost-5.7.20.tar.gz
[root@localhost mysql]# vim Dockerfile
FROM centos:7
MAINTAINER build image mysqld
RUN yum -y install \
ncurses \
ncurses-devel \
autoconf \
bison \
make \
cmake \
gcc \
gcc-c++
RUN useradd -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1&& make && make install
RUN chown -R mysql:mysql /usr/local/mysql/
RUN rm -rf /etc/
my.cnf
ADD my.cnf /etc
RUN chown mysql:mysql /etc/my.cnf
ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
RUN echo -e "#!/bin/bash \nsystemctl enable mysqld">/run.sh
RUN chmod 755/run.sh
RUN sh /run.sh
CMD ["init"][root@localhost ~]# docker build -t centos:mysql .[root@localhost mysql]# docker run --name=mysql_server -d -P --privileged centos:mysql /usr/sbin/init
06da4f89c789a73af05df55ab2ac87381754cfc3ae106e6e25584a11940b9885
[root@localhost mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06da4f89c789 centos:mysql "/usr/sbin/init"32 seconds ago Up 31 seconds 0.0.0.0:32768->3306/tcp mysql_server
[root@localhost mysql]# docker exec -it 06da4f89c789 /bin/bash
[root@06da4f89c789 mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version:5.7.20 Source distribution
Copyright (c)2000,2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
Query OK,0 rows affected,1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
Query OK,0 rows affected,1 warning (0.00 sec)
mysql> flush privileges;
Query OK,0 rows affected (0.00 sec)
mysql> create database school;
Query OK,1 row affected (0.00 sec)
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || school
|| sys |+--------------------+5 rows in set (0.00 sec)[root@localhost mysql]# setenforce 0[root@localhost mysql]# iptables -F
验证
开一个新的终端
[root@localhost ~]# yum -y install mariadb*[root@localhost ~]# mysql -h 192.168.20.50-u root -P 32768-p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version:5.7.20 Source distribution
Copyright (c)2000,2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || school || sys |+--------------------+5 rows in set (0.00 sec)
[root@localhost apache]# docker pull centos:7
宿主机目录/var/www挂载容器中的/data1
[root@localhost apache]# docker run -v /var/www:/data1 --name web1 -it centos:7/bin/bash
[root@9db6dfc71788 /]# ls
anaconda-post.log data1 etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
[root@9db6dfc71788 /]# cd data1
[root@9db6dfc71788 data1]# touch 1.txt
[root@9db6dfc71788 data1]# ls
1.txt
另一个终端
[root@localhost ~]# cd /var/www
[root@localhost www]# ls
1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
数据卷容器
[root@localhost apache]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9db6dfc71788 centos:7"/bin/bash"2 minutes ago Exited (127)2 seconds ago web1
8f05a37c2ba6 httpd:centos "/run.sh" About an hour ago Up About an hour 0.0.0.0:1123->80/tcp epic_mayer
[root@localhost apache]# docker run --name web100 -v /data1 -v /data2 -it centos:7/bin/bash
[root@ac804f314989 /]# ls
anaconda-post.log data1 dev home lib64 mnt proc run srv tmp var
bin data2 etc lib media opt root sbin sys usr
另一台终端
[root@localhost ~]# docker run -it --volumes-from web100 --name web200 centos:7/bin/bash
[root@888b9c20cb25 /]# ls
anaconda-post.log data1 dev home lib64 mnt proc run srv tmp var
bin data2 etc lib media opt root sbin sys usr
[root@888b9c20cb25 /]# cd data1
[root@888b9c20cb25 data1]# touch 123.txt
[root@888
b9c20cb25 data1]# cd ..[root@888b9c20cb25 /]# cd data2
[root@888b9c20cb25 data2]# touch 456.txt
另一个终端验证同步
[root@ac804f314989 /]# cd data1
[root@ac804f314989 data1]# ls
123.txt
[root@ac804f314989 data1]# cd ..[root@ac804f314989 /]# cd data2
[root@ac804f314989 data2]# ls
456.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
私有仓库的建立
私有仓库(个人镜像仓库)
[root@localhost opt]# docker pull registry
[root@localhost opt]# vim /etc/docker/daemon.json
"insecure-registries":["192.168.20.50:5000"],//添加。注意最后y有个,不加服务起不来[root@localhost opt]# systemctl restart docker
[root@localhost opt]# docker create -it registry /bin/bash
13d2be46f309cbfc98182d1d56f92aedbdf46476a71474332b64ec7511f5b7
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13d2be46f309 registry "/entrypoint.sh /bin…"7 minutes ago Created crazy_chaum
888b9c20cb25 centos:7"/bin/bash"14 minutes ago Exited (137)11 minutes ago web200
ac804f314989 centos:7"/bin/bash"15 minutes ago Exited (0)12 minutes ago web100
9db6dfc71788 centos:7"/bin/bash"18 minutes ago Exited (127)15 minutes ago web1
8f05a37c2ba6 httpd:centos "/run.sh" About an hour ago Exited (137)11 minutes ago epic_mayer
[root@localhost opt]# docker start 13d2be46f309
13d2be46f309
[root@localhost opt]# docker run -d -p 5000:5000-v /data/registry:/tmp/registry registry
4213ef914d8100a803e3ad3b00f17db5912f5318bd5d04afc8041b856f603bb4
[
root@localhost opt]# docker pull nginx
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos c1b1b6ac3906 2 hours ago 419MB
nginx latest 7e4d58f0e5f3 9 days ago 133MB
centos 77e6257c9f8d8 5 weeks ago 203MB
registry latest 2d4f4b5309b1 3 months ago 26.2MB
[root@localhost opt]# docker tag nginx:latest 192.168.20.50:5000/nginx
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos c1b1b6ac3906 2 hours ago 419MB
192.168.20.50:5000/nginx latest 7e4d58f0e5f3 9 days ago 133MB
nginx latest 7e4d58f0e5f3 9 days ago 133MB
centos 77e6257c9f8d8 5 weeks ago 203MB
registry latest 2d4f4b5309b1 3 months ago 26.2MB
[root@localhost opt]# curl -XGET http://192.168.20.50:5000/v2/_catalog
{"repositories":[]}[root@localhost opt]# docker push 192.168.20.50:5000/nginx
[root@localhost opt]# curl -XGET http://192.168.20.50:5000/v2/_catalog
{"repositories":["nginx"]}
测试
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos c1b1b6ac3906 2 hours ago 419MB
192.168.20.50:5000/nginx latest 7e4d58f0e5f3 9 days ago 133MB
nginx latest 7e4d58f0e5f3 9 days ago 133MB
centos 77e6257c9f8d8 5 weeks ago 203MB
registry latest 2d4f4b5309b1 3 months ago 26.2MB
[root@localhost opt]# docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
[root@localhost opt]# docker rmi 192.168.20.50:5000/nginx:latest
Untagged:192.168.20.50:5000/nginx:latest
Untagged:192.168.20.50:5000/nginx@sha256:794275d96b4ab96eeb954728a7bf11156570e8372ecd5ed0cbc7280313a27d19
Deleted: sha256:7e4d58f0e5f3b60077e9a5d96b4be1b974b5a484f54f9393000a99f3b6816e3d
Deleted: sha256:eedfd37abc0b05520a719addf6675b372687fe9a2ed1746fa988335720d4376c
Deleted: sha256:4f052ddd517f6dc5b74d2f7d881838b8ec6a5e67ec7a1da8bbbe91e1e068ad3a
Deleted: sha256:5f78154d356577280e4ac0d9bf0d021d488828e57f953b011dcbadaecd539ce4
Deleted: sha256:db5783668220e16186080f3e9d69ad748aab8c72abb8e457f6fc45369461634b
Deleted: sha256:07cab433985205f29909739f511777a810f4a9aff486355b71308bb654cdc868
[root@localhost opt]# docker pull 192.168.20.50:5000/nginx
Using default tag: latest
latest: Pulling from nginx
d121f8d1c412: Pull complete
ebd81fc8c071: Pull complete
655316c160af: Pull complete
d15953c0e0f8: Pull complete
2ee525c5c3cc: Pull complete
Digest: sha256:794275
d96b4ab96eeb954728a7bf11156570e8372ecd5ed0cbc7280313a27d19
Status: Downloaded newer image for192.168.20.50:5000/nginx:latest
192.168.20.50:5000/nginx:latest
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd centos c1b1b6ac3906 2 hours ago 419MB
192.168.20.50:5000/nginx latest 7e4d58f0e5f3 9 days ago 133MB
centos 77e6257c9f8d8 5 weeks ago 203MB
registry latest 2d4f4b5309b1 3 months ago 26.2MB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
端口映射
[root@localhost opt]# docker run -d -P 192.168.20.50:5000/nginx
53d26ca86a11cae2b815fa6cf72fdb9f164fb3e49f0a0ad04898184fb22098d3
[root@localhost opt]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53d26ca86a11 192.168.20.50:5000/nginx "/docker-entrypoint.…"15 seconds ago Up 14 seconds 0.0.0.0:32768->80/tcp dazzling_shirley
4213ef914d81 registry "/entrypoint.sh /etc…"11 minutes ago Up 11 minutes 0.0.0.0:5000->5000/tcp elegant_galois
1
2
3
4
5
6
7
1
2
3
4
5
6
7
容器互联
[root@localhost opt]# docker run -itd -P --name web11 centos:70b4e50fdbe23f17ab4b40f8dea0ebc2e11f01f146ea27c49b0df28a13e14e850
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b4e50fdbe23 centos:7"/bin/bash"7 seconds ago Up 6 seconds web11
[root@localhost opt]# docker exec -it 0b4e50fdbe23 /bin/bash
再打开一个终端
[root@localhost ~]# docker run -itd -P --name web12 --link web11:web11 centos:7/bin/bash
349164b10b351970ac95ff147e1bb1e2bf864fb37b7e48beaf0b430948e0d30c
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
349164b10b35 centos:7"/bin/bash"2 seconds ago Up 1 second web12
0b4e50fdbe23 centos:7"/bin/bash" About a minute ago Up About a minute web11
[root@localhost ~]# docker exec -it 349164b10b35 /bin/bash
[root@349164b10b35 /]# ping web11
PING web11 (172.17.0.4)56(84) bytes of data.64 bytes from web11 (172.17.0.4): icmp_seq=1 ttl=64 time=0.084 ms
64 bytes from web11 (172.17.0.4): icmp_seq=2 ttl=64 time=0.048 ms
64 bytes from web11 (172.17.0.4): icmp_seq=3 ttl=64 time=0.049 ms
^C
--- web11 ping statistics ---3 packets transmitted,3 received
,0% packet loss, time 2000ms
rtt min/avg/max/mdev =0.048/0.060/0.084/0.017 ms
[root@0b4e50fdbe23 /]# yum install net-tools -y
[root@0b4e50fdbe23 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:04 txqueuelen 0(Ethernet)
RX packets 4961 bytes 12032149(11.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3101 bytes 170965(166.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000(Local Loopback)
RX packets 0 bytes 0(0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0(0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@349164b10b35 /]# yum install net-tools -y
[root@349164b10b35 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.5 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:05 txqueuelen 0(Ethernet)
RX packets 3734 bytes 11961122(11.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2636 bytes 145827(142.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000(Local Loopback)
RX packets 0 bytes 0(0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0(0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0