There are a number of logical constructs in docker. Images which are built from
a Dockerfile
contain an OS and all the libraries and configs needed to run
some program. Containers are created from an image and represent an actual
running (or stopped) version of an application. Volumes are used by containers
for persistent storage. There are also docker-compose.yml
files which are used
to tie together a bunch of these containers and volumes into a single service.
These are very useful if you have a bunch of micro-services.
kubernetes
For managing groups of computers "nodes" running docker containers.
huge power usage
https://www.beatworm.co.uk/blog/thinkpad/docker-ate-my-battery
As a result I tend to keep docker disabled on void linux. To re-enable it:
sudo ln -s /etc/sv/containerd /var/service/
sudo ln -s /etc/sv/docker /var/service/
images
building
This builds and locally installs your image.
docker build .
-t 'name:tag'
- Name and optionally tag the image.
list locally installed images
docker images
remove installed image
docker rmi name
remove all images
docker rmi -f $(docker images -aq)
run (create and start)
This creates a "container" from your image and starts it up. If you don't name
the container it gets a random automatic one.
docker run name:tag
-d
- Detach and run container in the background.
-p 8080:80
- Allow access on a certain port. This maps 8080 on the local
machine to 80 in the container.
--name
- Name the container rather than using docker's auto generated names.
containers
By default docker containers are restriced with limited system resources and their own network which cannot by default interact with the host system. Ensure you permit anything needed.
create (container from image)
Alternatively to run
you can create
a container from an image without also
starting it.
docker create
remove installed container
docker rm name
remove all containers
docker rm -vf $(docker ps -aq)
start or stop containers
docker start name
docker stop name
list running images/containers
docker ps
-a
- All. Include non-running containers in the list.
logs
Print out the logs from a container without attaching to it.
docker logs name
-f
- Follow the logs. Continue printing output.
compose (stacks)
Compose reads a docker-compose.yml
file and builds any missing images, then
creates and starts any containers needed with the config options (such as opened
ports). This is a simple and rather limited container orchestration tool, like
kubernetes. It's useful for smaller deployments where kubernetes would add
unnecessary complexity.
docker compose up -d
volumes
Used for persistent storage.
override hosts
--add-host hostname:ip
launch options for the container
Believe it or you can add launch options for the container at the end of a docker run command:
docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always --add-host api.portainer.io:127.0.0.1 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainerci/portainer-ee:2.20 --license-expire-absolute --log-level=DEBUG