How To Docker Create Container, Change Container, Save as new image, and Connect to Container Step-By-Step PLUS much more


Docker Cheat Sheet

For those just getting started with Docker and for me to have an easy place to go back and get this basic information…I wanted to create a cheat sheet.  The electronic version of this document can be found at https://wp.me/p6B4pe-oP5

A couple URGENT basic notes…

the $ and the # in the below statements are not actually typed inThey are symbols to show you which command prompt you should be at.  the $ prompt is the Docker VM prompt.  The # is the Bash container command prompt.

Docker Installation

Let me start with if you do not have docker installed, you can install it on linux (eg. Ubuntu 16.x)

sudo curl -fsSL https://get.docker.com/ | sh

See https://docs.docker.com/docker-for-windows/ to install on Windows 10 with Hyper-V

You may have to put sudo in front of the commands if you have not setup your user to be an administrator.

Basic Commands:

$ sudo docker info                                 —  shows docker status and configuration
$ sudo docker ps                                   — show docker containers
$ sudo docker ps -l                               — show “latest” docker container -l = lower case L
$ sudo docker ps -a                               — show “all” docker container; even those not running
$ sudo docker images                           — show docker images (and tags)
$ sudo docker run -it <container> <app>       — connect / login to work interactively on container
$ systemctl status docker                     — show status and log for docker  <CTRL-C> to exit
# sudo systemctl enable docker          — enable docker <not usually needed> using system control
# sudo systemctl start docker             —  start docker <if it was stopped>
$ sudo service docker stop                  — Stop docker service
$ sudo service docker start                 — Start docker service
$ sudo service docker restart              — restart docker service
$ sudo usermod -aG docker <AdminUser>    — Add the <AdminUser> to Linux Authorized users for docker replace <AdminUser> with your username must log out and log back in for it to take affect

Definitions

  • Images– Images are the collection of file system, configuration, application and other components needed to run a container. Images are used to create containers. They have a base OS and any binaries or applications contained in it.  Images are SAVED copies of a container. A list of saved containers can be seen using the docker images command.
  • Container – A running image. Running instances of Docker images — containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel with other containers, and runs as an isolated process in user space on the host OS. A list of running containers can be seen using the docker ps command.
  • Nesting – Running a container based on an image making changes and saving as another image.  Any number of containers can be nested.  As an example, if I create  a new container running Ubuntu 16, add an app and save it I have a new nested image with the app that runs on Ubuntu 16.  The app container does not have the bits in it for Ubuntu 16, it only has the changes that were made from the base image to add the app.  This process can be repeated any number of times.
  • Docker daemon (or engine) – The background service running on the host that manages building, running and distributing Docker containers.
  • Docker client – The command line tool that allows the user to interact with the Docker daemon.
  • Docker Hub (repository) – A registry of Docker images. You can think of the registry as a directory of all available Docker images.
  • Dockerfile: Configuration file used to automate the image creation process to a Docker container

Step-By-Step

1) create container from ubuntu (latest) image and run a bash terminal.  This tells docker to use the latest available ubuntu image from the default repository (usually online) run an interactive session and leave me at the bash command line. The -i -t says to keep you logged into the new container so you can work with it “interactively”  Notice that your command prompt changed to a #.  This is because you are inside the container which does not have docker installed. It is just a container with Ubuntu 16.

$ docker run -i -t ubuntu:latest /bin/bash

2) Inside the terminal install something. In my case, I am just going to do updates and install curl and save this as a Base image.  From the linux command line…

# apt-get update
# apt-get install curl

3) Exit the container terminal so we can save the current container.

# exit

4) You will see that you now have the $ prompt.  This shows that you are no longer in the container.  You are back on the docker host.  Now we need to see what our container ID is for the container we were just working with.  the  -l [lower case L] stands for last image you created.  We need to get the container ID so we can work with it.  Take a note of your container id by executing following command :

$ sudo docker ps –l

Notice My container ID is: 7e20d89ce838

SNAGHTML159325a8

5) Now we just save the container as a new image.  In my example the new image name is ubuntu and the tagname is base. As a shortcut to typing the container id, you can select the ID and right click, Linux will automatically type it for you.

$ sudo docker commit 7e20d89ce838 ubuntu:base

Once you do this, you should get a sha256:76be…. value returned.  You now have a new image.

6) verify that you can see your new image with curl installed.

$ sudo docker images

7) You can connect to your new image by typing…

$ sudo docker run -it ubuntu:base

where  -it says you want to run an interactive session and ubuntu:base is the name:tag of the container.  If from the command prompt you type:

# which curl

You will see output that shows the location of curl  /usr/bin/curl

8) You can exit out of the container by typing:

# exit

 

More Great tips you may want to know about docker…

9) You can change so you do not need to type sudo in front of docker commands…

$sudo usermod -aG docker <AdminUser>

This will add the <AdminUser> to Linux Authorized users for docker replace <AdminUser> with your username must log out and log back in for it to take affect

$ systemctl status docker
# sudo systemctl enable docker
# sudo systemctl start docker

Now you should be able to run docker ps –a (or other docker commands) without elevating with sudo

10) Install remote desktop client. First you have to install the desktop.  Also remember you have to open port 3389 to your destination.  Connect to your container  then run

# sudo apt-get install -y xubuntu-desktop

NOTE: you will get some pop-up boxes in the interactive session.  You can take the defaults (assuing English).  This is a very large install so it could take 20 mins or more to complete.

# sudo apt-get install xrdp

# echo xfce4-session >~/.xsession

You  could exit out and save your new image with

# exit

$ sudo docker images
$ sudo docker commit <Image_ID>  ubuntu16:rdp

Connect back to your new container

$ sudo docker run -it ubuntu16:rdp

11) Let’s install something harder… R-Studio (https://www.datascienceriot.com/how-to-install-r-in-linux-ubuntu-16-04-xenial-xerus/kris/)

sudo echo “deb http://cran.rstudio.corsm/bin/linux/ubuntu xenial/” | sudo tee -a /etc/apt/sources.list

gpg –keyserver keyserver.ubuntu.com –recv-key E084DAB9
gpg -a –export E084DAB9 | sudo apt-key add –

# NOTE:  Install R-Base
sudo apt-get update
sudo apt-get install r-base r-base-dev -y

# NOTE:  Install R-Studio
sudo apt-get install gdebi-core -y
wget https://download1.rstudio.org/rstudio-0.99.896-amd64.deb
sudo gdebi -n rstudio-0.99.896-amd64.deb
rm rstudio-0.99.896-amd64.deb

12) You can remove an image with the docker rmi <image> command.  docker rm <container> to remove a container. However, containers built on other containers/images cannot  be removed as a container unlike a VM is not fully self contained.  It relies on the base container as it’s image.   Remember to show containers docker ps -a.  To show images use docker images.  The difference between the two is an <image> is a saved <container>.

docker rmi <image>
docker rm <container>

13) You can rename an image using docker tag.  This is especially helpful if you have many like images. tags are often used as a versioning tool.  However you create any number of naming conventions with both names and tags.

docker tag d583c3ac45fd myname/server:latest
or
docker tag ubuntu:base ubuntu16base:mytag

14) Docker auto assigns each container a name. You can rename a container using docker rename CONTAINER NEW_NAME

docker rename serene_pare mybase16

 

Using Private Registry:

15) If you are using a private registry (separate post to describe that later) you can push images to it with two commangs.  docker tag then docker push.  In this example, I added a docker images in the middle just to show you what it is doing.  Here is what is going on… danreghost02az.southcentralus.cloudapp.azure.com is the public dns name of the server that is setup as a docker registry.  :5000 is the port assigned to the registry.  iqssbase16 is the new name you want to give the image on the private registry.  With a private registry, you have to refer to the image with it’s fully qualified name.  I suspect there is a way to set the default in docker configuration files:

  • /etc/docker/registry/config.yml
  • /etc/docker/daemon.json
  • /etc/init/docker.conf
  • /etc/default/docker
  • /etc/init.d/docker

but that investigation will be left for another day.  For now, information on standing up your own registry can be found at: https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-docker-registry-in-blob-storage/.  Information on how to stand up a secure private registry on azure can be found at: https://docs.docker.com/registry/configuration/.  I know it is not easy to follow.  I will be creating a Step-By-Step in the coming weeks.

docker tag ubuntu:base danreghost02az.southcentralus.cloudapp.azure.com:5000/iqssbase16

the above command tags the image with the fully qualified registry – it still resides locally

docker images danreghost02az.southcentralus.cloudapp.azure.com:5000/iqssbase16

the above command just shows the local image with the new tag

docker push danreghost02az.southcentralus.cloudapp.azure.com:5000/iqssbase16

The above push command is actually what uploads the image to the private registry.

You can then use the image with the pull command

See https://docs.docker.com/registry/ for more examples.

More Resources