Deploying Your First Docker Container
文章源自Katacoda Docker Playground的学习
Step 1 - Running A Container
1 # find existing images 2 # docker search <name> 3 $ docker search redis 4 5 # To run in the background, the option -d needs to be specified 6 $ docker run -d redis
Step 2 - Finding Running Containers
1 # lists all running containers 2 $ docker ps 3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4 0fd76e3ea29a redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 6379/tcp dazzling_poincare 5 6 # more details about a running container 7 # docker inspect <friendly-name|container-id> 8 $ docker inspect 0fd76e3ea29a 9 [ 10 { 11 "Id": "0fd76e3ea29a7c3335c883b626667725cb2d8cf2019870c9efa62af2696b7a04", 12 "Created": "2020-04-29T15:48:17.702215202Z", 13 "Path": "docker-entrypoint.sh", 14 "Args": [ 15 "redis-server" 16 ], 17 "State": { 18 "Status": "running", 19 "Running": true, 20 "Paused": false, 21 22 ....... 23 ] 24 25 # display messages the container stderr or stdout 26 # docker logs <friendly-name|container-id> 27 $ docker logs 0fd76e3ea29a 28 1:C 29 Apr 15:48:24.011 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 29 1:C 29 Apr 15:48:24.011 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=1, just started 30 1:C 29 Apr 15:48:24.011 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 31 1:M 29 Apr 15:48:24.013 * Running mode=standalone, port=6379. 32 1:M 29 Apr 15:48:24.013 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 33 1:M 29 Apr 15:48:24.013 # Server initialized 34 1:M 29 Apr 15:48:24.013 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 35 1:M 29 Apr 15:48:24.013 # WARNING you have Transparent Huge Pages (THP) support enabled inyour kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 36 1:M 29 Apr 15:48:24.013 * Ready to accept connections
Step 3 - Accessing Redis
# bind port using -p <host-port>:<container-port> option # By default, the port on the host is mapped to 0.0.0.0, # which means all IP addresses. You can specify a particular IP address when you define the port mapping, # for example, -p 127.0.0.1:6379:6379 $ docker run -d --name redisHostPort -p 6379:6379 redis:latest $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 99e4f41b9f4d redis:latest "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6379->6379/tcp redisHostPort
Step 4 - Accessing Redis
$ docker run -d --name redisDynamic -p 6379 redis:latest $ docker port redisDynamic 6379 0.0.0.0:32768 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7240e7ac35ef redis:latest "docker-entrypoint.s…" 36 seconds ago Up 35seconds 0.0.0.0:32768->6379/tcp redisDynamic
Step 5 - Persisting Data(持久数据)
# Binding directories (also known as volumes) is done using the option -v <host-dir>:<container-dir> $ docker run -d --name redisMapped -v /opt/docker/data/redis:/data redis # Docker allows you to use $PWD as a placeholder for the current directory $ docker run -d --name redisMapped -v "$PWD/data":/data redis
Step 6 - Running A Container In The Foreground
# containers work with foreground processes, such as ps or bash $ docker run ubuntu ps PID TTY TIME CMD 1 ? 00:00:00 ps $ docker run -it ubuntu bash root@53aa36ae8fa8:/# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr
作者:Vito_L
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
浙公网安备 33010602011771号