Docker is a platform to manage the lifecycle of your containers. It is just one tool among many in the container ecosystem, there are other containerisation platforms and tools such as podman, LXC/LXD, CRIO etc.
The core technology behind docker is containerisation, which allows you to create lightweight and portable isolated environments where your applications can run easily anywhere with a container runtime.IN
⏰ Check it’s time for Containerisation? #
- 🤔 Do you need to run your app in different environment or on different operating system/hardware architectures?
- 🚀 Do you want to easily move your application between different environments (e.g. from development to production)?
- 🔒 Do you need to ensure that your application is isolated from other applications running on the same infrastructure with minimal system resource?
- 🔒 Do you need to ensure that your application is isolated from other applications running on the same infrastructure?
- 🔄 Do you need to ensure that your application runs consistently across different environments and configurations?
- ⏰ Do you want to reduce the time and effort required to set up and manage your application environment?
- ☁️ Do you need to ensure that your application can be easily and reliably deployed to cloud infrastructure, regardless of the cloud provider let to be AWS, Azure, Google Cloud, Digital Ocean, Heroku or anything else..
- 💪 Do you need to ensure that your application is highly available and fault-tolerant?
- 👥 Are planning to work as team for the project and scale your application in future?
- 🆕 Do you want to be able to easily roll out updates or changes to your application?
These are some question you should ask yourself before containerising an app, there is no need to containerise everything. 💡
Containerising stateful applications like databases, it is generally not recommended. This is because stateful applications typically require persistent data storage, which can be more challenging to manage within a containerised environment. Containers are designed to be stateless.
🛠️ Prerequisites #
- Basic knowledge of programming
- Familiarity with command-line interface (CLI) and Git
- Familiarity with cloud platforms
- Understanding of networking and security
📥 Installation and Setup #
🐧 Linux #
curl https://get.docker.com | bash
🪟 Windows #
🍎 Mac #
💡 Learning Session #
1. How docker works? #
🎓 Topics to Learn #
- Basic idea of containerisation technology
- Containerisation vs Virtualisation
- Networking, Sockets, Namespaces, Cgroups & Union FIlesystems (Optional)
🧑🏻💻 Learn from #
2. 📦 Containerise and 🏃♀️run app using Docker #
2.1 Create a Docker file #
A Dockerfile is a simple text file that contains a list of commands that the Docker client calls while creating an image. It’s a simple way to automate the image creation process.
FROM python:3.9-slim-buster WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["flask", "run", "--port=80"]
2.2 Build Docker Image #
Read-only templates that contain the application code and dependencies required to run the application. Docker images are created using a Dockerfile.
It allows developers to package their application along with its dependencies, libraries, and configurations into a single image. This image can then be easily shared and deployed on any machine that has Docker installed.
docker build -t website:1.0 .
Note: Dockerfile should be named
Dockerfile, and it should be located in the current directory.
2.3 Push to Container Registry(Optional) #
A container registry is similar to a container repository, in that it allows us to push code like we would with version control systems such as Github. Docker Hub is one of the most popular container registries.
Note: If you are using GitHub, I recommend using the GitHub Container Registry. It can be easily integrated with GitHub Actions, making it easier to build your DevOps pipeline.
docker image tag <app name>:latest <registry host>/<repository name>/<app name>:<version> docker image tag website:1.0 ghcr.io/user-name/website:1.0
docker push <registry host>/<repository name>/<app name>:<version> docker push website:1.0
📽️ Videos Container Registry Explained
2.4 Pull from Container Registry(Optional) #
Note: This step is required if you want to run someone’s image or an image you pushed on your computer or another platform.
It is not necessary to pull from the registry; you can take the build from the Dockerfile and run the created image. However, this is not the best practice and can take time to build.
docker pull <registry host>/<repository name>/<app name>:<version>
docker pull website:1.0
2.5 Run Container #
docker run -d <registry host>/<repository name>/<app name>:<version> # My app needs to listen on port 80(-p) and run in the background(-d). docker run -d -p 80:80 website:1.0
2.6 Check running Containers #
You can check the running containers with “docker ps” command
You can run a multi-container application in a single file using Docker Compose, which is a simple orchestration tool.
Projects you need try #
- Run a static website using nginx in container
- Containerise and run a stateless flask app
- Build and Dockerize a Full-stack React app with Node.js, MySQL and Nginx