In this lesson, we will learn how to create, manage, and run containers using Docker. Docker is a platform that allows developers to automate the deployment of applications inside lightweight containers. Containers are isolated environments that package an application and its dependencies, ensuring that it runs consistently across different computing environments.
Docker is an open-source platform that enables developers to build, ship, and run applications in containers. Containers are similar to virtual machines but are more lightweight and share the host operating system's kernel.
To get started, you need to install Docker on your machine. Follow the official Docker installation guide for your operating system.
To create a Docker image, you need to write a Dockerfile. Here’s a simple example of a Dockerfile for a Node.js application:
# Use the official Node.js image as a base
FROM node:14
# Set the working directory
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the application port
EXPOSE 3000
# Command to run the application
CMD ["node", "app.js"]
To build the image from the Dockerfile, run the following command in your terminal:
docker build -t my-node-app .
Once the image is built, you can run a container using the following command:
docker run -p 3000:3000 my-node-app
This command maps port 3000 of the container to port 3000 on your host machine.
You can list all running containers with:
docker ps
To stop a running container, use:
docker stop <container_id>
To remove a container, use:
docker rm <container_id>
Always use specific version tags for images. This helps ensure that your application runs consistently across environments.
Keep your Dockerfile clean and organized. Use comments to explain each part of the Dockerfile for better readability.
With this foundation in Docker, you are now ready to explore how Kubernetes orchestrates these containers in the next lessons.
Dockerfile and copy the provided Dockerfile content into it.app.js) that listens on port 3000 and returns 'Hello World'.http://localhost:3000.app.js to return a JSON response instead of plain text.docker ps, docker stop, and docker rm.