skip to Main Content

I am just a beginner in docker. I am building my own docker file using this code.

FROM alpine
RUN apk add --update redis
CMD ["redis-server"]

but the third command CMD is not running. I am unable to see successfully built message.

This is the cmd output that I am getting:

this is the cmd  output that I am getting.

Please help.

3

Answers


  1. CMD is command that runs when you create a container.

    you need to run a container from that image after the build

    docker run -it -p 6379:6379 [image_name] or [image_id]
    

    6379 is default port for redis

    Login or Signup to reply.
  2. Curious, I get good result,
    are you up to date ? (I run on 19.03.13).
    If it do not work maybe try to reinstall.
    Bash output

    Login or Signup to reply.
  3. You should run the last obtained image. Don’t expect that Dockerfile will do this for you.

    Let me explain:

    First let’s begin by recalling what is an image.

    Image = File System Snapshot + Startup Command.

    The full story recaped here :

    You have got 3 instructions in your Dockerfile (FROM, RUN, CMD).

    When you "docker build .", here is what happens:

    $ docker build .
    
    [+] Building 1.8s (6/6) FINISHED
     => [internal] load build definition from Dockerfile                                                                                                                                                   0.0s
     => => transferring dockerfile: 37B                                                                                                                                                                    0.0s
     => [internal] load .dockerignore                                                                                                                                                                      0.0s
     => => transferring context: 2B                                                                                                                                                                        0.0s
     => [internal] load metadata for docker.io/library/alpine:latest                                                                                                                                       1.7s
     => [1/2] FROM docker.io/library/alpine@sha256:ec14c7992a97fc11425907e908340c6c3d6ff602f5f13d899e6b7027c9b4133a                                                                                        0.0s
    

    FROM instruction: Here is the generation of the alpine image with a file system. The image id is displayed after the "sha256:". Let’s call it image A.

     => CACHED [2/2] RUN apk add --update redis                                                                                                                                                            0.0s
     => exporting to image                                                                                                                                                                                 0.0s
     => => exporting layers                                                                                                                                                                                0.0s
     => => writing image sha256:45e4f46a107d5b213a2b325204322e52ac4e4eeeb2282ba66e5c6455310ad282
    

    RUN instruction: It takes the image generated during the previous step (image A). It creates a container out of it, it executes the command in this container. The file system should be modified right now (redis added thanks to the package manager apk). A snapshot is taken from the file system of this container and saved as output for the next instruction (next one is CMD). This output is an image with the id shown after the second "sha256:". Let’s call it image B.

    Now the image B is the one that contains: a file system snapshot (with redis inside) + a startup command (redis-server).

    This image (B) is now ready to be run. The result would be a container:

     $ docker build run image_B
    1:C 07 Apr 2021 09:40:13.168 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    1:C 07 Apr 2021 09:40:13.168 # Redis version=6.0.11, bits=64, commit=1522534f, modified=0, pid=1, just started
    1:C 07 Apr 2021 09:40:13.168 # 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 07 Apr 2021 09:40:13.170 * Running mode=standalone, port=6379.
    1:M 07 Apr 2021 09:40:13.170 # 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 07 Apr 2021 09:40:13.170 # Server initialized
    1:M 07 Apr 2021 09:40:13.171 * Ready to accept connections
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search