docker provides the ability to package and run an application in a loosely isolated environment called container.
docker cli
the docker engine consists of three parts:
- docker cli
- docker rest api
- docker daemon
When you run a command, behind the scenes the client sends a request through the REST API to the docker daemon which takes care of images, containers and other resources.
docker management commands like compose, image, system, etc. follow this semantics.
docker COMMAND SUBCOMMAND [options] [arguments]
container
- lightweight
- independent - they contain everything required to run an application
- leverages kernel namespaces and cgroups
- can interact with the host machine via. TCP / UDP
docker run --name container-name -dp 8080:3000 image-name:tag
above command runs a container, downloads if it does not exist locally.
- -d β detached mode, runs in background
- -p HOST:CONTAINER β port map, maps port 3000 in the container to 8080 in the host machine
- you could also run
docker container run, it follows thedocker COMMAND SUBCOMMANDsemantics
docker container sub-commands
e.g. docker container SUBCOMMANDS
lsβ lists all running containers-a: all containers and-s: for size- same as
docker psordocker container ps
start/stopβ start / stop container (-d: detached mode)rm NAME|ID1 NAME|ID2 ...β delete a container (you need to stop it first)pruneβ delete all stopped containersexec NAME|ID COMMANDβ execute a command inside a container- e.g., docker exec 918ffd ps -a
image vs container
a container is the food while an image is the recipe.
- you cannot change an existing image.
- to create new images, you start with a base image and add layers to it.
dockerfilecontains those layered instructions to build an image.
docker image sub-commands
e.g. docker iamge SUBCOMMAND
buildβ builds an image from a dockerfilepull IMAGE:TAGβ downloads an image from registrypush IMAGE:TAGβ push an image to registryrm NAME|ID1 NAME|ID2 ...β delete an image- you should always delete all containers related to the image first, then delete the image.
pruneβ removes βdangling imagesβ - these are images that have no name.-aβ removes all images not associated with a container.
docker system prune β removes everything:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- unused build cache
copy, history, commit
you can copy files from host machine to your container and vice versa using docker cp or docker container cp. If you are mentioning the container path, it should be of this format: CONTAINER-ID|NAME:CONTAINER-FILE-PATH.
docker cp SOURCE DEST
---
e.g., docker cp /hello.txt c99d19efdab3:/usr/src/app/hello.txt
since we changed the container, you can use docker diff ID|NAME command to check all the changes made in the container. A=added; D=deleted; C=changed.
you can build an image from an container using docker commit. its generally better to use a dockerfile instead as defining the changes to the Dockerfile is much more sustainable method of managing changes.
docker commit CONTAINER-ID|NAME IMAGE-NAME
related
contents
- basic docker commands
- running docker containers
- dockerfile
- bind mount, ports and expose
- docker volumes
- docker-compose
- docker networking
creating container from scratch β https://youtu.be/8fi7uSYlOdc namespaces, cgroups, union filesystems
Link to original