Some of Docker commands

When Docker engine installed, there are multiple commands comes with it in order to let one work with images in different ways.

Here I’m listing few commands and how to use them.

1. docker pull

It’s possible to just download the image from a registry for deploying in future by just running the “docker pull registry/image” command.

sudo docker pull registry/image

For example, one can run below command to get a light version of Nginx web server from Tutum repository.

sudo docker pull tutum/hello-world

2. docker run

This command could be very simple or complex depending on what’s the goal of action.

It’s possible to simply run “docker run registry/image” command to pull (if needed) an image and deploy a container from it with random NAME or make it a little more complicated and add name, hostname, resources, source and destination port for port mapping and etc.

sudo docker run registry/image

sudo docker run –name NAME –hostname INSTANCE_NAME -p Outside:Inside -v ServerData:InstanceDataMount registry/image

For deploying a new container from mentioned “Tutum/hello-world” image, it’s possible to use “docker run” in multiple ways but based on what described above I’m writing two below commands as a sample.

The difference is that the first command would deploy a container with a random name and no port mapping and no hostname while all of these parameters have been set on the second command.

sudo docker run tutum/hello-world

sudo docker run -d –name NAME –hostname CONTAINER_NAME -p 8080:80 tutum/hello-world

3. docker images

It’s possible to deploy multiple containers from one image or multiple containers from multiple images; it all depends on the environment and propose of the server.

In order to get the list downloaded images, it’s simply needed to run “docker images” and it’d show the list of the downloaded images name, size, tags and when they have been created.

sudo docker images

4. docker ps

This command would list all the deployed containers on the server. depending on the which option used with the command, it can list just running containers (first command), list all containers (second command), using filters to narrow down the result (last command).

sudo docker ps

sudo docker ps -a

sudo docker ps -a –filter ancestor=registry/image

5. docker stop

Stopping the container could be done using “docker stop” and container name (given during creation) or “Container ID”.

If a wait time is needed before stopping the instance it’s possible to use “-t” parameters with a number after it, which defines wait time in “Seconds“.

sudo docker stop NAME/Container_ID -t Num

In below example, the first line would stop “Kasraeian” instance after 10 seconds while the second line would stop any instance with Container ID of “e9ae7f3bbce6” after 20 seconds.

sudo docker stop Kasraeian -t 10

sudo docker stop e9ae7f3bbce6 -t 20

6. docker start

This command uses for starting already existed container on the server and just like “docker stop”, this command accepts both container name (given during creation) or “Container ID”.

sudo docker start NAME/Container_ID

7. docker rm

Removing containers could be easily done by using “docker rm” command. as for safety and prevention of accident, docker engine can’t remove running containers unless “-f” or “–force” option added to the command.

sudo docker rm NAME/Container_ID

sudo docker rm NAME/Container_ID –force

Access

Accessing the instances is different based on the container and its image, for example, some containers might provide only one service such as a database on one specific port while another container might provide multiple services on different ports.

For continuing with “Tutum/hello-world” sample, as it’s a web server with default configuration to provide service on port 80 and as port mapping used to map server port 8080 to container port 80, it’s possible to simply lunch a web browser and navigates to the server IP use 8080 for the port (for this example).

The left image shows original instance while the right image showing the modified instance.

Alias

As the system running Docker, multiple commands or action could be used frequently and as for that, I’ve added few aliases to the “.bashrc” in order to make things a little easier and faster for myself, hope you find it useful.

Running below lines would add three aliases for “docker ps”, “docker ps -a” and “docker images”.

echo “alias dll=’docker ps'” >> ~/.bashrc
echo “alias dla=’docker ps -a'” >> ~/.bashrc
echo “alias dli=’docker images'” >> ~/.bashrc

In the next posts, I’d show how to connect to a container and made some modification as well as sharing a small script I wrote a few months back.

It's your kindness to leave a reply/feedback