docker容器學(xué)習(xí)筆記(常用命令記錄)

在centos7上操作

一、安裝、啟動(dòng)、開(kāi)機(jī)啟動(dòng)、停止docker

1.Docker 要求 CentOS 系統(tǒng)的內(nèi)核版本高于 3.10

查看內(nèi)核版本
[root@common-server ~]# uname -r
3.10.0-693.21.1.el7.x86_64
注:返回的版本可以安裝docker

用yum安裝(如果之間安裝過(guò),先卸載)
[root@common-server ~]# yum -y install docker

用wget安裝(這里是非root用戶)
[xl@common-server ~]# sudo wget -qO- https://get.docker.com/ | sh
  將將當(dāng)前用戶追加到docker組中(這樣執(zhí)行的時(shí)候就不用sudo提權(quán)了)
[xl@common-server ~]# sudo usermod -aG docker xl

2.查看是否安裝成功

[root@common-server ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
 Go version:      go1.9.4
 Git commit:      94f4240/1.13.1
 Built:           Fri May 18 15:44:33 2018
 OS/Arch:         linux/amd64

3.開(kāi)啟docker服務(wù)端

[root@common-server ~]# service docker start
Redirecting to /bin/systemctl start docker.service
或者
[root@common-server ~]# systemctl start docker

4.開(kāi)啟后在查看版本(服務(wù)端已經(jīng)啟動(dòng)了)

[root@common-server ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
 Go version:      go1.9.4
 Git commit:      94f4240/1.13.1
 Built:           Fri May 18 15:44:33 2018
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
 Go version:      go1.9.4
 Git commit:      94f4240/1.13.1
 Built:           Fri May 18 15:44:33 2018
 OS/Arch:         linux/amd64
 Experimental:    false

5.加入開(kāi)機(jī)啟動(dòng)

[root@common-server ~]# systemctl enable docker

6.關(guān)閉docker服務(wù)端

[root@common-server ~]# service docker stop
Redirecting to /bin/systemctl stop docker.service
或者
[root@common-server ~]# systemctl stop docker

7.網(wǎng)上找的刪除docker的方法(沒(méi)試過(guò),只作為記錄)

方法一:
yum remove docker docker-common docker-selinux docker-engine -y
/etc/systemd -name '*docker*' -exec rm -f {} ;
find /etc/systemd -name '*docker*' -exec rm -f {} \;
find /lib/systemd -name '*docker*' -exec rm -f {} \;

方法二:
[root@localhost ~]# yum remove docker
[root@localhost ~]# yum remove docker-common
[root@localhost ~]# yum remove container-selinux
  或者
[root@localhost ~]# yum erase docker
[root@localhost ~]# yum erase docker-common
[root@localhost ~]# yum erase container-selinux

二、操作docker

1.查看鏡像(images)

[root@common-server ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED                  SIZE
docker.io/hello-world         latest              2cb0d9787c4d        Less than a second ago   1.85 kB
docker.io/jenkins             latest              00b7c903b9e4        2 weeks ago              696 MB
hub.c.163.com/library/nginx   latest              46102226f2fd        13 months ago            109 MB

2.拉取鏡像

[root@common-server ~]# docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for docker.io/hello-world:latest

或者(指定鏡像中心地址)
[root@common-server ~]# docker pull hub.c.163.com/library/nginx:latest

3.運(yùn)行鏡像

[root@common-server ~]# 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/engine/userguide/

5.在后臺(tái)運(yùn)行鏡像,并指定映射端口

[root@common-server ~]# docker run -d -p 8080:80 hub.c.163.com/library/nginx
356b6966c727f422dfc39222a306271eeef2d76f421ad34aeab740c9c9b89d7f

使用命令查看監(jiān)聽(tīng)的端口(8080已經(jīng)在監(jiān)聽(tīng)容器中的80端口了)
[root@common-server ~]# netstat -na|grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN

6.查看正在運(yùn)行的容器

[root@common-server ~]# docker ps
CONTAINER ID        IMAGE                         COMMAND                  CREATED              STATUS              PORTS                  NAMES
356b6966c727        hub.c.163.com/library/nginx   "nginx -g 'daemon ..."   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp   dazzling_goldwasser

7.進(jìn)入容器內(nèi)部(容器內(nèi)部類似一個(gè)linux系統(tǒng),可以使用(部分)linux命令)

[root@common-server ~]# docker exec -it 8a375439629e bash
root@8a375439629e:/#

在容器內(nèi)查看正在運(yùn)行的任務(wù)
root@8a375439629e:/# ps -ef

退出容器
root@8a375439629e:/# exit
exit

8.停止容器運(yùn)行(只需要輸入能辨別容器的容器id號(hào)碼即可,不需要將整個(gè)容器id輸入完)

[root@common-server ~]# docker stop 356
356

9.查詢?nèi)萜?/p>

[root@common-server ~]# docker ps -a
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                        PORTS               NAMES
28b62dc4bd54        hub.c.163.com/library/nginx   "nginx -g 'daemon ..."   3 hours ago         Exited (0) 20 minutes ago                         tender_noether
66126ce89b60        hub.c.163.com/library/nginx   "nginx -g 'daemon ..."   3 hours ago         Exited (0) 3 hours ago                            pedantic_meninsky
316f23a0bf35        hub.c.163.com/library/nginx   "nginx -g 'daemon ..."   3 hours ago         Exited (0) 3 hours ago                            blissful_lamarr
36e7f241e390        hello-world                   "/hello"                 4 hours ago         Exited (0) 4 hours ago                            naughty_jones
dff23de99bd4        docker.io/jenkins             "/bin/tini -- /usr..."   9 hours ago         Exited (143) 20 minutes ago                       jenkins

10.重啟容器

[root@common-server ~]# docker restart 8a375439629e
8a375439629e

11.刪除容器

[root@common-server ~]# docker rm 214b86ffe5b9
214b86ffe5b9

12.刪除鏡像(要先刪除用這個(gè)鏡像的容器)

[root@common-server ~]# docker rmi docker.io/hello-world
Untagged: docker.io/hello-world:latest
Untagged: docker.io/hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Deleted: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34
Deleted: sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5

三、制作鏡像

1.創(chuàng)建一個(gè)Dockerfile文件,將要發(fā)布的應(yīng)用和這個(gè)文件放在一個(gè)目錄

文件中的內(nèi)容
# 以tomcat為基礎(chǔ)
from hub.c.163.com/library/tomcat:latest
# 作者信息
MAINTAINER xxx@163.com
# 將jpress.war應(yīng)用放到容器中的tomcat中的/usr/local/tomcat/webapps目錄下
COPY jpress.war /usr/local/tomcat/webapps

1.1 編寫Dockerfile(在項(xiàng)目的src同級(jí)目錄下新建Dockerfile文件,將下面內(nèi)容填入)

# 從docker倉(cāng)庫(kù)中拉取一個(gè)能運(yùn)行jar程序的環(huán)境(jdk)
FROM java:8-alpine

# 將自己的程序(app.jar)添加到容器中的指定目錄中(/usr/local/src目錄下)
ADD target/app.jar /usr/local/src/app.jar

# 指定端口(外部訪問(wèn)時(shí)的端口)
EXPOSE 8761

# 執(zhí)行命令啟動(dòng)程序
ENTRYPOINT ["java", "-jar", "/usr/local/src/app.jar"]

2.構(gòu)建鏡像(指定鏡像名)

在有Dockerfile的目錄下執(zhí)行docker build命令(-t后面是鏡像名和標(biāo)簽,最后有個(gè)點(diǎn)表示當(dāng)前目錄)

[root@common-server ~]# docker build -t jpress:latest .
Sending build context to Docker daemon   286 MB
Step 1/3 : FROM hub.c.163.com/library/tomcat:latest
 ---> 72d2be374029
Step 2/3 : MAINTAINER xxx@163.com
 ---> Running in 00ac0fb7ae2c
 ---> 1a0858c478de
Removing intermediate container 00ac0fb7ae2c
Step 3/3 : COPY jpress.war /usr/local/tomcat/webapps
 ---> d52a8f8158f0
Removing intermediate container 494cdef62c83
Successfully built d52a8f8158f0

使用docker images查看,已經(jīng)有剛構(gòu)建的鏡像了
[root@common-server ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED                  SIZE
jpress                         latest              d52a8f8158f0        About a minute ago       313 MB
四.Mysql

啟動(dòng)mysql容器并設(shè)置root密碼和創(chuàng)建數(shù)據(jù)庫(kù)

[root@common-server ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=jpress hub.c.163.com/library/mysql
e5c1d8c219a7de7ab0eb7e59430cc939b07f8d1ca0b93563e1952d03beeadb30
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容