Docker

Installation

Install on Mac

  • Download Docker dmg and Install: https://docs.docker.com/docker-for-mac/install/
  • Try
    $ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    
    $ docker run hello-world
    Unable to find image 'hello-world:latest' locally
    
    
    latest: Pulling from library/hello-world
    78445dd45222: Pull complete 
    Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
    Status: Downloaded newer image for hello-world:latest
    
    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.
     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://cloud.docker.com/
    
    For more examples and ideas, visit:
     https://docs.docker.com/engine/userguide/
    

Install on CentOS7

Container command

run container

  • -d,--detach: Run container in background and print container ID. ( optional )
  • -p,--publish: Publish a container's port(s) to the host (default []) ( optional )
  • --name: Assign a name to the container ( optional )
    $ docker run -p 8080:80 -d nginx
    ecde48053e2da9042db32f1ee60f1e53ccb57c8bf32ead741a022c15ee6bf607
    

start / stop container

$ docker start webserver
webserver

$ docker stop webserver 
webserver

attach/detach from container

  • ctrl+p and ctrl+q
  • attach to container
    $ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    31973ec56854        centos:6.9          "/bin/bash"         11 minutes ago      Up 11 minutes                           adoring_varahamihira
    
    $ docker attach adoring_varahamihira
    

remove container - rm

  • force to remove although it is running
    $ docker rm -f webserver
    
  • remove all containers
    • -q, --quiet: Only display numeric IDs
      docker rm -f $(docker ps -a -q)
      

show all containers including stopped

$ docker ps -a

$ docker container ls --all
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
b90f8501ec86        nginx               "nginx -g 'daemon ..."   16 minutes ago      Created                                         webserver
37ac3e9b3b1e        hello-world         "/hello"                 27 minutes ago      Exited (0) 20 minutes ago                       distracted_mcclintock

Image Command

List image

docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              5766334bdaa0        2 days ago          183 MB
hello-world         latest              48b5124b2768        2 months ago        1.84 kB

Remove image

$ docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
Deleted: sha256:48b5124b2768d2b917edcb640435044a97967015485e812545546cbed5cf0233
Deleted: sha256:98c944e98de8d35097100ff70a31083ec57704be0991a92c51700465e4544d08

Update Local Images

$ docker pull centos:6.9
6.9: Pulling from library/centos
Digest: sha256:a23bced61701af9a0a758e94229676d9f09996a3ff0f3d26955b06bac8c282e0
Status: Image is up to date for centos:6.9

Build Docker Images

Build Docker Image from container

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
31973ec56854        centos:6.9          "/bin/bash"         18 minutes ago      Up 18 minutes             adoring_varahamihira

$ docker commit adoring_varahamihira centos6-my

Docker Compose

  • multiple Docker containers can be linked manually. By using docker-compose, it can be automated.

Architecture recommendation

External References

Last modified 3 years ago Last modified on 04/29/17 20:38:17