Category Archives: docker

Basic Docker command set

Docker is a container platform. Docker is very useful (and fast compared to virtual machines) for isolating different programs, data pipelines, etc., especially if they depend on different packages. A Docker container is an isolated environment that runs on a shared operating system. A Docker image is a container snapshot saved on disk.

There are many tutorials how to use Docker containers. Here are my notes how to get started with Docker.


Install Docker as described in Docker’s homepage.

On Ubuntu, it’s as easy as running

curl -fsSL | sudo bash


Run the Docker daemon on Windows or Mac, type docker in the terminal to make sure all is good. Now we can try to create a Docker container.

First you need to get a Docker image with your intended software on it. The easiest way is to download a ready made image from Docker Hub. There you can find the most popular configuration/setups that people have submitted as images.

To download an image to your computer you use pull. 

# get the latest version of ubuntu distribution
docker pull ubuntu
# get a specific version
docker pull ubuntu:xenial

Then use run to create a running container from the image.  run  will also pull the image if you don’t have it. So you can also skip pull.

# run the image and give it something to do (a command)
docker run --name my_server ubuntu:xenial sleep 100
# run a database and name the container
docker run --name my_db postgres
# run a database in the background (detach)
docker run -d --name my_db postgres

Check out the list of containers in your computer:

# see all running containers
docker ps
# see all running and stopped containers
docker ps -a

When the Docker instance has  finished the command you provided, it will stop. This state is like a computer switched off. You can rerun the container with start. It will execute the same command as before and you use -a (attach) flag to see the output.

# start the stopped container again with previous cmd
docker run -a my_server

If your container is running a database or some kind of continuous service then it will not stop unless you explicitly stop it.

# start a container with postgres DB, give it a name
docker run -d --name my_db postgres 
# stop a running container
docker stop my_db

Interactive mode

If you want to open a terminal inside the container and write commands there then you have to run an image using  interactive mode requiring flags -i (interactive – keeping listening to input (stdin)) -t (open a terminal interface). The default command in the ubuntu images is /bin/bash.

# both are the same for ubuntu
docker run -it ubuntu
docker run -it ubuntu /bin/bash

Type exit to exit the container. After this the container will stop too. If you want to keep your container running the add -d (detach) flag – this sends it to background. Now, to attach to this container you use exec. This executes commands on a running container. So now to attach to it you add interactive flags and a command to run when inside (bash terminal).

docker run -i -t -d --name my_server ubuntu 
# attach to running container, exit won't kill it
docker exec -i -t my_server /bin/bash
# stop the container afterwards
docker stop my_server

You can add flags to your containers when you run it. Some useful ones are:

--rm # remove the container after it is stopped
-d # run the container in the background (detach)

Creating images

The main idea of Docker is to create your own special environments and reuse them. This means saving a running, configured/set up container to an image.

First set up the container.

# creating container
docker run -i -t --name my-server ubuntu:xenial
# install/set up environment with Python
root@628f79fad524:/# apt-get update
root@628f79fad524:/# apt-get install python
root@628f79fad524:/# echo 'print "Hello!"' >>
root@628f79fad524:/# exit 

Saving the container to an image.

docker commit my-server ubuntu-with-python
# see that it's created
docker images

# use the new image to start up container
docker run -it ubuntu-with-python
# see that the environment is set up
root@3555c0e3a292:/# python
root@3555c0e3a292:/# exit

You can run the command also when you run the container.

docker run ubuntu-with-python python

Multiple Docker containers linked (deprecated)

When you place a database in a container and your program in another, you can make the database accessible with links. You just need to pass a the name of the database container when you run the container with your app with the link  argument:

docker run --name postgres_db -d postgres 
docker run -it --link postgres_db:db ubuntu

#inside the ubuntu container 
root@e3a977c99abb:/# apt-get update
root@e3a977c99abb:/# apt-get install netcat -y
# check the connection
root@e3a977c99abb:/# nc db 5432

Containers on a network

The recommended way to establish communication between containers is with a network. Use this network when creating the containers. It is possible to add it later when the containers are running too.

# create a new network
docker network create my_network
# see that it's there
docker network ls

# run containers on the network
docker run --name pos_db --network=my_network postgres
docker run -i -t --network=my_network ubuntu

#inside the ubuntu container
root@5434eb0cd678:/# apt-get update root@5434eb0cd678:/# apt-get install netcat -y
# check the connection 
root@5434eb0cd678:/# nc db 5432

Exit/shut down

To stop/shut down a container means to simply shut it down as you would with a power button to a computer. The progress on disk will remain, but the progress in memory will be lost.

To exit a container when you are inside it in the interactive mode you simply type exit  or hit ctrl+d.

To stop a container type docker stop <container name>:

# stop a running container
docker stop my-server

Clean the PC

Working with docker creates some artifacts. All the pulled images as well as stopped containers are stored in your computer and over time can take up some space. To remove containers type docker rm <container name> :

# remove one stopped container
docker rm my_server
# stop all containers -q lists container IDs
docker stop $(docker ps -a -q)
# remove all stopped containers
docker rm $(docker ps -a -q)

# remove a running container with --force (-f): 
docker rm -f my_server 
# remove all stopped/running containers
docker rm  -f $(docker ps -a -q)

You might want to remove the pulled/saved images as well:

# remove specific image
docker rmi ubuntu:latest
# remove pulled/created images that don't have an associated container 
docker image prune
# or
docker rmi $(docker images -q)

This Docker overview looked at simple commands that you can run from the terminal. If you want to use docker in production you should use Dockerfiles – an automatized way how to use docker images.