Avoid Common Mistakes Using a Docker Container
Understanding Docker’s core concepts, particularly image creation, container management, and file relationships is crucial for leveraging its full potential. At the same time, careless use of Docker’s commands could increase your machine's memory significantly. This article delves into key aspects and represents how to create, stop, and restart node.js applications avoiding common mistakes.
Prerequisites:
- Node.js application: You can either create a new Node.js project or use one found on GitHub.
- Docker: Ensure you have Docker version 18.04 or later installed.
To work with Docker you need to add settings files:
- Dockerfile.
- compose.yaml (docker-compose.yaml in early Docker versions).
- .dockerignore (optional).
Creating an image:
The cornerstone of building a Dockerized Node.js application lies in the Dockerfile. This text file specifies the instructions Docker follows to create an image. Let’s dissect a basic example:
FROM node:18 # Base image with Node.js 18
WORKDIR /app # Working directory within the containerExplanation of Dockerfile lines:
FROM node:18: Defines the base image, inheriting the operating system and Node.js version (18 in this case).WORKDIR /app: Sets the working directory within the container, typically the root directory of your application.
Another possible commands, but we won’t use them in this article:
COPY package*.json ./: Copiespackage.jsonandpackage-lock.jsonfiles into the container's working directory.RUN npm install: Installs the dependencies listed inpackage.json.COPY . .: Copies the remaining application code from the current directory into the container's working directory.EXPOSE 3000: Exposes port 3000 from the container, allowing external communication with the application.CMD ["npm", "start"]: Sets the default command that runs when a container instance is created based on this image.
Building an image:
To create an image based on your Dockerfile, use the following command in your terminal, assuming your Dockerfile is named Dockerfile:
docker build .This command instructs Docker to use the current directory (.) as the build context (location of Dockerfile and application files).
Note: You should be careful with the command above because it creates a new image every time you run it.
You also may want to build the image without cache:
docker build --no-cache .Create a container :
While building images is essential, managing complex applications often involves multiple containers and services. Here’s where docker compose (docker-compose) comes in. It allows you to define and orchestrate multi-container applications using a YAML file called compose.yaml or docker-compose.yaml.:
version: '3.7'
services:
node-app:
build:
context: .
dockerfile: Dockerfile
target: base
volumes:
- .:/app
container_name: node-app
ports:
- '3001:3001'
command: sh -c "npm install && node ./build/index.js"Explanation of each command:
version: '3.7': Specifies the version of the Docker Compose file syntax being used. In this case, it's version 3.7.services:: Indicates the start of the services definition block.node-app:: Defines a service named "node-app." This service is essentially a containerized Node.js application.build:: Specifies the build configuration for the Docker image of the "node-app" service.
context: .: Sets the build context to the current directory, meaning the build process will use the current directory as the source for building the Docker image.dockerfile: Dockerfile: Specifies the Dockerfile to use for building the image. In this case, it is assumed that there is a file named "Dockerfile" in the build context.target: base: Sets the target build stage to "base." This is useful when the Dockerfile has multiple build stages, and it specifies which stage to use for the final image.
5. volumes:: Mounts a volume from the host machine to the "/app" directory inside the container.
- .:/app: Maps the current directory (".") on the host machine to the "/app" directory inside the container.
6. container_name: node-app: Specifies the name for the container, which will be "node-app."
7. ports:: Maps host machine ports to container ports.
- '3001:3001': Maps port 3001 on the host machine to port 3001 inside the container.
8. command:: Overrides the default command to be executed when the container starts.
sh -c "npm install -f && npm run tsc && node ./build/index.js": Runs a shell command within the container. The command installs npm dependencies (npm install) and finally executes the application (node ./build/index.js).
Note: Running node.js application using nodeinstead of pm2is recommended.
Running a Docker Container:
Once you have an image, you can create a container based on it using the docker compose up -d command.
Note: You should be careful with the command above because it creates a new container every time you run it.
If you want to restart exist container without creating a new one, you should stop it:
docker compose downand then use this command:
docker compose up --force-recreate --build -dMonitoring:
Let’s check what we have already done. The next command provides information about running docker instances:
docker system df
Note: You should monitor your created instances regularly. Increasing numbers of images, containers, caches, etc. could be the reason for filling your memory unexpectedly.
To see information of your active containers use:
docker ps
To see the container's real-time logs use the container ID:
docker logs --follow ec479385f3fdTo execute commands inside the container use:
docker exec -it ec479385f3fd sh -c "echo a && echo b"To remove all your inactive docker instances use:
docker system prune --volumes -afCaching:
Caching processes are really important in using dockers, but it is really a huge topic for other research. I recommend reading this material that covers most of the scenarios working with a cache.
Documentation:
Conclusion:
In summary, understanding Docker basics is crucial for getting the most out of Node.js apps. This article breaks down key Docker concepts like creating images, managing containers, and dealing with files. By carefully following Dockerfile and docker-compose setups, developers can create strong and scalable environments. Think of Docker images as the building blocks, perfectly wrapping up Node.js apps. Docker-compose is like the conductor, managing the complexity of your app setup. Just be cautious when using Docker commands to avoid using up too much memory. With insights on building images, creating containers, and keeping an eye on things, developers can smoothly use Docker to integrate Node.js apps into the world of containers.
Thank you for reading. If you have any questions you can find me on LinkedIn. Feel free to leave comments. You can also read my previous articles: