Docker Basic Command

## Command
1. Windows Sub linux Ubuntu start docker
`sudo service docker start`
1. show basic information of docker engine 
`docker info`
1. show local images
`docker image ls`
1. remove image
`docker rmi {repo:tag}` or `docker rmi {image_id}`
`docker image prune` remove all not used
1. show image config
`docker image inspect hello-world` hello-world is the image name.
ContainerConfig.Cmd is important.
1. build image
`docker build -t karl/hello:1.0 .` need a Dockerfile in current directory
1. pull from a registry other than docker hub
`docker pull private-docker-registry.example.com/nginx`
1. docker start a container
`docker run -p 80:80 nginx`
`docker run -it --name <container-name> <image-name>` run at inrerative mode
    >-p  the port on the Docker host : the port within the container which is showed on "Config.ExposedPorts" in image inspect
    `docker start -ai container-name`  start docker container interactively

`docker run --entrypoint ifconfig bash` run image "bash" but overwrite the default command by `--entrypoint ifconfig`
`docker run --entrypoint ping bash google.com -c1` overwrite default command with parameters
1. show running containers
`docker ps` 
1. show all containers
`docker ps -a` 
1. tag an image
`docker tag <image id> <repository:tag name>`
`docker tag <old tag> <new tag>`
1. Remove all stopped containers
`docker container prune -f`
`docker rm $(docker ps --filter status=exited -q)`
1. Copy files from container
`docker cp <container-id>:/path-in-container <path-in-local>`
`docker cp <container-name>:/path-in-container <path-in-local>`
1. run sh in container
`docker exec -it <container name> sh`

## Dockerfile Instructions
1. `WORKDIR /path/to/directory` :  sets the current working directory for the RUN, CMD, ENTRYPOINT, COPY, and ADD instructions.
it is the path in the docker container.
1.  `ADD <source> <destination>`
`COPY  <source> <destination>`
1. RUN
1>shell form:to use shell variables, subcommands, command pipes, and command chains
`RUN <command>`
    > A program started in shell form will run as a subcommand of /bin/sh -c. This means the executable will not be running as PID and will not receive UNIX signals. As a consequence, a Ctrl+C to send a SIGTERM will not be forwarded to the container and the application might not exit correctly.

    2>exec form
`RUN ["executable", "parameter 1", " parameter 2"]`
1. Docker creates a new layer whenever it encounters RUN, COPY, or ADD instructions. 
it is best to chain the update and installs, 
RUN apt-get update && apt-get install -y \
   pkg1 \
   pkg2 \

1. You can override the CMD instruction by passing arguments to the docker run command. 
    ```
    CMD ["executable","param1","param2"] (exec form)
    CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
    CMD command param1 param2 (shell form)
    ```
1. `ENTRYPOINT`　set the program to run, pass the parameter in the command line.
`ENTRYPOINT ["curl", "-s"]`
`docker run sathyabhat/curl wttr.in`

1. `ENV <key>=<value> ...`
set env value at run: 
`docker run -it -e LOGS_DIR="/logs" -e APPS_DIR="/opt" karl/hello:0.1`
1. The EXPOSE instruction tells Docker that the container listens for the specified network ports at runtime.
`EXPOSE 53/tcp`
`EXPOSE 53/udp`
Default is TCP
1. Dockerfile Sample
```D
FROM python:3.11
LABEL author="Karl"
LABEL description="An example Dockerfile for python"
RUN echo `uname -rv` > $HOME/kernel-info
RUN apt-get update
WORKDIR /app
COPY hello.py hello.py
ENTRYPOINT ["python"]
CMD ["/app/hello.py"]
```