How to Publish Images in Docker | DevOps Tutorial | VisualPath

Publish the Image

Upload your tagged image to the repository:
 docker push username/repository:tag  
Once complete, the results of this upload are publicly available. If you log in to Docker Hub, you will see the new image there, with its pull command.

Pull and run the image from the remote repository 
From now on, you can use docker run and run your app on any machine with this command:
docker run -p 4000:80 username/repository:tag  

If the image isn’t available locally on the machine, Docker will pull it from the repository. 
$ docker run -p 4000:80 john/get-started:part1
Unable to find image 'john/get-started:part1'locally
part1: Pulling from orangesnap/get-started
10a267c67f42: Already exists
f68a39a6a5e4: Already exists
9beaffc0cf19: Already exists
3c1fe835fb6b: Already exists
4c9f1fa8fcb8: Already exists
ee7d8f576a14: Already exists
fbccdcced46e: Already exists
Digest: sha256:0601c866aab2adcc6498200efd0f754037e909e5fd42069adeff72d1e2439068
Status: Downloaded newer image for john/get-started:part1
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)  
Note: If you don’t specify the tag portion of these commands, the tag of the latest will be assumed, both when you build and when you run images. Docker will use the last version of the image that ran without a tag specified (not necessarily the most recent image).
No matter where docker run executes, it pulls your image, along with Python and all the dependencies from requirements.txt, and runs your code. It all travels together in a neat little package, and the host machine doesn’t have to install anything but Docker to run it.
Dockerfile Instructions We have seen in the previous section that Dockerfile is used to build docker images. It contains the list of instructions that docker reads to set up an Image. There is around a dozen Instruction that we can use in our Dockerfile.
FROM
This instruction is used to set the base image for subsequent instructions. It is mandatory to set this in the first line of a Dockerfile. You can use it any number of times though.
Example:
FROM ubuntu: latest  
MAINTAINER
This is a non-executable instruction used to indicate the author of the Dockerfile.
Example: 
MAINTAINER <'name'>  
RUN
This instruction lets you execute a command on top of an existing layer and create a new layer with the results of command execution. For example, if there is a pre-condition to install PHP before running an application, you can run appropriate commands to install PHP on top of the base image (say Ubuntu) like this: 
FROM ubuntu
RUN apt-get update update apt-get install php5  
CMD
The major difference between CMD and RUN is that CMD doesn’t execute anything during the build time. It just specifies the intended command for the image. Whereas RUN executes the command during build time.
Note: There can be only one CMD instruction in a Dockerfile if you add more, only the last one takes effect.
Example: 
CMD "echo""Hello World!"  
EXPOSE
While running your service in the container you may want your container to listen on specified ports. The EXPOSE instruction helps you do this.
Example: 
EXPOSE 6456  
ENV
This instruction can be used to set the environment variables in the container.
Example: 
ENV var_home="/var/etc"  
COPY
This instruction is used to copy files and directories from a specified source to a destination (in the file system of the container).
Example: 
COPY preconditions.txt /usr/temp  
ADD
This instruction is similar to the COPY instruction with few added features like remote URL support in the source field and local-only tar extraction. But if you don’t need an extra feature, it is suggested to use COPY as it is more readable.
Example: 
ENTRYPOINT
You can use this instruction to set the primary command for the image.
For example, if you have installed only one application in your image and want it to run whenever the image is executed, ENTRYPOINT is the instruction for you.
Note: arguments are optional, and you can pass them during the runtime with something like docker run <'image-name'>. Also, all the elements specified using CMD will be overridden, except the arguments. They will be passed to the command specified in ENTRYPOINT.
Example:
CMD "Hello World!"ENTRYPOINT echo  
VOLUME
You can use the VOLUME instruction to enable access to a location on the host system from a container. Just pass the path of the location to be accessed.
Example: 
VOLUME /data  
USER
This is used to set the UID (or username) to use when running the image.
Example: 
USER daemon  
WORKDIR
This is used to set the currently active directory for other instructions such as RUN, CMD, ENTRYPOINT, COPY and ADD. Note that if a relative path is provided, the next WORKDIR instruction will take it as relative to the path of previous WORKDIR instruction.
Example:
WORKDIR /user
WORKDIR home
RUN pwd  
This will output the path as /user/home.

ONBUILD
This instruction adds a trigger instruction to be executed when the image is used as the base for some other image. It behaves as if a RUN instruction is inserted immediately after the FROM instruction of the downstream Dockerfile. 
This is typically helpful in cases where you need a static base image with a dynamic config value that changes whenever a new image must be built (on top of the base image).

Example:
ONBUILD RUN rm -rf /usr/temp  
Docker hub has Dockerfile for every official Image that’s hosted there.
Above screenshot is from nginx official repository from Docker hub. If you see there are links to Dockerfile for every version of the image. The links point to Dockerfile hosted in GitHub. 

These Dockerfiles are the best place to learn the Dockerfile as its verified from Docker Inc.
One of the best practice is that we combine multiple commands in RUN instruction by using && Every RUN instruction creates a layer on the images, so if you have ten RUN instruction that creates ten extra layers in your image.

To avoid this, we can write all the commands in single RUN instruction by combining all the commands with && as shown in above screenshot. 

A sample MongoDB Dockerfile.

##########################################################
# Dockerfile to build MongoDB container images
# Based on Ubuntu
##########################################################
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER Example McAuthor
# Update the repository sources list
RUN apt-get update
################## BEGIN INSTALLATION ######################
# Install MongoDB Following the Instructions at MongoDB Docs
# Add the package verification key
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
# Add MongoDB to the repository sources list
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen'| tee /etc/apt/sources.list.d/mongodb.list
# Update the repository sources list once more
RUN apt-get update
# Install MongoDB package (.deb)
RUN apt-get install -y mongodb-10gen
# Create the default data directory
RUN mkdir -p /data/db
##################### INSTALLATION END #####################
# Expose the default port
EXPOSE 27017
# Default port to execute the entrypoint (MongoDB)
CMD ["--port 27017"]
# Set default container command
ENTRYPOINT usr/bin/mongo
For more information about Visualpath, visit www.visualpath.in and follow the company on Facebook and Twitter.

Comments