关于Docker容器再次强调的概念:
docker容器可以理解为在沙盒中运行的进程。这个沙盒包含了该进程运行所必须的资源,包括文件系统、系统类库、shell 环境等等。但这个沙盒默认是不会运行任何程序的。你需要在沙盒中运行一个进程来启动某一个容器。这个进程是该容器的唯一进程,所以当该进程结束的时候,容器也会完全的停止。
从DockerHub镜像托管仓库拉取项目需要的镜像文件命令:docker pull 镜像名称:版本号,比如:docker pull mysql:7.5.30
查看docker容器中已经的拉取的所有镜像:docker images
删除docker容器中已经拉取的镜像:docker rmi 镜像名称:版本号 / 或者镜像ID
查看进程:docker ps (如果加参数-a,把已经停止的运行的进程也显示)
开启已经存在的容器进程:docker start 容器名称 / 容器ID
停止容器进程 :docker stop 容器名称 / 容器ID
删除一个容器进程:docker rm 容器名称 / 容器ID
运行一个容器进程的命令(暂且以MySQL 5.7.30版本举例):
docker run -dt --name=test_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30
容器进程运行成功后,进入容器进程的命令:docker exec -it 容器名称 /bin/bash (说明:它是通过执行:容器名称下的bin目录下的bash脚本来进入容器的,这时你会看到你在容器ID的目录下)
更多docker命令(通过上面的几个说明,下面的英文命令应该会更好的理解了,不再翻译,遇到难点会补充在文后):
ubuntu@ubuntu:~$ docker Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers (self-sufficient adj. 自给自足的;极为自负的;过于自信的) Options: --config string Location of client config files (default "/home/ubuntu/.docker") -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 "/home/ubuntu/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/home/ubuntu/.docker/cert.pem") --tlskey string Path to TLS key file (default "/home/ubuntu/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: builder Manage builds config Manage Docker configs container Manage containers 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 volumes Commands: 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 deploy Deploy a new stack or update an existing stack 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 codes Run 'docker COMMAND --help' for more information on a command.上面命令演示:
root@ubuntu:/home/ubuntu# ls Desktop Documents Downloads examples.desktop Music Pictures Public Templates Videos root@ubuntu:/home/ubuntu# docker images //查看docker容器中已经拉取的镜像,这里只有一个mysql REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7.30 9cfcce23593a 4 months ago 448MB root@ubuntu:/home/ubuntu# docker ps //查看容器中运行的进程 没有运行任何进程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES root@ubuntu:/home/ubuntu# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES root@ubuntu:/home/ubuntu# whoami //查看自己的权限等级 此时在根用户 即超级用户权限下 root root@ubuntu:/home/ubuntu# docker run -dt --name=test_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql //没写版本号,默认启动最新版本MySQL,但是没发现镜像 Unable to find image 'mysql:latest' locally docker: Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 127.0.1.1:53: server misbehaving. See 'docker run --help'. //解释见文后 root@ubuntu:/home/ubuntu# docker run -dt --name=test_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30 5f9cf41737de4ba18f5084463026426411753bdbb6bc75fcd636cd88b65afa76 root@ubuntu:/home/ubuntu# docker ps //查看容器进程 此时已经运行了test_mysql CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f9cf41737de mysql:5.7.30 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp test_mysql root@ubuntu:/home/ubuntu# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f9cf41737de mysql:5.7.30 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp test_mysql root@ubuntu:/home/ubuntu# docker stop test_mysql //停止容器进程test_mysql test_mysql root@ubuntu:/home/ubuntu# docker ps //查看进程 就没有运行的了 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES root@ubuntu:/home/ubuntu# docker ps -a //加-a 查看包括已经停止的进程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f9cf41737de mysql:5.7.30 "docker-entrypoint.s…" 4 minutes ago Exited (0) 23 seconds ago test_mysql root@ubuntu:/home/ubuntu# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES root@ubuntu:/home/ubuntu# docker start test_mysql //重新启动容器进程test_mysql test_mysql root@ubuntu:/home/ubuntu# docker ps //运行起来了 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f9cf41737de mysql:5.7.30 "docker-entrypoint.s…" 5 minutes ago Up 10 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp test_mysql root@ubuntu:/home/ubuntu#命令解释:docker run -dt(守护模式启动) --name=test_mysql(容器进程起名) -p 3306:3306(指明对外端口号和对内端口号) -e MYSQL_ROOT_PASSWORD=123456 (运行MySQL时的密码) mysql:5.7.30(要运行镜像的名字和版本号)
docker run 命令的运行模式,可以使用docker run --help查看:
root@ubuntu:/home/ubuntu# docker run --help Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] //命令使用的组合方式 Run a command in a new container Options: --add-host list Add a custom host-to-IP mapping (host:ip) -a, --attach list Attach to STDIN, STDOUT or STDERR --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --blkio-weight-device list Block IO weight (relative device weight) (default []) --cap-add list Add Linux capabilities --cap-drop list Drop Linux capabilities --cgroup-parent string Optional parent cgroup for the container --cidfile string Write the container ID to the file --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota --cpu-rt-period int Limit CPU real-time period in microseconds --cpu-rt-runtime int Limit CPU real-time runtime in microseconds -c, --cpu-shares int CPU shares (relative weight) --cpus decimal Number of CPUs --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) -d, --detach Run container in background and print container ID --detach-keys string Override the key sequence for detaching a container --device list Add a host device to the container --device-cgroup-rule list Add a rule to the cgroup allowed devices list --device-read-bps list Limit read rate (bytes per second) from a device (default []) --device-read-iops list Limit read rate (IO per second) from a device (default []) --device-write-bps list Limit write rate (bytes per second) to a device (default []) --device-write-iops list Limit write rate (IO per second) to a device (default []) --disable-content-trust Skip image verification (default true) --dns list Set custom DNS servers --dns-option list Set DNS options --dns-search list Set custom DNS search domains --entrypoint string Overwrite the default ENTRYPOINT of the image -e, --env list Set environment variables --env-file list Read in a file of environment variables --expose list Expose a port or a range of ports --group-add list Add additional groups to join --health-cmd string Command to run to check health --health-interval duration Time between running the check (ms|s|m|h) (default 0s) --health-retries int Consecutive failures needed to report unhealthy --health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s) --health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s) --help Print usage -h, --hostname string Container host name --init Run an init inside the container that forwards signals and reaps processes -i, --interactive Keep STDIN open even if not attached --ip string IPv4 address (e.g., 172.30.100.104) --ip6 string IPv6 address (e.g., 2001:db8::33) --ipc string IPC mode to use --isolation string Container isolation technology --kernel-memory bytes Kernel memory limit -l, --label list Set meta data on a container --label-file list Read in a line delimited file of labels --link list Add link to another container --link-local-ip list Container IPv4/IPv6 link-local addresses --log-driver string Logging driver for the container --log-opt list Log driver options --mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33) -m, --memory bytes Memory limit --memory-reservation bytes Memory soft limit --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap --memory-swappiness int Tune container memory swappiness (0 to 100) (default -1) --mount mount Attach a filesystem mount to the container --name string Assign a name to the container --network string Connect a container to a network (default "default") --network-alias list Add network-scoped alias for the container --no-healthcheck Disable any container-specified HEALTHCHECK --oom-kill-disable Disable OOM Killer --oom-score-adj int Tune host's OOM preferences (-1000 to 1000) --pid string PID namespace to use --pids-limit int Tune container pids limit (set -1 for unlimited) --privileged Give extended privileges to this container -p, --publish list Publish a container's port(s) to the host -P, --publish-all Publish all exposed ports to random ports --read-only Mount the container's root filesystem as read only --restart string Restart policy to apply when a container exits (default "no") --rm Automatically remove the container when it exits --runtime string Runtime to use for this container --security-opt list Security Options --shm-size bytes Size of /dev/shm --sig-proxy Proxy received signals to the process (default true) --stop-signal string Signal to stop a container (default "SIGTERM") --stop-timeout int Timeout (in seconds) to stop a container --storage-opt list Storage driver options for the container --sysctl map Sysctl options (default map[]) --tmpfs list Mount a tmpfs directory -t, --tty Allocate a pseudo-TTY --ulimit ulimit Ulimit options (default []) -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) --userns string User namespace to use --uts string UTS namespace to use -v, --volume list Bind mount a volume --volume-driver string Optional volume driver for the container --volumes-from list Mount volumes from the specified container(s) -w, --workdir string Working directory inside the container root@ubuntu:/home/ubuntu#此时再回过头来看看这句话,会更好理解了:
root@ubuntu:/home/ubuntu# docker run -dt --name=test_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30运行一个进程以启动一个容器(一个应用就是一个进程,比如MySQL),需要使用上述进入容器的命令:
root@ubuntu:/home/ubuntu# docker run -p 3306:3306 --name=blog_test_mysql -e MYSQL_ROOT_PASSWORD=123456 -dt mysql:5.7.30 eaada9d1d0544bee853a4d9821f80b16bfefe99811c9e24b6b8970f5268717e0 root@ubuntu:/home/ubuntu# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eaada9d1d054 mysql:5.7.30 "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp blog_test_mysql root@ubuntu:/home/ubuntu# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eaada9d1d054 mysql:5.7.30 "docker-entrypoint.s…" 29 minutes ago Up 29 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp blog_test_mysql root@ubuntu:/home/ubuntu# root@ubuntu:/home/ubuntu# root@ubuntu:/home/ubuntu# root@ubuntu:/home/ubuntu# docker exec -it blog_test_mysql /bin/bash root@eaada9d1d054:/# //已经进入容器了 在容器ID为eaada9d1d054的目录下 root@eaada9d1d054:/# root@eaada9d1d054:/# root@eaada9d1d054:/# pwd / root@eaada9d1d054:/# root@eaada9d1d054:/# root@eaada9d1d054:/# root@eaada9d1d054:/# mysql -uroot -p123456 //mysql的-u指的是用户名root,-p指的是密码为123456 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 2 Server version: 5.7.30 MySQL Community Server (GPL) Copyright (c) 2000, 2020, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql>进入容器之后就可以通过用户名和密码登录MySQL:mysql -uroot -p123456
最后,通过show databases命令查看数据库,看到输出就感觉到很熟悉了。和Oracle输出的数据库差不多!
root@ubuntu:/home/ubuntu# whoami root root@ubuntu:/home/ubuntu# ifconfig //在跟用户下查看本机的IP地址 docker0 Link encap:以太网 硬件地址 02:40:12:17:7c:9f inet 地址:182.17.0.1 广播:182.17.255.255 掩码:255.255.0.0 inet6 地址: fe80::42:12ff:fe17:7c9f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1 接收数据包:0 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:64 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:0 接收字节:0 (0.0 B) 发送字节:6944 (6.9 KB) ens33 Link encap:以太网 硬件地址 00:00:29:32:a1:1d inet 地址:182.16.185.132 广播:182.16.185.255 掩码:255.255.255.0 inet6 地址: fe80::96999:e15a:9bef:f840/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1 接收数据包:430206 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:56802 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:1000 接收字节:638862281 (638.8 MB) 发送字节:3626629 (3.6 MB) lo Link encap:本地环回 inet 地址:127.0.0.1 掩码:255.0.0.0 inet6 地址: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 跃点数:1 接收数据包:1319 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:1319 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:1000 接收字节:100647 (100.6 KB) 发送字节:100647 (100.6 KB) veth59ea942 Link encap:以太网 硬件地址 ea:9b:11:55:a5:e4 inet6 地址: fe88::e89b:1cff:fe55:a5e4/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1 接收数据包:0 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:40 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:0 接收字节:0 (0.0 B) 发送字节:4350 (4.3 KB)查看到IP地址(ens33开头处对应inet比如182.16.185.132)之后将它作为Navicat Premium链接MySQL的主机(host)地址,输入MySQL账户、密码,链接成功如下:
如果感觉上述运行启动MYSQL容器进程麻烦,可以参见更简单的:通过docker-compose, 执行yam配置文件进行运行启动MySQL容器:https://blog.csdn.net/luqingshuai_eloong/article/details/109236678
一些补充参见:
https://blog.csdn.net/u013378306/article/details/86668313?utm_medium=distribute.pc_relevant_download.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-baidujs-1.nonecase