Command
- Windows Sub linux Ubuntu start docker
sudo service docker start
- show basic information of docker engine
docker info
- show local images
docker image ls
- remove image
docker rmi {repo:tag}
ordocker rmi {image_id}
docker image prune
remove all not used - show image config
docker image inspect hello-world
hello-world is the image name.
ContainerConfig.Cmd is important. - build image
docker build -t karl/hello:1.0 .
need a Dockerfile in current directory - pull from a registry other than docker hub
docker pull private-docker-registry.example.com/nginx
- 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
- show running containers
docker ps
- show all containers
docker ps -a
- tag an image
docker tag <image id> <repository:tag name>
docker tag <old tag> <new tag>
- Remove all stopped containers
docker container prune -f
docker rm $(docker ps --filter status=exited -q)
- Copy files from container
docker cp <container-id>:/path-in-container <path-in-local>
docker cp <container-name>:/path-in-container <path-in-local>
- run sh in container
docker exec -it <container name> sh
Dockerfile Instructions
-
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. -
ADD <source> <destination>
COPY <source> <destination>
-
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"]
-
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 \ -
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)
-
ENTRYPOINT
set the program to run, pass the parameter in the command line.
ENTRYPOINT ["curl", "-s"]
docker run sathyabhat/curl wttr.in
-
ENV <key>=<value> ...
set env value at run:
docker run -it -e LOGS_DIR="/logs" -e APPS_DIR="/opt" karl/hello:0.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 -
Dockerfile Sample
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"]