Docker Learning Notes
[Personal Devlog: August 29, 2023]
Docker Concepts
Docker is a virtualization software.It makes developing and deploying applications much easier.
Containers are isolated environments on Computers, it helps application to run on specific environments without interfering with the other applications’ specs.
The container is also sharable to the other team members so that they can run the application on the same environment without any issues. It has all the dependencies, source code needed.
A container can also be uploaded to a server, along with the configuration for the server to run and build application properly.
The difference between Virtual Machines & Containers
A virtual machine has it’s own full operating system.
Containers share the host’s operating system.
Docker commands run the same on different OS environments
Virtual Machine Layers (First two parts)
OS Application Layer — OS Kernel — Hardware
Docker virtualize OS Applications Layer, thus docker images are smaller, occupying less space. In terms of performance it all depends on the configurations, settings and hardware.
A Docker image can include all these conponents:
- Runtime environment
- Source code
- Dependencies
- Environment variables
- Commands and scripts
Images in Docker
Images are blueprints for containers — Note: images are read only
One image can run several containers.
Some useful command lines.
To copy an image using
docker pull [node/python3/whatever]
Without any tags this command will just pull the latest images. It’s constantly updating, so the environmental variables can change.
docker images — shows docker images information.
docker ps — shows docker container processes.
docker ps -a (or -all) — shows all containers (stopped and running)
docker run -d [image name]:[tag] — This command runs the image with optional tag. (If the specified image that is not pulled from the container hub will be automatically pulled before it runs)
docker run -d -p(or -publish) [localhost port]:[application port] [image name]:[tag] — This command runs the image
docker stop [container id]/[container name] — this command will exit the running container.
docker start [container id]/[container name] — this command will start a stopped container.
docker logs [container id] — it shows all the docker logs running in the background.
To give a container a name, when running it, add -name [custom container name] to docker run command:
docker run — name [custom container name] -d [image name]:[tag]
docker rm [container name]
Docker Registries — a storage where docker images are shared. The largest one is Docker Hub.
Containers are the isolated process that runs the image and the application.
Parent image includes the OS & sometimes the runtime environment
And the dependencies and source code can be put on top of parent image
Parent images can be found in dockerhub https://hub.docker.com/
Container Port vs Host Port
Once the container is running, the application inside the container is running in an isolated Docker network.
The same port can be run multiple times.
To access the container, the network will need to be exposed to local network via Port Binding.
Every application has a standard network port.
Public docker registries are the ones that are available for anyone. When company creates their own private images, those are private and not sharable to the public.
A docker registry is a service that provides storage, and it can have a collection of repositories.
A docker repository is a collection of related images with the same name but different versions.
Customized Docker Images
Dockerfile is the instruction of how to guide Docker how to build an image.
Dockerfiles start from a parent (base) image, the rest will be choosing tools we need for the project.
To use a base image, Dockerfiles must begin with a FROM instruction, and build this image from the specified image.
Every image consists of multiple image layers, it’s efficient since the image layers can be cached.
Before executing dependency installation commands, we can set root directory using WORKDIR, and copy and paste the source and configuration files into the root directory inside the container.
FROM — Build current image based on a specified image.
RUN — Execute commands in a shell inside the container environment.
COPY — copy and paste the source and configuration files inside the container.
WORKDIR — Set root directory of the container
CMD — The instruction that is to be executed when a Docker container starts, there can only be one CMD instruction in a Dockerfile.
After writing instructions, we can build our custom image by the command line
Also Docker needs to run in the background.
docker build -t [name]:[version] .