0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Docker Basic Command

Last updated at Posted at 2022-12-24

Command

  1. Windows Sub linux Ubuntu start docker
    sudo service docker start
  2. show basic information of docker engine
    docker info
  3. show local images
    docker image ls
  4. remove image
    docker rmi {repo:tag} or docker rmi {image_id}
    docker image prune remove all not used
  5. show image config
    docker image inspect hello-world hello-world is the image name.
    ContainerConfig.Cmd is important.
  6. build image
    docker build -t karl/hello:1.0 . need a Dockerfile in current directory
  7. pull from a registry other than docker hub
    docker pull private-docker-registry.example.com/nginx
  8. 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
  2. show all containers
    docker ps -a
  3. tag an image
    docker tag <image id> <repository:tag name>
    docker tag <old tag> <new tag>
  4. Remove all stopped containers
    docker container prune -f
    docker rm $(docker ps --filter status=exited -q)
  5. Copy files from container
    docker cp <container-id>:/path-in-container <path-in-local>
    docker cp <container-name>:/path-in-container <path-in-local>
  6. 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.

  2. ADD <source> <destination>
    COPY <source> <destination>

  3. 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"]

  4. 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 \

  5. 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)
    
  6. ENTRYPOINT set the program to run, pass the parameter in the command line.
    ENTRYPOINT ["curl", "-s"]
    docker run sathyabhat/curl wttr.in

  7. ENV <key>=<value> ...
    set env value at run:
    docker run -it -e LOGS_DIR="/logs" -e APPS_DIR="/opt" karl/hello:0.1

  8. 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

  9. 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"]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?