Docker
一、docker安裝
1、docker官方安裝
ubuntu下安裝
lsb_release -a # 查看系統(tǒng)信息
uname -a # 查看位數(shù)
如果是第一次安裝,你需要先添加docker的源然后再安裝
1、更新包
$ sudo apt-get update
2、安裝證書(shū)
$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
3、添加docker的官方GPGkey
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4、添加docker源
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
5、再次跟新包索引
$ sudo apt-get update
6、安裝docker
$ sudo apt-get install docker-ce
7、檢測(cè)
檢測(cè)安裝是否成功
$ sudo docker run hello-world
安裝成功會(huì)出現(xiàn)如下輸出:
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
安裝后可能出現(xiàn)問(wèn)題
安裝docker之后,測(cè)試hello-world鏡像,終端卡在Unable to find image 'hello-world:latest' locally位置
參考:https://blog.csdn.net/wireless911/article/details/88989620
2、國(guó)內(nèi)源安裝docker
參考:http://www.lxweimin.com/p/9b1dd79b5708?tdsourcetag=s_pcqq_aiomsg
為了方便使用,不用sudo就可以運(yùn)行docker命令,安裝好docker后再命令行輸入如下命令:
$ sudo usermod -aG docker $USER
運(yùn)行正常后,重新連接即可。
二、docker簡(jiǎn)單使用
1、docker基本使用
啟動(dòng)docker 查看docker版本號(hào)
$ systemctl start docker
設(shè)置開(kāi)機(jī)啟動(dòng)docker
$ systemctl enable docker
停止docker
$ systemctl stop docker
查看幫助
$ docker --help
[root@localhost ~]# docker --help
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
2、Docker鏡像操作
操作 | 命令 | 說(shuō)明 |
---|---|---|
檢索 | docker search 關(guān)鍵字eg:docker search mysql | 我們經(jīng)常去docker hub上檢索鏡像的詳細(xì)信息,如鏡像的TAG |
拉取 | docker pull 鏡像名:tag | :tag是可選的,tag表示標(biāo)簽,多為軟件的版本,默認(rèn)是latest(最新的) |
列表 | docker images | 查看所有本地鏡像 |
刪除 | docker rmi image-id | 刪除指定的本地鏡像 |
1、檢索鏡像
[root@localhost ~]# docker search centos
默認(rèn)會(huì)去 https://hub.docker.com/ 搜索,搜過(guò)結(jié)果如下
[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 4709 [OK]
ansible/centos7-ansible Ansible on Centos7 118 [OK]
jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86… 99 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 63 [OK]
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 45 [OK]
tutum/centos Simple CentOS docker image with SSH access 43
centos/mysql-57-centos7 MySQL 5.7 SQL database server 39
gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glust… 34 [OK]
openshift/base-centos7 A Centos7 derived base image for Source-To-I… 33
centos/python-35-centos7 Platform for building and running Python 3.5… 30
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 29
kinogmt/centos-ssh CentOS with SSH 22 [OK]
openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use w… 15
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 7
openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for use … 5
openshift/jenkins-1-centos7 DEPRECATED: A Centos7 based Jenkins v1.x ima… 4
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 2
pivotaldata/centos Base centos, freshened up a little with a Do… 2
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 0
pivotaldata/centos7-test CentosOS 7 image for GPDB testing 0
pivotaldata/centos7-build CentosOS 7 image for GPDB compilation 0
smartentry/centos centos with smartentry 0 [OK]
jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]
- NAME:倉(cāng)庫(kù)名稱(chēng)
- DESCRIPTION:鏡像描述
- STARS:用戶(hù)評(píng)價(jià),反應(yīng)一個(gè)鏡像的受歡迎程度
- OFFICIAL:是否官方
- AUTOMATED:自動(dòng)構(gòu)建,表示該鏡像由Docker Hub自動(dòng)構(gòu)建流程創(chuàng)建的
2、拉取鏡像
拉取鏡像默認(rèn)是從docker hub拉取,這是docker默認(rèn)的公用倉(cāng)庫(kù),不過(guò)缺點(diǎn)是國(guó)內(nèi)下載會(huì)比較慢。這里設(shè)置從ustc****拉取鏡像****(建議使用)。
在宿主機(jī)器編輯文件:vi /etc/docker/daemon.json,在該配置文件中加入(沒(méi)有該文件的話(huà),請(qǐng)先建一個(gè)):
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
最后,需要重啟docker服務(wù) systemctl restart docker.service 執(zhí)行拉取鏡像命令
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
256b176beaff: Pull complete
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest
3、列表鏡像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest 41a54fe1f79d 3 days ago 463 MB
docker.io/centos latest 5182e96772bf 5 weeks ago 200 MB
- REPOSITORY:鏡像所在的倉(cāng)庫(kù)名稱(chēng)
- TAG:鏡像標(biāo)簽
- lMAGE ID:鏡像ID
- CREATED:鏡像的創(chuàng)建日期(不是獲取該鏡像的日期)
- SIZE:鏡像大小
這些鏡像都是存儲(chǔ)在Docker宿主機(jī)的/var/lib/docker目錄下
4、刪除鏡像
[root@localhost ~]# docker rmi 41a54fe1f79d
3、Docker容器操作
操作 | 命令 | 說(shuō)明 |
---|---|---|
運(yùn)行 | docker run --name container-name -d image-nameeg:docker run –name myredis –d redis | --name:自定義容器名-d:后臺(tái)運(yùn)行image-name:指定鏡像模板 |
列表 | docker ps(查看正在運(yùn)行中的容器); | docker ps -a:查看歷史運(yùn)行過(guò)的容器docker ps -l:查看最近運(yùn)行過(guò)的容器 |
停止 | docker stop container-name/container-id | 停止當(dāng)前你運(yùn)行的容器 |
啟動(dòng) | docker start container-name/container-id | 啟動(dòng)容器 |
刪除 | docker rm container-id | 刪除指定容器 |
端口映射 | -p 6379:6379eg:docker run -d -p 6379:6379 --name myredis docker.io/redis | -p: 主機(jī)端口(映射到)容器內(nèi)部的端口 |
容器日志 | docker logs container-name/container-id | |
更多命令 | https://docs.docker.com/engine/reference/commandline/docker/ |
1、啟動(dòng)容器
1)以交互方式啟動(dòng)容器:docker run -it --name 容器名稱(chēng) 鏡像 /bin/bash;
- -i:表示以“交互模式”運(yùn)行容器
- -t:表示容器啟動(dòng)后會(huì)進(jìn)入其命令行。加入這兩個(gè)參數(shù)后,容器創(chuàng)建就能登錄進(jìn)去。即分配一個(gè)偽終端。
- --name :為創(chuàng)建的容器命名。
- -v:表示目錄映射關(guān)系(前者是宿主機(jī)目錄,后者是映射到宿主機(jī)上的目錄),可以使用多個(gè)-v做多個(gè)目錄或文件映射。注意:最好做目錄映射,在宿主機(jī)上做修改,然后共享到容器上。
- -d:在run后面加上-d參數(shù),則會(huì)創(chuàng)建一個(gè)守護(hù)式容器在后臺(tái)運(yùn)行(這樣創(chuàng)建容器后不會(huì)自動(dòng)登錄容器,如果只加-i -t兩個(gè)參數(shù),創(chuàng)建后就會(huì)自動(dòng)進(jìn)去容器)。
- -p:表示端口映射,前者是宿主機(jī)端口,后者是容器內(nèi)的映射端口。可以使用多個(gè)-p做多個(gè)端口映射
創(chuàng)建一個(gè)交互式容器并取名為mycentos
[root@localhost docker]# docker run -i -t --name=mycentos centos /bin/bash
2)以守護(hù)進(jìn)程后臺(tái)方式啟動(dòng)容器:docker run -d --name 容器名稱(chēng) 鏡像
[root@localhost ~]# docker search tomcat #搜索tomcat
[root@localhost ~]# docker pull tomcat #拉取tomcat鏡像
[root@localhost ~]# docker run -d --name mytomcat -p 8888:8080 tomcat:latest #啟動(dòng)tomcat容器,并做端口映射
[root@localhost ~]# docker ps #查看容器運(yùn)行列表
2、停止容器
docker stop 容器名稱(chēng)或者容器ID
[root@localhost ~]# docker stop 62a021b1c0fd
3、重啟容器
docker start 容器名稱(chēng)或者容器ID
[root@localhost ~]# docker start 62a021b1c0fd
4、刪除容器
刪除容器必須是已經(jīng)停止的容器,刪除指定容器:docker rm 容器名稱(chēng)或者容器ID;刪除所有容器:docker rm ‘docker ps -a -q’
[root@localhost ~]# docker rm 62a021b1c0fd
以上參考:https://www.cnblogs.com/frankdeng/p/9275092.html
5、容器自啟
-
在創(chuàng)建容器的時(shí)候加參數(shù)
docker run --restart=always
-
那已經(jīng)創(chuàng)建好了怎么辦呢?
也不用擔(dān)心,運(yùn)行下面的命令
docker update --restart=always 容器名[容器id]
就可以實(shí)現(xiàn)容器的自動(dòng)啟動(dòng)