Demystifying Docker - Part 1: Everything Developers Need to Know to Stay Ahead
Shokhrukh Karimov
SWE @ DSR Corporation | Ex SWE @ EPAM Systems | Javascript & React Enthusiast
Prerequisites
Verify your setup:
What is Docker?
At its core, Docker simplifies how developers build, ship, and run applications by using containers. Think of a container as a "lightweight, portable box" that includes everything your app needs—code, dependencies, and configurations.
Here’s how others define Docker:
?? Still unclear? Don’t worry. As we progress, you’ll have a definition that works for you!
Why Use Docker?
Let’s say you’ve built a cool Node.js project on your machine, and you want to share it with your friends. Simple, right? Not quite. Here’s why:
The Problem:
Different environments can lead to compatibility issues. Without Docker, your friends might struggle to run your project.
The Solution:
Docker allows you to package your app with everything it needs—Node.js, PostgreSQL, and even your OS — into a container. This container runs the same way, everywhere. Problem solved. ??
The Docker Advantage
Docker lets you create containers — lightweight, portable environments that include all your app’s dependencies. In our Node.js project example, Docker helps you:
Ready to dive in and create your first container? Let’s get started. ??
Let's play a bit with Docker
Note: Follow the steps and see the result. Don't worry we'll explore all the needed concepts later ??
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "<h1>Hello, Docker!</h1>"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
flask
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
P.s: We'll explore each line of commands later
docker build -t flask-docker-app .
Open your Docker Desktop app and go to the Images section, and you'll see an image
docker run --name flask-app -d -p 5000:5000 flask-docker-app
Open your Docker Desktop app and go to the Containers section, and you'll see a container running on port 5000
Docker concepts every developer should know
What is a Dockerfile?
A Dockerfile is a text-based document that's used to create a container image. It provides instructions to the image builder on the commands to run, files to copy, startup command, and more.
A Dockerfile is a text-based instruction manual used to create Docker images. It tells Docker:
In our project:
Docker Image
A container image is a standardized package that includes all of the files, binaries, libraries, and configurations to run a container. For a PostgreSQL image, that image will package the database binaries, config files, and other dependencies. For a Python web app, it'll include the Python runtime, your app code, and all of its dependencies.
A Docker Image is like a blueprint for creating containers. It includes:
In the example project, we created an image via the command:
领英推荐
docker build -t flask-docker-app .
Let's analyze each keyword here:
Now, let's learn a few commands related to Docker Images:
docker images
docker rmi <IMAGE-ID> or <IMAGE_NAME>:<IMAGE-TAG>
docker rmi $(docker images -q)
Docker Container
Simply put, containers are isolated processes for each of your app's components. Each component - the frontend React app, the Python API engine, and the database - runs in its own isolated environment, completely isolated from everything else on your machine.
A Docker Container is a running instance of an image. It:
Now, let's analyze how we ran our Image and created our final Container:
docker run --name flask-app -d -p 5000:5000 flask-docker-app
Let's have a look at some commands to work with containers:
docker ps
docker ps -a
docker stop <CONTAINER-NAME>
docker rm <CONTAINER-NAME>
docker rm -f <CONTAINER-NAME>
docker rm -f $(docker ps -aq)
Let's imagine how they are connected to each other!
Pushing a Docker Image to Docker Hub
Step 1: Docker Hub requires images to be tagged with a repository name in this format:
<your-docker-hub-username>/<repository-name>:<tag>
Note: If you haven't built an image yet, you can try this command to create an image that is ready to push:
docker build -t YOUR_DOCKER_USERNAME/flask-docker-app .
Step 2: In the example project above, we built an image and we should tag our image with our Docker username by the following command:
docker tag flask-docker-app YOUR_DOCKER_USERNAME/flask-docker-app:latest
Step 3: Now, our image is ready to get pushed to Docker Hub, let's do it
docker push YOUR_DOCKER_USERNAME/flask-app:latest
Step 4: Verify the Image on the Docker Hub
Step 5: Navigate to your repository to confirm the image is uploaded.
Conclusion
I hope you found this article informative and helpful. If you have any thoughts, questions, or suggestions, feel free to share them in the comments below. This is the first part of a series on Docker, and I’ve aimed to cover the fundamental concepts with clear explanations and visuals.
To make learning easier, here’s a link where you can review and practice the basic Docker commands. Remember, practice is the key to mastering anything.
Don’t hesitate to experiment — create your own images and containers using your existing applications or even new ones. If you have any questions during your exploration, I’ll be happy to help in the comments.
Thank you for reading, and I look forward to hearing about your Docker journey. Let’s keep learning and growing together! ??