This section details the steps necessary to build and start the Docker-based project using JavaScript, HTML, CSS, and shell scripting.
Step 1: Clone the Repository
Begin by cloning the GitHub repository for the project. This command will copy the project files to your local machine.
git clone https://github.com/docker/getting-started.git
Change your current directory to the project folder:
cd getting-started
Step 2: Inspect the Dockerfile
Open the Dockerfile
located in the project root. This file contains the instructions for building the Docker image.
Example of a Dockerfile
# Use Node.js base image
FROM node:14
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install the application dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the application port
EXPOSE 8080
# Start the application
CMD ["npm", "start"]
Step 3: Build the Docker Image
Once you have reviewed the Dockerfile and confirmed that it includes the necessary setup, you can build the Docker image. Use the following command:
docker build -t getting-started .
This tells Docker to build an image named getting-started
using the Dockerfile in the current directory.
Step 4: Run the Docker Container
After the image is built, run the Docker container. The command below will start the container from the built image and map port 8080 in the container to port 8080 on your host.
docker run -d -p 8080:8080 getting-started
The -d
flag runs the container in detached mode, meaning it runs in the background.
Step 5: Verify the Application is Running
To confirm that the application is running, open a web browser and navigate to http://localhost:8080
. You should see the application interface.
Step 6: Check Docker Container Logs
If the application does not appear to be working as expected, checking the logs can help diagnose the issue. Use the command below to view the logs from the running container:
docker logs <container_id>
You can obtain the container_id
by listing all running containers:
docker ps
Step 7: Stopping the Docker Container
To stop the running Docker container, use the following command:
docker stop <container_id>
Replace <container_id>
with the actual ID of your container, which you can find using docker ps
.
Step 8: Cleaning Up
If you want to remove the stopped container and the image to free up space, you can run the following commands:
Stop and remove the container:
docker rm <container_id>
Remove the Docker image:
docker rmi getting-started
This concludes the setup process for building and starting the project using Docker.
Source: docker/getting-started