Docker basics-image, container, repository/docker三大件:镜像、容器和仓库

Docker basics-image, container, repository/docker三大件:镜像、容器和仓库

Docker basics-image, container, repository

1. Docker repository

docker search ubuntu
docker pull ubuntu:16.04

2. Docker image commands

docker images -a
docker images -a -q
docker rmi $image-id
docker tag $image-id $image-repository:$image-tag

3. Docker container commands

docker ps -a
docker ps -a -q
docker ps -l

docker stats
docker logs -f $container-id/$container-name
docker top $container-id/$container-name  //returns pid info **inside** the container.
docker inspect $container-id/$container-name //returns json format info of the container.
docker stop $container-id/$container-name
docker kill $container-id/$container-name
docker start $container-id/$container-name
docker restart $container-id/$container-name
docker rm $container-id/$container-name
docker diff $container-id/$container-name
docker cp $container-id:/container_path to_host_path


docker run -d -p 127.0.0.1:5000:5000/udp --name $container-name $image-name $shell-command
docker port $container-name 5000
*127.0.0.1:5001*

docker create  --name $container-name $image-name  //Create a container without running it. Use it with docker run --name $container-name or docker start -a -i $container-name

Two ways to create new images of your own:

  1. By command line

    docker run -t -i ubuntu:16.04 /bin/bash   //When no container of this image is not running.
    **or**
    docker exec -i -t $container-id/$container-name  //Or just -it should work as well.
    **or** 
    docker attach $container-id/$container-name  //Attach causes the container to stop after using `exit`. Use CTRL-C instead.
    
    # after finishing some operations in the container, commit to save changes to create  a new image.
    docker commit -m="has done something" -a="hok" $container-id hok/ubuntu16.04:v1
    
  2. By dockerfile

    By writing your own docker file and run the command docker build command to build your own image.

    docker build -t hok/nginx:v1 $path_to_Dockfile
    

Two ways to share local images:

  1. Use save/load commands

    docker save $image-name -o ./consul.tar

    docker load -i ./consul.tar

  2. Use Dockerfile to share your configuration.

One quick way to clean your docker containers and images

docker kill $(docker ps -q) 
docker rm $(docker ps -a -q)
docker rmi $(docker images -q -a) 
docker run -it --rm --name nginx-test nginx:stable /bin/bash

--rm option makes the container removed after exit the bash session.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.