Docker
一、docker安装
1、docker官方安装
ubuntu下安装
lsb_release -a # 查看系统信息
uname -a # 查看位数
如果是第一次安装,你需要先添加docker的源然后再安装
1、更新包
$ sudo apt-get update
2、安装证书
$ 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、检测
检测安装是否成功
$ sudo docker run hello-world
安装成功会出现如下输出:
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/
安装后可能出现问题
安装docker之后,测试hello-world镜像,终端卡在Unable to find image 'hello-world:latest' locally位置
参考:https://blog.csdn.net/wireless911/article/details/88989620
2、国内源安装docker
参考:https://www.jianshu.com/p/9b1dd79b5708?tdsourcetag=s_pcqq_aiomsg
为了方便使用,不用sudo就可以运行docker命令,安装好docker后再命令行输入如下命令:
$ sudo usermod -aG docker $USER
运行正常后,重新连接即可。
二、docker简单使用
1、docker基本使用
启动docker 查看docker版本号
$ systemctl start docker
设置开机启动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镜像操作
操作 | 命令 | 说明 |
---|---|---|
检索 | docker search 关键字eg:docker search mysql | 我们经常去docker hub上检索镜像的详细信息,如镜像的TAG |
拉取 | docker pull 镜像名:tag | :tag是可选的,tag表示标签,多为软件的版本,默认是latest(最新的) |
列表 | docker images | 查看所有本地镜像 |
删除 | docker rmi image-id | 删除指定的本地镜像 |
1、检索镜像
[root@localhost ~]# docker search centos
默认会去 https://hub.docker.com/ 搜索,搜过结果如下
[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:仓库名称
- DESCRIPTION:镜像描述
- STARS:用户评价,反应一个镜像的受欢迎程度
- OFFICIAL:是否官方
- AUTOMATED:自动构建,表示该镜像由Docker Hub自动构建流程创建的
2、拉取镜像
拉取镜像默认是从docker hub拉取,这是docker默认的公用仓库,不过缺点是国内下载会比较慢。这里设置从ustc****拉取镜像****(建议使用)。
在宿主机器编辑文件:vi /etc/docker/daemon.json,在该配置文件中加入(没有该文件的话,请先建一个):
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
最后,需要重启docker服务 systemctl restart docker.service 执行拉取镜像命令
[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:镜像所在的仓库名称
- TAG:镜像标签
- lMAGE ID:镜像ID
- CREATED:镜像的创建日期(不是获取该镜像的日期)
- SIZE:镜像大小
这些镜像都是存储在Docker宿主机的/var/lib/docker目录下
4、删除镜像
[root@localhost ~]# docker rmi 41a54fe1f79d
3、Docker容器操作
操作 | 命令 | 说明 |
---|---|---|
运行 | docker run --name container-name -d image-nameeg:docker run –name myredis –d redis | --name:自定义容器名-d:后台运行image-name:指定镜像模板 |
列表 | docker ps(查看正在运行中的容器); | docker ps -a:查看历史运行过的容器docker ps -l:查看最近运行过的容器 |
停止 | docker stop container-name/container-id | 停止当前你运行的容器 |
启动 | docker start container-name/container-id | 启动容器 |
删除 | docker rm container-id | 删除指定容器 |
端口映射 | -p 6379:6379eg:docker run -d -p 6379:6379 --name myredis docker.io/redis | -p: 主机端口(映射到)容器内部的端口 |
容器日志 | docker logs container-name/container-id | |
更多命令 | https://docs.docker.com/engine/reference/commandline/docker/ |
1、启动容器
1)以交互方式启动容器:docker run -it --name 容器名称 镜像 /bin/bash;
- -i:表示以“交互模式”运行容器
- -t:表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即分配一个伪终端。
- --name :为创建的容器命名。
- -v:表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录),可以使用多个-v做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上。
- -d:在run后面加上-d参数,则会创建一个守护式容器在后台运行(这样创建容器后不会自动登录容器,如果只加-i -t两个参数,创建后就会自动进去容器)。
- -p:表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个-p做多个端口映射
创建一个交互式容器并取名为mycentos
[root@localhost docker]# docker run -i -t --name=mycentos centos /bin/bash
2)以守护进程后台方式启动容器:docker run -d --name 容器名称 镜像
[root@localhost ~]# docker search tomcat #搜索tomcat
[root@localhost ~]# docker pull tomcat #拉取tomcat镜像
[root@localhost ~]# docker run -d --name mytomcat -p 8888:8080 tomcat:latest #启动tomcat容器,并做端口映射
[root@localhost ~]# docker ps #查看容器运行列表
2、停止容器
docker stop 容器名称或者容器ID
[root@localhost ~]# docker stop 62a021b1c0fd
3、重启容器
docker start 容器名称或者容器ID
[root@localhost ~]# docker start 62a021b1c0fd
4、删除容器
删除容器必须是已经停止的容器,删除指定容器:docker rm 容器名称或者容器ID;删除所有容器:docker rm ‘docker ps -a -q’
[root@localhost ~]# docker rm 62a021b1c0fd
以上参考:https://www.cnblogs.com/frankdeng/p/9275092.html
5、容器自启
-
在创建容器的时候加参数
docker run --restart=always
-
那已经创建好了怎么办呢?
也不用担心,运行下面的命令
docker update --restart=always 容器名[容器id]
就可以实现容器的自动启动