How to build a Docker image and upload it to Docker Hub
Muhammad Latif
Chief Technology Officer | Blockchain & Cloud Innovator | Driving Digital Transformation & Web3 Excellence ??
This statement introduces a guide for beginners using Docker and containers. It explains that while you might start by working with containers in your own data center, eventually you'll need to work with cloud-hosted containers. The guide will cover how to create your own Docker image and upload it to Docker Hub, which is important for sharing it with others.
What you’ll need
To make this work you’re going to need the following:
A Docker Hub account.
A running instance of the Docker engine.
A user that belongs to the docker group (so you can issue the docker command without using sudo).
How to build your image
The image we’re going to build is based on python:3.8-buster and will include the following libraries:
numpy 1.14.3
matplotlib 2.2.2
seaborn 0.8.1
First, let’s create a directory to work in with the command:
Step 1:
Step 2: Change the directory
?????????? ?cd docker-document
Step 3: Now, we’ll create our Dockerfile with the command:
???????????? New-Item -Path . -Name “Dockerfile” -ItemType “file”
New-Item -Path . -Name “Dockerfile” -ItemType “file”
Step 4: notepad Dockerfile
notepad Dockerfile
paste the following: on dockerfile
FROM python:3.8-buster
RUN pip install --upgrade pip
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY src/ .
CMD [ "python", "trtest.py"]
Save and close the file.
Step 5: Now, we’ll create our requirements.txt file with the command:
??????????? New-Item -Path . -Name “requirements.txt” -ItemType “file”
New-Item -Path . -Name “requirements.txt” -ItemType “file”
Step 6: Using this command edit the above file,
notepad requirements.txt
领英推荐
notepad requirements.txt
In that file, paste the following:
?????????? numpy==1.14.3
?????????? matplotlib==2.2.2
?????????? seaborn==0.8.1
?? Save and close the file.
Step 7: Create a new sub-directory with:
mkdir src
mkdir src
How to log into your Docker Hub account
To push a new image to Docker Hub from the command line, you'll first need to log in using an access token. Start by logging into Docker Hub, then go to Account Settings via your profile image. Navigate to the Security section and create a New Access Token.
Once you’ve generated the access token, copy it to your clipboard. Go back to the terminal window and issue the command:
docker login -u NAME
Where NAME is your Docker Hub username. You will be prompted for your Docker Hub password, where you’ll use the access token you just generated.
How to build your image
It’s time to build our image. We’re going to name the image tested-image. To do this, issue the command:
docker build -t tested-image.
When the build completes, you’ll have a new image, named tested-image.
How to tag and push the image
To tag our new image and then push it to Docker Hub.
First tag the image with :latest using the command:
docker image tag tested-image USER/tested-image:latest
Where USER is your Docker Hub username.
Now that the image is tagged, we can push it to Docker Hub with:
docker image push USER/tested-image:latest
Log in to?Docker Hub?and click your profile image.