🐋Docker
Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Docker file.
🐋Docker file
A Docker file is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Docker file might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.
✔Task
Create a Dockerfile for a simple web application
# Use official Node.js image as base
FROM node:14
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to container
COPY package*.json ./
# Install app dependencies
RUN npm install
# Copy the rest of the application code to container
COPY . .
# Expose port 3000 to the outside world
EXPOSE 3000
# Command to run the application
CMD ["node", "app.js"]
Build the Image and Run the Container
To build the Docker image, navigate to the directory containing the Dockerfile and run:
docker build -t my-node-app . #command for build the Docker Image
Run the container using:
docker run -d -p 3000:3000 my-node-app
This command runs the container in detached mode (-d
), exposing port 3000 of the container to port 3000 on your host system (-p 3000:3000
).
To verify that the application is working, you can open a web browser and navigate to http://localhost:3000
.
Push the image to a public or private repository (e.g. Docker Hub )
To push the image to a Docker repository, first, you need to tag the image appropriately with your repository name. For example:
docker tag my-node-app yourusername/my-node-app
Then, you can push the tagged image to the repository:
docker push yourusername/my-node-app
Replace yourusername
with your Docker Hub username or the name of your private repository. This will push the image to Docker Hub or your private repository.
Happy learning😊