安裝
使用官方安裝指令碼自動安裝
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
手動安裝
CentOS 7 (使用yum進行安裝)
解除安裝舊版docker
sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
# step 1: 安裝必要的一些系統工具sudo yum install -y yum-utils# Step 2: 新增軟體源資訊 (推薦使用下面阿里雲源)sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo# 新增阿里雲源資訊sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo# 開啟一些選項 (這一步可跳過)sudo yum-config-manager --enable docker-ce-nightlysudo yum-config-manager --enable docker-ce-test# 關閉選項sudo yum-config-manager --disable docker-ce-nightly# 安裝 Docker-CEsudo yum install docker-ce docker-ce-cli containerd.io
驗證
sudo docker version
啟動docker
配置映象加速器可以上阿里雲獲取個人加速地址
https://dev.aliyun.com/https://cr.console.aliyun.com/cn-shenzhen/instances/mirrors
修改daemon配置檔案/etc/docker/daemon.json使用加速器
sudo mkdir -p /etc/dockersudo tee /etc/docker/daemon.json <<-'EOF'{ "registry-mirrors": ["https://e3b4lt8s.mirror.aliyuncs.com"]}EOF
重啟
sudo systemctl daemon-reloadsudo systemctl restart docker
hello world
docker run hello-world
命令
檢視幫助
docker --help
Usage: docker [OPTIONS] COMMANDA self-sufficient runtime for containersOptions: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -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 quitManagement Commands: builder Manage builds config Manage Docker configs container Manage containers context Manage contexts engine Manage the docker engine 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 trust Manage trust on Docker images volume Manage volumesCommands: attach Attach local standard input, output, and error streams 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 to files or directories 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 codesRun 'docker COMMAND --help' for more information on a command.
檢視指定命令幫助
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]List imagesOptions: -a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show numeric IDs
常用命令
檢視本地映象
docker imagesdocker images -a # 檢視本地所有映象(包括懸空映象)docker images -q # 檢視本地映象的映象ID資訊docker images -qadocker images --digests # 顯示映象的摘要寫資訊docker images --no-trunc # 檢視完整的映象資訊
搜尋映象
docker search -s 30 nginxdocker search --filter=stars=20 nginx
列出自動構建的映象
docker search --automated nginxdocker search --filter=is-automated=true nginx
拉取映象
docker rmi -f $(docker images -qa)1
容器命令
容器不但可以包含軟體, 容器也可以包含一個作業系統
docker search centos
新建並執行容器
docker run
檢視幫助
docker run --help# Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
執行容器
docker run -it centosdocker run -it centos /bin/bash
-i: 以互動模式啟動容器, 和-t搭配使用
-t: 為容器重新分配一個輸入終端
docker run -it 0d120b6ccaa8
指定容器新名字
docker run -it --name mycentos centos
檢視正在執行的容器
docker ps
檢視正在執行+歷史執行過的容器
docker ps -a
檢視上一次執行的容器
docker ps -ldocker ps -lq
檢視最近2次執行的容器
docker ps -n 2
關閉並退出容器
exit
只退出不關閉容器
ctrl+p+q
重啟啟動容器
docker start IDdocker start 04ac5ae5d5fb
停止容器
docker stop IDdocker stop 04ac5ae5d5fb
強制停止容器
docker rm -f ID
一次性所有容器
docker rm -f $(docker ps -qa)1
以守護程序啟動容器
docker run -d centos # [這樣會馬上退出]
不退出
docker run -d centos /bin/sh -c "while true; do echo hello soul; sleep 2; done"
檢視容器日誌
docker logs IDdocker logs -f -t --tail 100 ID
檢視容器內執行的程序
docker top IDdocker top e7574dd446d6
檢視容器內部細節
docker inspect ID
進入正在執行的容器
docker attach IDdocker exec -t ID /bin/bash
直接容器執行命令
docker exec -t ID ls -l /tmp
從容器複製檔案到主機上
# docker cp 容器ID:容器內路徑 目的主機路徑docker cp 04ac5ae5d5fb:/mnt/test.log /data1
生成新映象副本
# docker commit 提交容器副本成為一個新的映象# docker commit -m="描述" -a="作者" 容器ID 映象名:版本docker commit -m="my tomcat" -a="soul" 04ac5ae5d5fb soul/go:1.0
執行自定義映象
docker run -it -p 7777:8080 soul/tomcat:1.0
指定埠執行
# docker run -it -p 主機埠:容器埠 tomcatdocker run -it -p 8081:8080 tomcat
瀏覽器訪問
http://localhost:8080
隨機分配主機埠
docker run -it -P tomcat
後臺執行
docker run -d -p 6666:8080 tomcat
指定容器資料卷啟動 (等於主機目錄掛載到容器指定目錄)
# docker run -it -v 主機目錄:容器目錄 映象名docker run -it -v /mydata:/mydata centos
只讀許可權
docker run -it -v /mydata:/mydata:ro centos
容器資料卷共享
docker run -it --name dc01 soul/centosdocker run -it --name dc02 --volumes-from dc01 soul/centosdocker run -it --name dc03 --volumes-from dc01 soul/centos
Redis執行容器
docker run -p 6379:6379-v /data1/redis/data:/data-v /data1/redis/conf/redis.conf:/usr/locak/etc/redis/redis.conf-d redis:5.0.10 redis-server /usr/local/etc/redis/redis.conf--appendonly yes
MySQL執行容器
docker run -p 12345:3306 --name mysql -v /data1/mysql/conf:/etc/mysql/conf.d-v /data1/mysql/logs:/logs-v /data1/mysql/data:/var/lib/mysql-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.6
備份資料庫
docker exec 04ac5ae5d5fb sh -C 'exec mysqldump --all-databases -uroot -p"123456"' > /data1/all-databases.sql
讀者福利:關注小編+轉發文章+私信【專案】獲取整理好的100+個Java專案影片+原始碼+筆記
提交映象到阿里雲登入阿里雲Docker Registry
$ sudo docker login --username=uisoul registry.cn-shenzhen.aliyuncs.com
用於登入的使用者名稱為阿里雲賬號全名,密碼為開通服務時設定的密碼。
您可以在訪問憑證頁面修改憑證密碼。
從Registry中拉取映象
$ sudo docker pull registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[映象版本號]
將映象推送到Registry
$ sudo docker login --username=uisoul registry.cn-shenzhen.aliyuncs.com$ sudo docker tag [ImageId] registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[映象版本號]$ sudo docker push registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[映象版本號]
請根據實際映象資訊替換示例中的[ImageId]和[映象版本號]引數。
最新評論