๐จโ๐ป Most of us have likely heard about Docker. This article aims to explain what Docker is and how it can aid in day-to-day coding. It will also discuss why companies and startups utilize Docker. We'll also provide essential Docker commands and guide you through the process of building a custom Docker image for your application. So, grab a โ and join me in exploring Docker.
summary
What is docker and how he can help me as a developer?
Basic docker commands
How do I build and run a Docker container from your app?
What is docker and how he can help me as a developer?
In simpler words, Docker is a tool that allows developers. to easily deploy their applications in a sandbox (called containers) to run on the host operating system. The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development. Unlike virtual machines, containers do not have high overhead and hence enable more efficient usage of the underlying system and resources, and that's the power of docker.
but as a developer, why do I need docker? and why does even docker exist?
well, the above meme can help you understand why Docker was even born.
When working in a team, if each developer only focuses on making their code work on their own machine, it will inevitably lead to issues when deploying to the production environment. This is because each developer may be using a different operating system (Linux, macOS, or Windows) and have different versions of local dependencies. Even when deploying to the cloud, the server may be set up with different dependencies. This is where Docker comes in to address this problem. By encapsulating all the code and dependencies within a container, Docker allows for consistent deployment regardless of the operating system, whether it's in a local development environment or in the cloud for production purposes.
and now, let's get our hands dirty, and start learning docker commands.
but before we start, please go and download Docker into your machine by following the steps mentioned on the official website here.
Basic docker commands
It's quite surprising, but you only need to learn a single command to build your Docker image and another command to run it. That's the beauty of Docker - it's incredibly easy to learn and use, so let's discover them.
First of all, every Docker command will start with the word "docker." To build an image from a Dockerfile (don't worry, we'll write our Dockerfile later) we use the "build" command. Additionally, we can use the "-t" option to tag your image and give it a specific label.
docker build -t yourusername .
And now, to run this Docker image, you simply need to execute 'docker run' and include some options. For example, the '--rm' option allows for the automatic removal of the container upon stopping it. Additionally, the '-it' flag specifies an interactive terminal, making it easier to terminate the container with Ctrl + C.
docker run --rm -it yourusername/image-name
How do I build and run a Docker container from your Express app?
As I mentioned above you need to add a Dockerfile file in your root directory and copy-paste the following file, I'll try to explain each command with a comment.
I will follow these steps with a simple Express application in my case.
# Use an official Node.js runtime as the base image
FROM node:14
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Download pnpm
RUN npm install -g pnpm
# Install app dependencies
RUN pnpm install
# Copy the rest of the app source code to the working directory
COPY . .
# Expose the port on which your Express app listens
EXPOSE 3000
# Start the Express app
CMD [ "pnpm", "start" ]
and now that we have a Dockerfile, we can build a Docker image using the following command:
docker build -t your-image-name .
Replace your-image-name
with the desired name for your Docker image. Don't forget the dot at the end, which represents the current directory.
Once the image is built, you can run a container based on that image using the following command:
docker run -p 3000:3000 -d your-image-name
This command maps port 3000 of the container to port 3000 of your local machine. Replace your-image-name
with the name you specified in the previous step.
Your application should now be running inside a Docker container. You can access it by visiting http://localhost:3000
your web browser.
Make sure your app is set to listen on port 3000 in its code.
In summary, there are a lot of reasons why people are using Docker:
Makes it easier to reliably build your application
Makes deploys easier and more reliable
Makes it easier to share and run a complex dev environment
Can easily run things like database servers without installation (traditionally, you could not!)
And most newer software projects have Docker somewhere in their process. Whether that is in their development environment, their production environment, or just in their CI servers, Docker is probably there.
And yeah, that's all ๐ค, I hope you enjoy reading this article! ๐ ,so feel free to explore and happy coding! ๐ป