Docker can be thought of in similar vein to virtualization however instead of virtualizing an entire computer it runs a container which consists of the necessary items required to run an application. Instead of having to setup an entire server, install the prerequisites, and install the server it instead launches a container with all of this setup already.
This allows you to run multiple applications on a single server that may have clashing prerequisites as each container runs independently, such as running how I setup this very blog!
Containers are designed to be brought up very quickly and re-created as and when. For example if a new version of the software the container is running is released instead of updating this software inside the container you instead destroy the container, download the newest version of the image, then recreate it.
Volumes are attached to the container to store important data on the host instead of inside the container. For example if you run a container that consists of a application and a database you may not care about keeping the application data as this simply runs the application but you will want to keep the database as this contains all your important information.
This means that if you destroy then re-create the container your database is still intact and re-attached to this new version of the container.
Containers
To get started here are a few simple commands related to containers
Show all available containers along with status.
docker container ls
Start/Stop a container
This stops the named container but does not remove it
docker container stop <name>
This starts the named container again
docker container start <name>
Note <name> only works if you have specified the container name on creation. If not then you need to use container id by listing containers
Remove/delete a container
This deletes the container from the docker host
docker container rm <name>
Creating a container
The example below creates a container called portainer using the image portainer, port 8000 and 9000 are mapped, a restart policy is configured to always restart the container, and some volumes are mapped to store data on the host so it will not be deleted along with the container if the container gets removed (allows us to destroy the recreate container without losing data)
Images by default get downloaded from Docker Hub so creating a portainer container will download the latest version from here if the image has not already been downloaded before.
docker run -d -e PUID=1000 -e PGID=1000 -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer:/data portainer/portainer
- -e PUID – PUID and PGID define which user and group will be mapped from the host machine to the internal user in the container. By default container users will be mapped to root on the host machine which is a security issue. These commands ensure that the containers users map to youraccount instead. 1000 is the default user ID number for the first user created on the server.
- -p – Map a port to the container, the first part is the port used on the host, the second is the port used in the container
- –name – Friendly name of the container
- –restart – Determine if the container will restart under certain conditions. Flags available are
- no – Do not automatically restart the container. (default)
- on-failure – Restart the container if it exits due to an error, which manifests as a non-zero exit code.
- always – Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted.
- unless-stopped – Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.
- -v – Attach a volume to the container to store data on the host. In the above example the host path /var/snap/docker/common/var-lib-docker/volumes/portainer (As we’re using the snap package for Docker on Ubuntu) is mapped to the folder /data inside the container. Any data then created in /data inside the container is then stored in this host path on the docker host. For more info on how/why this works see the Volumes section below.
Volumes
Containers can use volumes to ensure that data persists between sessions. It is possible to directly reference a file path and use this as a volume however the preferred mechanism is to use docker volumes.
Volumes get created in the default path of /var/lib/docker/volumes or in the case of the snap package used on S-DOCKER1 /var/snap/docker/common/var-lib-docker/volumes/
Create a volume
This command will create a folder called mynewvolume in the path mentioned above as a docker volume
docker volume create mynewvolume
F.A.Q
What is docker.sock?
docker.sock represents the daemon (service) that runs docker. Attaching this as a volume to a container is usually used to allow the container to directly interact with the docker daemon. In the example of portainer this is required so the container can directly manage the docker host (such as listing, stopping, starting, and configuring containers on said host)
What is portainer?
Portainer is a piece of software with a web interface that gives you a user friendly GUI to manage your Docker installation instead of needing to know the CLI commands.
It’s worth getting this running when you first start using Docker as it will give you an easier visual representation of what’s running and an easy way to read the logs for your container.
What is an image?
A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. These images are what you use to create a container.
For example if you want to create a container that runs a basic apache install you could use the httpd image which is the officially maintained Apache image. This image consists of a lightweight linux distro called alpine along with a configured apache installation.
References
- Docker restart settings – https://docs.docker.com/config/containers/start-containers-automatically/
- docker.sock – https://datacadamia.com/vm/docker/sock
- Portainer – https://www.portainer.io/portainer-ce/
- Docker image – https://www.docker.com/resources/what-container
- PUID & PGID – https://docs.linuxserver.io/general/understanding-puid-and-pgid
- Volumes – https://docs.docker.com/storage/volumes/
Leave a Reply