Getting started with Docker command line

November 4th, 2016 by Alex Leave a reply »

Docker is a nice virtualization and “containerization” tool, which allows you to deploy different services on any machine regardless of hardware the service was developed on, and regardless where it is deployed.

Let’s take a look at the common commands we can use to play around. In this post I assume that you installed docker.

So first, the Docker Hub is located at – this is the place where you can find ready docker containers to use for your projects or to derive from.

We will start with a simple Ubuntu version 16.04 in a docker.

docker run -it ubuntu:16.04 bash

If everything was fine, you will see something like

Unable to find image 'ubuntu:16.04' locally
16.04: Pulling from library/ubuntu
6bbedd9b76a4: Pull complete
fc19d60a83f1: Pull complete
de413bb911fd: Pull complete
2879a7ad3144: Pull complete
668604fde02e: Pull complete
Digest: sha256:2d44ae143feeb36f4c898d32ed2ab2dffeb3a573d2d8928646dfc9cb7deb1315
Status: Downloaded newer image for ubuntu:16.04

This means everything was fine and now we are in bash of Ubuntu 16.04. So what we did:

docker run is the command to run an image, -it means we want to allocate a pseudo-tty and keep the shell listening for our input. Next, ubuntu:16.04 is the name of image to run, and bash is the command to run instantly after the container is up.

Let’s continue discovering.

docker run -d -p 27017:27017 --name database mongo

Here we run the latest mongo image, expose the container port with parameter -p 27017 to host port 27017, run it in background with -d and name the container database. But how to connect to that MongoDB database? What’s the URL? Since we are using dockers, we need to get the IP address of the docker machine. If you have docker-machine installed, run

docker-machine ip <name>

The result might look like or something like that. Of course, replace <name> with your docker-machine name (you can have multiple different names). If you have older boot2docker instead of docker-machine, then it’s boot2docker ip

Now we can connect to our container by using that IP + the port 27017.


Now we can play with real mongo database and not worry about breaking your main OS. When you want to run the fresh MongoDB again, start and remove the container:

docker stop database
docker rm database

This should totally remove the container. If you run the last docker run command again, you will notice how fast the container was created – because the image is already there and running a whole new database takes a mere second only. That’s the beauty of Docker.

To see a list of containers running, use

docker ps

If some container died or was stopped, you can see it if you run

docker ps -a

To stop all containers, you can use bash arrays like this

docker stop $(docker ps -q)

You might notice the new -q parameter – this means “quiet” and it will output only the container names. If you want to remove all containers, the command will be

docker rm $(docker ps -qa)

Here, -a is for all, and -q is for quiet.

Another important feature is folder mounting. This allows us to expose the host folder to container, moreover the folder changes inside the container will be reflected in the host OS. This makes it possible to easily deploy host files (say, git checkout a repository and run a server in one step). Consider this more advanced command:

docker run -p 80:80 -p 443:443 -v `pwd`:/var/www/html -d --name=apache eboraas/apache

So let’s see what happens here. First, we will expose port 80 (for http) and port 443 (for https) from container to host, then we will mount the current working directory with parameter -v (`pwd` will get resolved to current working dir when you run it). The mounting parameter has a format of /hostpath:/containerpath and paths can’t be relative. -d will run container in detached mode, which allows us to continue using the terminal after this. If you run this in a folder with file named index.html, and then navigate browser to the IP address of the docker machine (in your case it was, you should see the contents of index.html served by apache that we just deployed in 1 single line. That’s docker magic.

So that should get you up and ready with the basics. Now go to the hub, explore the public containers and try to accept the fact that it is so easy to create wonderful stuff nowadays 🙂

Leave a Reply