Deploying Your First Docker Container

文章源自Katacoda Docker Playground的学习

Step 1 - Running A Container

# find existing images
# docker search <name>
$ docker search redis

# To run in the background, the option -d needs to be specified
$ docker run -d redis

Step 2 - Finding Running Containers

# lists all running containers
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0fd76e3ea29a        redis               "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        6379/tcp            dazzling_poincare

# more details about a running container
# docker inspect <friendly-name|container-id>
$ docker inspect 0fd76e3ea29a
[
    {
        "Id": "0fd76e3ea29a7c3335c883b626667725cb2d8cf2019870c9efa62af2696b7a04",
        "Created": "2020-04-29T15:48:17.702215202Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "redis-server"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            
.......
]

# display messages the container stderr or stdout
# docker logs <friendly-name|container-id>
$ docker logs 0fd76e3ea29a
1:C 29 Apr 15:48:24.011 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 29 Apr 15:48:24.011 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=1, just started
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
1:M 29 Apr 15:48:24.013 * Running mode=standalone, port=6379.
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.
1:M 29 Apr 15:48:24.013 # Server initialized
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.
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.
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

猜你喜欢

转载自www.cnblogs.com/Vito-L/p/deploying-your-first-docker-container.html