Docker Essentials: Building and Running Containers with Dockerfiles and Images
Docker is an open-source run-time for containers. It creates isolated containers on the same OS. A container packages all the required libraries and dependencies in one place. Containers are different from VMs in utilizing system resources. It makes the installation easy for various OS.
Docker utilizes the docker engine to keep track of images and containers. The Docker engine is also connected to the Docker-registry a place like GitHub for open-source and private images.
Images vs Container
A docker image is a lightweight, standalone single unit comprising source code, runtime, libraries, and dependencies to run an application.
A container is an isolated instance of a docker image. We can run multiple containers from a single image. Each container is running in an isolated environment.
Common Docker commands
First, install docker GUI or CLI in your OS to utilize docker functionalities.
- docker images: It lists all the docker images in your system.
2. docker ps: It will list all the running containers in your machine
3. docker run: docker run -d -p 3000:3000 mongo
-p lets you map the port
-d lets you run the container in detached mode
4. docker build: It lets you build an image from a docker file
Dockerfile
A dockerfile is a text document used to build an image of your application. Then you can push this image to the docker hub and create containers from this image.
领英推è
A dockerfile has two parts. One is a base image, usually an OS with runtime like node, golang, Python, etc. The other part is a bunch of commands to run an application.
Let's create a dockerfile to create image of some basic nodejs backend application
FROM node:20-alpine
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
RUN npx prisma generate
EXPOSE 3000
CMD ["node", "dist/index.js"]
WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY instructions that follow it.
RUN: Executes any commands in a new layer on top of the current image and commits the results.
CMD: Provides defaults for executing a container. There can only be one CMD instruction in a Dockerfile.
EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
COPY: Allow files from the Docker host to be added to the Docker image
Building images
Add a .dockerignore file to ignore copying files and folders like .env and node_modules in the docker image
docker build -t image_name .
docker images
docker run -p 3000:3000 image_name
Conclusion
Docker is very useful for setting up application in different OS. It saves us a hectic environment setup. With few commands, anyone can set up an application. In open-source projects, it is very beneficial to set up applications locally. It is a prerequisite for learning more advanced tools like Kubernetes
WordPress Website Designer & Developer | On-Page SEO Expert | Landing Page Designer | Local SEO | HMTL & CSS | Php
9 个月Very informative
Flutter | Firebase | Mobile App developer
9 个月Thanks for sharing