Docker Build Local uses your machine’s resources to build container images. This is the traditional approach that works without requiring any subscription or cloud services.
To verify Docker is properly installed and running:
docker -v
If you see information about your Docker version without errors, you’re ready to proceed:
Docker version 27.3.1, build ce1223035a
Authenticate with GitHub using the pre-installed GitHub CLI:
# Check if already authenticated
gh auth status || gh auth login
If you need to authenticate, you will be guided through several prompts:
Instead of cloning directly, we’ll fork the repository first:
# Fork and clone the repository
gh repo fork aws-samples/Rent-A-Room --clone=true
# Change to the repository directory
cd Rent-A-Room
First, let’s create the Dockerfile:
cat << 'EOF' > Dockerfile
# Build stage
FROM node:12 as build
WORKDIR /app
ENV DISABLE_ESLINT_PLUGIN=true
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:1.14
COPY --from=build /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
EOF
Next, create the nginx configuration file:
cat << 'EOF' > nginx.conf
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
# Support for SPA routing
location / {
try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache";
}
# Serve static files
location /static/ {
expires 1y;
add_header Cache-Control "public";
}
# Handle all API requests
location /api/ {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
EOF
Create environment files to handle different deployment scenarios:
# Create development environment file:
cat << 'EOF' > .env.development
REACT_APP_API_URL=/proxy/3000
EOF
# Create production environment file:
cat << 'EOF' > .env.production
REACT_APP_API_URL=/api
EOF
Update both package.json and App.js to handle different environments:
# Add environment check before the return statement in App.js
sed -i '/function App() {/a\ const isVSCodeServer = window.location.href.includes('\''cloudfront.net'\'');\n const basename = isVSCodeServer ? '\''/proxy/3000/'\'' : '\''/'\'';' src/App.js
# Update Router to use basename
sed -i '/<[Rr]outer>/s/>/\ basename={basename}>/g' src/App.js
# Update package.json to add homepage
sed -i '/"private": true,/a\ "homepage": ".",' package.json
Dockerfile
?This Dockerfile uses a multi-stage build process to create an optimized production image for a Node.js application served by Nginx. Let’s break it down:
# Build stage
FROM node:12 as build
WORKDIR /app
ENV DISABLE_ESLINT_PLUGIN=true
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Production stage
FROM nginx:1.14
COPY --from=build /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Build the image using Docker:
# Measure build time
start_time=$(date +%s)
docker build -t rent-a-room .
end_time=$(date +%s)
echo "Build completed in $((end_time - start_time)) seconds"
Check that your image was built successfully:
docker images | grep rent-a-room
If built successfully you will see output like this:
rent-a-room latest 0b9c31de0251 3 seconds ago 111MB
Start a container using the image you built:
# Stop and remove container if it exists, then create a new one
docker stop rent-a-room-container 2>/dev/null || true
docker rm rent-a-room-container 2>/dev/null || true
docker run -d -p 3000:80 --name rent-a-room-container rent-a-room
Access your application in VS Code Server by going to the PORTS section, and click on the Forwarded Address.
This is what the application will look like:
For faster builds, especially in CI/CD environments, let’s use your Docker Hub account for remote caching:
# Use your Docker Hub username from the environment variable
echo "Using Docker Hub username: $DOCKER_USERNAME"
docker build \
--cache-from $DOCKER_USERNAME/rent-a-room:latest \
-t rent-a-room .
Now that you’ve built your image locally, let’s push it to Docker Hub so you can share it or use it in other environments:
# Tag the image with your Docker Hub username
docker tag rent-a-room $DOCKER_USERNAME/rent-a-room:latest
# Push the image to Docker Hub
docker push $DOCKER_USERNAME/rent-a-room:latest
Check that your image has been tagged correctly:
docker images | grep rent-a-room
You should see both your local image and the tagged image ready for Docker Hub:
$DOCKER_USERNAME/rent-a-room latest 0b9c31de0251 5 minutes ago 111MB
rent-a-room latest 0b9c31de0251 5 minutes ago 111MB
After pushing, you can verify that your image is available on Docker Hub by clicking on this link:
echo https://hub.docker.com/r/$DOCKER_USERNAME/rent-a-room
Or by navigating to Docker Hub in your browser and checking your repositories.
To stop the running container:
docker stop rent-a-room-container
To remove the container:
docker rm rent-a-room-container
You’ve completed the Docker Build Local section!
To continue with the workshop:
This ensures you follow the correct path and don’t duplicate the build process.
In the next section, we will explore how to utilize Docker Scout.