This guide tries to simplify the intricacies of docker and docker compose with the aim of enabling a solid grasp of the fundamentals for beginners.
What is Docker?
- Basically docker is a platform/tool that simplifies the development, running and shipping of applications.
- Docker achieves this by virtualizing the operating system of the computer on which it is installed and running.
What does Docker actually do?
- Docker provides the ability to package and run an application in an isolated environment.
Why do we need Docker in Developing Applications?
- Docker allows you to separate your applications from your infrastructure, thus enabling fast deployment of the applications.
Before diving deeper into docker, you can download docker from the following link, which provides guidelines for installation and running of docker on various operating systems: Download Docker
Docker Key Words/Components
Docker Engine/Daemon - Listens for requests from docker client(s), manages containers and coordinates docker operations.
Docker Client - Is a command line interface (CLI) through which users execute commands to manage their images and containers.
Docker Registry - Stores container images. An example is the Docker Hub
-
Dockerfile - A text that contains instructions describing how to build a docker image. Typically, a docker file contains the following instructions:
- FROM <image> : specifies the base image from which to build from.
- WORKDIR <path>: specifies the working directory.
- COPY<host-path><image-path>: Instructs the builder to copy files from host and place them into the container image.
- RUN<command>: Instructs the builder to run the specified command.
- CMD<command>, <arg1>: Sets the default command to be used by the container using this image.
A typical Dockerfile will look like this:
FROM python:3.10
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "main.py"]
Image(s) - An image is a blueprint/template of creating a container i.e. contains instructions for creating a container.
Container(s) - A container is an instance of an image. It contains everything required to run a particular application, including code, libraries, system tools etc.
To build the image and run a container from the Terminal, run the following commands:
docker build -t demo .
where -t denotes tag which identifies the name given your image i.e. demo.
docker run --name demo1 -p 5000:5000 demo
--name is the name of container to be run, and -p sets the port of the docker container to localhost.
- Other common commands include:
docker ps
- lists running containers
docker images
- lists local images
docker stop <id>
- stop running a particular container specified by its id.
docker -rm <id>
- removes a container.
What is Docker Compose?
Docker compose is a tool for defining and running multi-container applications, whereby these containers are managed in a single docker-compose.yaml
file.
So what's the difference between Docker and Docker Compose?
Docker builds, run and manages containers (containers package applications with their dependencies), whereas Docker Compose simplifies the management of multi-container applications using a singledocker-compose.yaml
file.
Key Components of Docker Compose YAML File
Version - Specifies the version of the docker compose file format.
Services - Specifies individual containers or services, making up an application. Each service may have a given docker image, environment variables, volumes, networks, ports to expose, and more options.
Volumes - Defines shared volumes that could be mounted into services to persist data. Volumes are crucial in storing data that survives container restarts or must be shared between many containers.
Networks - Defines custom networks for your services to communicate over. A network creates an isolated communications channel, and each service can have zero or more networks.
Top comments (0)