A container image represents a shippable software component that combines the necessary OS libraries, application middleware, and custom built software application. A container registry is used to store the container images created by developers or third party software vendors. Snyk helps developers to monitor container images for vulnerabilities and licensing issues.
In the prerequisites section you either created a Docker Hub account or had an existing account. In this section we will create a container image using the goof application and push the image to your Docker Hub repository.
To build the sample application goof, use the git CLI to clone the application to a directory on your laptop. Once the clone is complete change to the top-level of the goof directory.
git clone https://github.com/omearaj/goof.gitCloning into 'goof'...remote: Enumerating objects: 326, done.remote: Counting objects: 100% (326/326), done.remote: Compressing objects: 100% (177/177), done.remote: Total 326 (delta 156), reused 291 (delta 132), pack-reused 0Receiving objects: 100% (326/326), 248.40 KiB | 1.67 MiB/s, done.Resolving deltas: 100% (156/156), done.
Our next step is to use the docker CLI to create a container image. Issue the following docker command to build a container image.
docker build . -t omearaj/goof:latest
This command tells docker to build a container image starting with the current local path and the -t tells docker to tag the image using your Docker Hub Id, the name of the container image, and the version of your container image.
docker build . -t omearaj/goof:latestSending build context to Docker daemon 2.605MBStep 1/10 : FROM node:6-stretch---> ab290b853066Step 2/10 : RUN mkdir /usr/src/goof---> Using cache---> d5a950d3892dStep 3/10 : RUN mkdir /tmp/extracted_files---> Using cache---> 4ac499320dbdStep 4/10 : COPY . /usr/src/goof---> 125716c0e356Step 5/10 : WORKDIR /usr/src/goof---> Running in 2aab08235f0aRemoving intermediate container 2aab08235f0a---> 94a6d3655b4aStep 6/10 : RUN npm update---> Running in 5c484736e8bbRemoving intermediate container 5c484736e8bb---> 9f8f59a27dd7Step 7/10 : RUN npm install---> Running in 20de7a3e00d4Removing intermediate container 20de7a3e00d4---> d5f9ff28c53bStep 8/10 : EXPOSE 3001---> Running in 0d860ecd0cfcRemoving intermediate container 0d860ecd0cfc---> 8c9a5a3019daStep 9/10 : EXPOSE 9229---> Running in d66571a711cbRemoving intermediate container d66571a711cb---> f9685d053233Step 10/10 : ENTRYPOINT ["npm", "start"]---> Running in 4f2b68a0e20aRemoving intermediate container 4f2b68a0e20a---> 0bd35689e95eSuccessfully built 0bd35689e95eSuccessfully tagged omearaj/goof:latest
After the docker build command is complete you should have an instance of the goof:latest container image on your laptop prefixed with your Docker Hub Id. To list all of the images on your laptop you can issue a docker images command.
To push container images to Docker Hub we must authenticate with Docker Hub. Issue the docker login command listed below if you are not authenticated with Docker Hub.
docker loginLogin with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.Username: **********Password: **********Login Succeeded
Authenticated to Docker Hub, we can now issue a docker push command and docker will start to transfer your image to Docker Hub. Docker will start to transfer the new layers to Docker Hub.
docker push omearaj/goof:latestThe push refers to repository [docker.io/omearaj/goof]2cadc3ef8733: Pushed87dc5827675b: Pushed6b6bbc7297c7: Pushed7a9d9f5366d2: Layer already existseaa27aeae469: Layer already existsf39151891503: Layer already existsf1965d3c206f: Layer already existsa27518e43e49: Layer already exists910d7fd9e23e: Layer already exists4230ff7f2288: Layer already exists2c719774c1e1: Layer already existsec62f19bb3aa: Layer already existsf94641f1fe1f: Layer already existslatest: digest: sha256:a97c312ed48c543f5375947b882e3ed6156b8f5ee4126cd826e5a3cc050db703 size: 3054
Once the image has reached Docker Hub you can view it from the Docker Hub UI. You'll notice the container image in the Docker Hub UI is in a repository based on your Docker Hub Id.