What are containers?
- Containers are a way of packaging applications together with all required dependencies and configuration, making the application easy to move, share, and deploy.
- Before containers, developers working on different operating systems had to manually install all required dependencies to run a project. This setup process could sometimes fail or cause inconsistencies between environments.
For example:
- "A React project requires Node.js and project-specific dependencies."
- With containers, once the project is packaged, only one tool is required — a container runtime such as Docker — which includes all necessary configurations and dependencies.
Container vs image
- Image – A packaged, read-only snapshot of an application with its dependencies and configuration.
- Container - Is the outcome of starting/running an image.
Docker vs Virtual Machine
- A OS is made up of the following layers :
- Application layer
- Kernel
- Hardware
Docker
- It virtualizes the OS Application layer resulting in small bundle size
- Services and apps bundled into a docker container run on top of the application layer
- As a result, it uses the kernel of the host machine as it lacks its own
- Docker containers use the Linux kernel. On macOS and Windows, Docker runs inside a lightweight Linux virtual machine.
Virtual Machine
- Virtual machines can run any operating system, regardless of the host OS, because they virtualize hardware.
Docker Installation
- Installation details for each os can be found from the docker website
Linux Installation Tip
- Once you've installed docker, to avoid having to use
sudoeach time you run a docker command, add your user to the docker group :
sudo usermod -aG docker ${USER}
Once done log out and log in again.
- Alternatively you could change the owner of the
/var/run/docker.sockfile to your user ie
sudo chown $USER /var/run/docker.sock
This approach however doesn't persist across restarts since when docker starts this file is recreated and ownership resets.
Common docker commands
1 . List out all images
docker images
2 . Start a container from an image
# Run in attached mode ie Ctrl+C stops the proces
docker run redis:latest
# Run in detached mode ie Ctrl+C does not stop the proces as it runs in the background
docker run -d redis:latest
# --name <someName> -> assigns a name to your container
# -p <exposedPort>:<internalPort> -> Used to specify that the app
# running in container at the port "<internalPort>" should be
# exposed to the host at this port "<exposedPort>"
docker run --name mongodb-4.4 -p 27017:27017 mongo:4.4
3 . List out containers and their metadata(container id, names etc)
docker ps # list only running containers
docker ps -a # list all container even ones that got stopped
4 . Stop container
docker stop <container-id>
5 . Start a stopped container
docker start <container-id>
Top comments (0)