Recently, I began learning Docker from scratch. I found that most tutorials only explain how to set up everything in a single container since it’s simpler, but they rarely teach you how to set up a standalone container. This tutorial will cover those missing details.
It took me a few hours to create my own container, but for standalone containers, I watched a bunch of tutorials, but nothing was useful. Now, let’s talk about what most tutorials miss about the Docker container.
Dockerfile is only required when you want to use a custom image
This thing 99% tutorial doesn’t teach you that the Docker container only needs a Dockerfile when you want to use your custom image, the custom image would be your NextJS and Django project, only if you want to run the project inside the Docker container.
In that case, you need the Dockerfile since you need to install all the dependencies to run the NextJS and Django project. Also, you need to add some custom commands so that the project will automatically start your Docker container.
Running Standalone Image
To run a standalone image inside the Docker containers, you don’t need a Dockerfile. If the container image is already available in the Docker Hub, you can just copy the image's name from Docker Hub, and it will pull the image from Docker Hub when you run the build command.
To access the Docker container outside the container, you need to create a shared network so that the Docker container can communicate from outside the project.
How to Create a Shared Network for External Projects
To set up a shared network, add the network definition inside the Docker services section.
networks:
- shared-infra
After that, you need to run this command in the terminal,
docker newtwork create shared-infra
which will create a shared network so that the outside network can communicate with the Docker container.
Just take an example, your Postgres database running inside the Docker container, and you want to connect with your NextJS, which is not inside the Docker container, then you have to follow what I have shared.
Also, you need to define the network external: true in the bottom, otherwise you will get an error when you run the docker compose up command.
networks:
shared-infra:
external: true
Create a shared Network between containers.
Let’s suppose you created a standalone container, then you want to utilise it in the other container, just mention this thing in the service.
networks:
- ecommerce_network
Also, you need to define the network as a bridge. so that they can communicate with each other.
networks:
ecommerce_network:
driver: bridge
In this case, you don’t need an external true since we need a shared network within the container. Also, you don’t need to run any command in the terminal.
Postgres doesn’t require a Dockerfile
If you want to set up the Postgres database inside the Docker container, then you don’t need a Dockerfile since the image is already available on Docker Hub. You can just copy the image's name. Which version would you like to use? I would recommend using Postgres 17 since it’s stable. Still, if you want to set up the Postgres database 17 without the Docker container, then Here is the Source.
But I would not recommend using the Postgres database inside the Docker container for production since it is hard to manage, and only good for development. Postgres on Docker containers only makes sense when you need a development environment in your virtual private server for testing.
If you wanted to see how it is so easy to migrate and dump the PostgreSQL database without a Docker Container, then Here is the Source.
You can Create Multiple Docker Compose files
This thing most tutorials don’t tell you about, you can create multiple docker compose wether one for production and a second for the development environments, but when you run Docker, you need to mention the name, let’s take an example.
Let’s suppose I have this Docker docker-compose.yml for the production environment, then I need to mention the file name when running the docker up command.
docker compose -f docker-compose.yml up -d
And for the development environment, I need to use docker-compose.dev.yml
docker compose -f docker-compose.dev.yml up -d
These are all the important concepts that people rarely talk about, and if you want to see how you can create a shared network in a Docker container and get a completely beginner-friendly guide, then Here is the Source.
And that’s pretty much it for this tutorial. I will see you in the next one.
Top comments (0)