Beginner’s guide to use docker (Build, Run, Push and Pull)

Deepak Shakya
6 min readApr 5, 2017

--

**This post is for newbies who wants to try out docker.

What is docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

I wanted to share in one quick flow on how to get started about using docker (build, run, tag and push). So here it is!

I will keep it short & sweet without much theoretical stuff. However I will point out to links where you can deep dive further into it.

Let’s begin

Download a stable docker version on Mac

https://download.docker.com/mac/stable/Docker.dmg

Install and Run Docker for Mac

  1. Double-click downloaded Docker.dmg file. Then drag and drop ‘Docker.app’ file into your applications folder.

2. To start running docker on your machine, click ‘Docker.app’ from your applications.

Click ‘Preferences’ to customise it further.

3. Quick check if docker is ready to use

Use below mentioned command on your mac shell (bash):

#docker ps

At this point, you won’t see any output except what shown above.

#docker version

4. Now it’s time to run our favourite “hello-world” on docker

#docker run hello-world

5. Run ‘docker ps -a’ to see all containers which are running on system.

Above verifies that our hello-world container is running fine.

Let’s get into some more action by running another example container

(Whalesay: An image for use in the Docker demo tutorial)

#docker run docker/whalesay cowsay Hello there!

The first time you run a software image, the docker command looks for it on your local system. If the image isn’t there, then docker gets it from the hub.

More details about whalesay: click here

Next, command to list all images on your local system

When you run an image in a container, Docker downloads the image to your computer. This local copy of the image saves you time. Docker only downloads the image again if the image’s source changes on the hub.

Big Step: Building your own docker image (excited!)

  1. Write a docker file
#mkdir mydockerbuild (create a directory named mydockerbuild)#cd mydockerbuild (go to directory created above)#vi Dockerfile (create a new docker file using vi editor)

(copy below mentioned contents into your Dockerfile)


FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

2. Build docker image

#docker build -t docker-whale .

If you see ‘no permission’ error, try to prefix above command with ‘sudo’ as shown in above snapshot.

3. Now if you can see your latest build using ‘docker images’ command

#docker images

4. Run your new docker-whale build

#docker run docker-whale

If you see above output then Hurray! You have done it! :)

If you are programmer or in some way related to coding or software world, I assume that you must be aware of GitHub. Similarly, there exists DockerHub.

What is DockerHub?

People all over the world create Docker images. You can find these images by browsing the Docker Hub. In this next section, you’ll search for and find the image you’ll use in the rest of this getting started.

What is Docker Store?

The Docker Store contains images from individuals like you and official images from organisations like RedHat, IBM, Google, Microsoft, and a whole lot more. Each image repository contains information about an image.

Create a Docker Hub account and repository

  1. Sign up for a docker hub account: In your web browser, go to the Docker Hub signup page.
  2. This account is also valid for docker store.
  3. Verify your email address and add a new repository:

Don’t worry about ‘docker-whale’. It’s coming next :)

4. Learn to ‘tag’ and ‘push’ image to your docker hub account.

We will use our docker-whale image to push to docker hub account which you created above.

4.1 Run ‘docker images’ command to list all images

Third column of above output shows you docker Image ID

We will use this Image ID to tag our docker-whale image

4.2 Tag the ‘docker-whale’ image using the ‘docker tag’ command and the image ID.

#docker tag <image-id of docker-whale> <your dockerhub username>/docker-whale:latest

Now if your use ‘docker images’ command to list images, you shall see your dockerhub username against docker-whale image as shown in above snapshot.

5. Push your tagged image to Docker Hub, using the ‘docker push’ command

6. Go back to the Docker Hub website to see the newly-pushed image.

7. Pull your new image from docker hub

7.1 Use ‘docker images’ to list all your local images

7.2 Let’s remove all versions of docker-whale image on our local system

#docker rmi -f <Image ID of docker-whale>

Use ‘docker images’ command to confirm if all instances of ‘docker-whale’ has been removed.

7.3 Now let’s run ‘docker-whale’ and see what happens.

When you use ‘docker run’ it automatically downloads (pulls) images that don’t yet exist locally, creates a container, and starts it. Use the following command to pull and run the ‘docker-whale’ image, substituting your Docker Hub username.

Conclusion

Here’s summary about what we covered:

  1. What is docker?
  2. Download docker image and get started on your machine
  3. A sample hello-world example
  4. How to build a new docker image?
  5. How to run a new docker image?
  6. How to tag, push and pull docker image?

Hope you find it useful.

--

--

Deepak Shakya

COO at Samudra Oceans (Climate-tech startup), Hatha Yoga teacher in London, U.K.