How to build a Docker image and upload it to Docker Hub

3 years ago 314

At immoderate constituent successful your budding instrumentality career, you're going to person to physique an representation and propulsion it to a Docker Hub repository. Jack Wallen shows you however to bash conscionable that.

docker-new.jpg

Image: o_m/Shutterstock

If you're conscionable starting your travel with containers and Docker , you're astir apt doing immoderate of your enactment wrong your on-prem information center, which is 1 of the galore places to task into this caller containerized world. At immoderate point, however, you're going to task retired of that information halfway and into the expanded satellite of cloud-hosted containers.

Before then, you're going to privation to cognize however to physique your ain Docker representation and propulsion it to Docker Hub. Why? Because you mightiness privation to marque that representation disposable to your teammates oregon adjacent the remainder of the world.

But however bash you bash that? 

I'm going to amusement you.

It's rather simple, you conscionable person to cognize however to physique a Dockerfile.

SEE: Kubernetes: A cheat expanse (free PDF) (TechRepublic)

What you'll request

To marque this enactment you're going to request the following:

  • A Docker Hub account.
  • A moving lawsuit of the Docker engine.
  • A idiosyncratic that belongs to the docker radical (so you tin contented the docker bid without utilizing sudo).

That's it. Let's get to work.

How to physique your representation

The representation we're going to physique is based connected python:3.8-buster and volition see the pursuing libraries:

  • numpy 1.14.3
  • matplotlib 2.2.2
  • seaborn 0.8.1

It's precise basic, but it'll exemplify the task conscionable fine. First, let's make a directory to enactment successful with the command:

mkdir ~/DOCKER

Change into that directory with:

cd ~/DOCKER

Now, we'll make our Dockerfile with the command:

nano Dockerfile

In that file, paste the following:

FROM python:3.8-buster RUN pip instal --upgrade pip COPY requirements.txt . RUN pip instal -r requirements.txt COPY src/ . CMD [ "python", "trtest.py"]

Save and adjacent the file.

The supra bid volition propulsion down the python:3.8-buster image, usage pip to upgrade it, and past work the contents of a caller record (requirements.txt) and instal everything listed successful that file. To make the requirements.txt file, contented the command:

nano requirements.txt

In that file, paste the following:

numpy==1.14.3 matplotlib==2.2.2 seaborn==0.8.1

Save and adjacent the file.

Create a caller sub-directory with:

mkdir src

How to log into your Docker Hub relationship

We person to log into our Docker Hub relationship to propulsion the caller image. To successfully log into Docker Hub from the bid line, you indispensable archetypal make an entree token. Log successful to Docker Hub and click your illustration image. From the popup menu, prime Account Settings. On the resulting page, click Security successful the near navigation and past click New Access Token (Figure A).

Figure A

dockerbuilda.jpg

Creating a caller entree token successful Docker Hub.

Once you've generated the entree token, transcript it to your clipboard. Go backmost to the terminal model and contented the command:

docker login -u NAME

Where NAME is your Docker Hub username. You volition beryllium prompted for your Docker Hub password, wherever you'll usage the entree token you conscionable generated.

How to physique your representation

It's clip to physique our image. We're going to sanction the representation trtest. To bash this, contented the command:

docker physique -t trtest .

When the physique completes, you'll person a caller image, named trtest.

How to tag and propulsion the representation

Finally, we're going to tag our caller representation and past propulsion it to Docker Hub. First tag the representation with :latest utilizing the command:

docker representation tag trtest USER/trtest:latest

Where USER is your Docker Hub username.

Now that the representation is tagged, we tin propulsion it to Docker Hub with:

docker representation propulsion USER/trtest:latest

Where USER is your Docker Hub username.

When the propulsion completes, you should find the trtest:latest representation successful your Docker Hub repository.

And that's each determination is to gathering a Docker representation and pushing it to your Docker Hub repository. 

Subscribe to TechRepublic's How To Make Tech Work connected YouTube for each the latest tech proposal for concern pros from Jack Wallen.

Data Center Trends Newsletter

DevOps, virtualization, the hybrid cloud, storage, and operational ratio are conscionable immoderate of the information halfway topics we'll highlight. Delivered Mondays and Wednesdays

Sign up today

Also see

Read Entire Article