Docker: Difference between revisions

From Elvanör's Technical Wiki
Jump to navigation Jump to search
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 13: Line 13:
* Execute a command in a running container: docker exec -it <container-name> /bin/bash
* Execute a command in a running container: docker exec -it <container-name> /bin/bash
* Obtaining information about CPU and RAM usage: docker stats
* Obtaining information about CPU and RAM usage: docker stats
* Getting stdout logs from a container: docker logs <container-id>
== Container events ==
* To get an history of container restarts (starts / stops), use the docker system events --since command. The --since is very important, else it will just stream the new events coming.
* When sending docker stop command, a SIGTERM signal is first issued, then after 15 seconds a SIGKILL signal is sent. This means that a manually issued stop command to a container can produce the same error as a OOM killed container, since in both cases the container process with pid 1 will receive a SIGKILL signal.


== Detaching and attaching ==
== Detaching and attaching ==

Latest revision as of 17:19, 8 March 2024

This is a short tutorial to use Docker.

Basic commands

  • List of all containers: docker ps -a
  • Remove a container: docker rm <container-id>
  • Remove all stopped containers: docker container prune
  • List of all images: docker image ls
  • Remove an image: docker rmi <image-id>
  • Remove all dangling images: docker image prune
  • Create a new container (from an image): docker create jatzoo/jrivermc24
  • Create and run a new container (from an image): docker run -d --name=jrivermc24--stable --net=host -e VNCPASS=foobar -v /volume1/Public/music:/mnt/media jatzoo/jrivermc24:latest
  • Execute a command in a running container: docker exec -it <container-name> /bin/bash
  • Obtaining information about CPU and RAM usage: docker stats
  • Getting stdout logs from a container: docker logs <container-id>

Container events

  • To get an history of container restarts (starts / stops), use the docker system events --since command. The --since is very important, else it will just stream the new events coming.
  • When sending docker stop command, a SIGTERM signal is first issued, then after 15 seconds a SIGKILL signal is sent. This means that a manually issued stop command to a container can produce the same error as a OOM killed container, since in both cases the container process with pid 1 will receive a SIGKILL signal.

Detaching and attaching

  • Control-P + Control-Q can be used to detach from a running container.
  • docker attach command can be used to attach / reattach.

Building an image

  • docker build -t app-name .

Dockerfile

  • This can be used to setup the timezone:
ENV TZ=Europe/Moscow
  • ARG directive allows you to provide an argument when building the image. You can provide a default value, and any environment variable that matches exactly the name of the ARG will be passed automatically to the build process.
  • You can later refer to an ARG by using $ + the argument name. For instance, $USER.

Containers

  • A started container is always associated with a command (either one present on the Docker image by default, or another one given on the command-line with docker run). This is the command that will be restarted with docker restart, or ran again after a docker stop / docker start.
  • A container exiting with error code 137 means that the container used too much memory (and was killed by the kernel OOM killer).
  • You can limit the memory used by a given container by using the --memory=500m parameter when starting the container. In that case, if memory runs out, the container is killed/stopped.
  • You can also update such parameters on a running container with the docker update command.
  • Docker can restart automatically containers; usually this is useful when an error occurs (container out of memory for instance). Use --restart on-failure. Also note that after a reboot, containers will be automatically restarted when the Docker daemon starts.

Volumes

  • Volumes with the default local driver can be accessed from the host.
  • A volume can be associated only when the container is created, not with a running container.