您好,登錄后才能下訂單哦!
最簡單運行容器:
docker run 鏡像名
docker run centos:v1(這個是鏡像名稱)
docker run -dit --name=c1 centos:v1
[root@docker ~]# docker tag hub.c.163.com/public/centos:6.7-tools centos:v1
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest b8fd9553f1f0 4 weeks ago 445 MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 2 years ago 407 MB
centos v1 b2ab0ed558bb 2 years ago 602 MB
hub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 2 years ago 602 MB
查看正在運行的容器:
docker ps
查看所有容器:
docker ps -a
運行容器:
docker run -t centos:v1
提供一個模擬終端,但不能交互,這樣容器里面終端不關閉,容器也不會關閉docker run -t -i centos:v1
允許交互
可以這樣寫:```
docker run -it centos:v1
我們在運行一個容器的時候,如果沒有指定容器運行什么程序,則默認運行鏡像最上面一層的CMD所指的東西。
我們可以用指定的命令,替換掉容器里默認的程序:
如:
[root@docker ~]# docker run centos:v1 /bin/bash
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4bbd7c1810d1 centos:v1 "/bin/bash" About a minute ago Exited (0) About a minute ago sleepy_spence
5be8fb481041 centos:v1 "/usr/bin/supervisord" 17 minutes ago Up 17 minutes 22/tcp c1
如果直接執行docker run centos:v1,是執行CMD "/usr/bin/supervisord"命令,但是我們通過修改指令,變成運行/bin/bash。 由于/bin/bash生命周期非常短,執行完成則關閉了。
可以指定不同的shell:
[root@docker ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@docker ~]# docker run -t -i centos:v1 /bin/sh
sh-4.1# exit
exit
[root@docker ~]# docker run -t -i centos:v1 /bin/bash
[root@53f96ebba145 /]#
驗證容器里面的程序結束,容器也跟著結束:
老段在驗證過程是用了個CMD "/bin/bash"的鏡像,容器一運行,就很快結束。
docker run centos:v1 sleep 10
可以觀察到10秒后,容器也跟著結束
創建容器同時定義名稱
docker run --name=c1 centos:v1
停掉/開啟/進入j交互容器
docker run -it --name=c1 centos:v1 /bin/bash
docker rm c1
docker start c1
docker attach c1
只要退出容器,容器就會關閉,怎么做到退出也仍然保留容器呢?
docker run -it --name=c1 --restart=always centos:v1
[root@docker ~]# docker run -it --name=c3 --restart=always centos:v1 /bin/bash
頻繁測試時候,會產生大量的不運行的容器,需要逐個刪非常麻煩,我們可以加--rm
docker run -it --name=c1 --rm centos:v1
docker run -it --name=c3 --restart=always centos:v1 /bin/bash
運行退出后,docker ps -a 下也會自動刪除
在交互環境下,改容器的hostname,可以加-h 名字
[root@docker ~]# docker run -it --name=c3 --rm -h test centos:v1 /bin/bash
[root@test /]#
容器放在后臺運行:-d
docker run --it -d --name=c1 centos:v1
練習:開發環境中,需要python3的環境,但是每次都需要部署安裝python3環境就非常麻煩,咱們可以直接運行容器實現:
1 首先找python3的鏡像:
docker search pyhon3
或者在網易云/阿里云上找鏡像。
2 下載
docker pull docker.io/sgoblin/python3.4
3 查看CMD:
[root@docker ~]# docker history docker.io/sgoblin/python3.4
IMAGE CREATED CREATED BY SIZE COMMENT
f01226b9a9cf 23 months ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/bash"] 0 B
<missing> 23 months ago /bin/sh -c #(nop) CMD [] 0 B
<missing> 23 months ago /bin/sh -c pip3 install -U virtualenv 3.86 MB
<missing> 23 months ago /bin/sh -c ln -s /usr/bin/python3.4 /usr/b... 10.9 MB
<missing> 23 months ago /bin/sh -c #(nop) ADD 79312a2a112b91c4ece7... 1.6 MB
<missing> 23 months ago /bin/sh -c sed -i 's/archive.ubuntu.com/mi... 341 MB
<missing> 23 months ago /bin/sh -c #(nop) MAINTAINER Ryan M. <rya... 0 B
<missing> 3 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 3 years ago /bin/sh -c sed -i 's/^#\s(deb.universe... 1.88 kB
<missing> 3 years ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/po... 701 B
<missing> 3 years ago /bin/sh -c #(nop) ADD file:3f4708cf445dc1b... 131 MB
[root@docker ~]#
4運行容器:
docker run -it --name=p1 --restart=always docker.io/sgoblin/python3.4
我們需運行mysql
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest b8fd9553f1f0 4 weeks ago 445 MB
docker.io/sgoblin/python3.4 latest f01226b9a9cf 23 months ago 489 MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 2 years ago 407 MB
centos v1 b2ab0ed558bb 2 years ago 602 MB
hub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 2 years ago 602 MB
[root@docker ~]#
[root@docker ~]# docker run -d --name=db --restart=always mysql
03a9ed98a6c87d07e072af59c6e6a758f200bc7db312142a76781da1c4a56f1a
[root@docker ~]#
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03a9ed98a6c8 mysql "docker-entrypoint..." 8 seconds ago Restarting (1) 1 second ago db
65a7fc20e52a docker.io/sgoblin/python3.4 "/bin/bash" 14 minutes ago Up 13 minutes p1
[root@docker ~]#
[root@docker ~]#
[root@docker ~]# docker log
login logout logs
[root@docker ~]# docker logs db 查看容器里面運行的內容使用logs
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
....
[root@docker ~]#
[root@docker ~]# docker logs -f db
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
.....
[root@docker ~]#
可以看到我們沒有定義數據庫相關的變量,就會有報錯提示,需要定義數據庫、賬號、密碼等
docker run -dit --restart=always -e 變量1=值1 -呃變量2=值2 鏡像
docker run -dit --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=woaini mysql
可以下載客戶端驗證:
yum install mariadb -y
查看容器mysql運行時候的IP:
docker exec db ip a
docker inspect db (db是容器名字)
docker run -d --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=woaini mysql
docker exec -it db /bin/bash
mysql -uroot -predhat
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
使用客戶端驗證docker運行 mysql時候出現的報錯:
[root@docker ~]# mysql -uroot -predhat -h272.17.0.2
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
問題原因:可以看到MySQL8.0.12版本默認的認證方式是caching_sha2_password,連接不上的原因在于下載的mariadb客戶端版本比較低,不支持該格式的密碼。
解決辦法:
[root@docker ~]# docker run -d --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=xxx mysql
9f947de785f8514d0688abbf30715022c6ffcdb8e3071029a818b51db1399a4b
[root@docker ~]# docker exec -it db /bin/bash
root@9f947de785f8:/# mysql -uroot -predhat
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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> select version();
+-----------+
| version() |
+-----------+
| 8.0.17 |
+-----------+
1 row in set (0.00 sec)
mysql> show variables like 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| default_authentication_plugin | caching_sha2_password |
+-------------------------------+-----------------------+
1 row in set (0.01 sec)
mysql> select host,user,plugin from mysql.user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | root | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.01 sec)
flush privileges;
mysql> select host,user,plugin from mysql.user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | root | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | mysql_native_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
mysql>
練習:
安裝nginx
安裝mysql
安裝tomcat
安裝redis
安裝apache
安裝nginx:
[root@docker ~]# docker pull hub.c.163.com/library/nginx:latest
Trying to pull repository hub.c.163.com/library/nginx ...
latest: Pulling from hub.c.163.com/library/nginx
5de4b4d551f8: Pull complete
d4b36a5e9443: Pull complete
0af1f0713557: Pull complete
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6
Status: Downloaded newer image for hub.c.163.com/library/nginx:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest b8fd9553f1f0 5 weeks ago 445 MB
docker.io/sgoblin/python3.4 latest f01226b9a9cf 23 months ago 489 MB
hub.c.163.com/library/nginx latest 46102226f2fd 2 years ago 109 MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 2 years ago 407 MB
centos v1 b2ab0ed558bb 2 years ago 602 MB
hub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 2 years ago 602 MB
[root@docker ~]#
[root@docker ~]#
[root@docker ~]# docker history hub.c.163.com/library/nginx
IMAGE CREATED CREATED BY SIZE COMMENT
46102226f2fd 2 years ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daem... 0 B
<missing> 2 years ago /bin/sh -c #(nop) STOPSIGNAL [SIGQUIT] 0 B
<missing> 2 years ago /bin/sh -c #(nop) EXPOSE 80/tcp 0 B
<missing> 2 years ago /bin/sh -c ln -sf /dev/stdout /var/log/ngi... 22 B
<missing> 2 years ago /bin/sh -c apt-get update && apt-get inst... 52.2 MB
<missing> 2 years ago /bin/sh -c #(nop) ENV NJS_VERSION=1.13.0.... 0 B
<missing> 2 years ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.13.... 0 B
<missing> 2 years ago /bin/sh -c #(nop) MAINTAINER NGINX Docker... 0 B
<missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 2 years ago /bin/sh -c #(nop) ADD file:253a2a9f49900b3... 57.1 MB
[root@docker ~]#
[root@docker ~]# docker run -d --name=web --restart=always -p 80:80 hub.c.163.com/library/nginx
d283f5c11513a068e871603067e8147a6e18c8aebb8c0c4b87c86846b8e7a304
/usr/bin/docker-current: Error response from daemon: driver failed programming external connectivity on endpoint web (1ab60e90073de05080aa53be78cff38a60b5c04fef6d51fbd8b82039695cbbfc): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 80 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).
[root@docker ~]#
出現報錯,查詢百度:
實際原因:
docker服務啟動時定義的自定義鏈DOCKER由于某種原因被清掉
重啟docker服務及可重新生成自定義鏈DOCKER
解決方法:
守護進程重啟 sudo systemctl daemon-reload
重啟docker服務 systemctl restart docker
[root@docker ~]# docker run -d --name=web --restart=always -p 80:80 hub.c.163.com/library/nginx
bfc14f7146607b351d5a1b4c9484cd0aecec7afdebd6526a3eddd9fac17fbb0a
[root@docker ~]#
[root@docker ~]# docker inspect web
在圖形界面的瀏覽器輸入IP地址,即可看到登陸nginx的網頁
安裝tomcat
docker pull hub.c.163.com/library/tomcat:latest
docker images
docker history hub.c.163.com/library/tomcat
docker run -d --name=tm --restart=always hub.c.163.com/library/tomcat
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。