9.1 Publishing Images: the docker push
Command
You probably already know that Docker Hub is a cloud platform for storing, sharing, and managing Docker images. Developers can easily share their images with coworkers or publish them for a wider audience. Working with Docker Hub is kinda similar to working with Git. The main commands are docker push and docker pull. In this lecture, we’ll talk about how to use these commands to publish and download Docker images.
The docker push
Command is used to upload local Docker images to Docker Hub. Before publishing your image, make sure it’s tagged with your Docker Hub username.
Steps to Publish an Image
Step 1. Build a Docker Image:
If you don’t already have a built image, create one with the docker build
command.
docker build -t myapp:latest .
Step 2. Tag the Image:
To publish the image to Docker Hub, you need to tag it with your Docker Hub username.
docker tag myapp:latest yourusername/myapp:latest
Step 3. Publish the Image:
Use the docker push command to upload the image to Docker Hub.
docker push yourusername/myapp:latest
Example:
docker build -t myapp:1.0 .
docker tag myapp:1.0 yourusername/myapp:1.0
docker push yourusername/myapp:1.0
9.2 Pulling Images: docker pull
Command
The docker pull
command is used for pulling images from Docker Hub to your local machine. This lets you access images published by other users or your team.
Steps to Pull an Image
Step 1. Search for an image:
Use the docker search
command to find the image you need on Docker Hub.
docker search nginx
Step 2. Pull the image:
Use the docker pull
command to download the image to your local machine.
docker pull yourusername/myapp:latest
Example:
docker pull nginx:latest
9.3 Full Example
A full example of the workflow for publishing and pulling images:
Step 1: Building the Image
Create a Dockerfile for your app. For example, for a Node.js app:
# Use a Node.js base image
FROM node:14
# Set the working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the app code
COPY . .
# Expose a port
EXPOSE 3000
# Run the app
CMD ["node", "app.js"]
Build the image using the docker build
command.
docker build -t mynodeapp:latest .
Step 2: Tagging the Image
Tag the image with your Docker Hub username.
docker tag mynodeapp:latest yourusername/mynodeapp:latest
Step 3: Publishing the Image
Push the image to Docker Hub.
docker push yourusername/mynodeapp:latest
Step 4: Pulling the Image
Now another user or a teammate can pull this image to their local machine.
docker pull yourusername/mynodeapp:latest
Step 5: Running a Container from the Pulled Image
After pulling the image, you can start a container based on it.
docker run -d -p 3000:3000 yourusername/mynodeapp:latest
GO TO FULL VERSION