msgbartop
MAC OS X, Linux, Windows and other IT Tips and Tricks
msgbarbottom

07 Apr 16 Some DOCKER Commands

Here are a few basic Docker commands:
Also see: https://github.com/wsargent/docker-cheat-sheet

Installing the latest Docker environment in Linux:
curl -sSL https://get.docker.com/ | sh
Start the docker terminal in OSX
bash --login '/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/start.sh'
Info: Container Names without ‘/’ in the name refers to basic public containers templates
Containers Names with a ‘/’ refers to user containers in the format: username/BasicContainer
Containers Names with a ‘:’ refers to a tag which is normally used as subversion of basic/user containers

Search for images in Internet Docker repository
docker search ubuntu
Download a docker image from Internet to the local repository
docker pull ubuntu:14.04
List local docker images:
docker images
Run a self-made image(myname/ubuntu:tmpl_1) in the background giving the container the name MyUbuntu
docker run -itd --name=MyUbuntu myname/ubuntu:tmpl_1
Attach to a container for working in it (press 2 x Enter to get bash afterwards)
docker attach MyUbuntu
To leave from a container without it being automatically stopped:
CTRL-P + CTRL-Q
When a ^D(or exit command) is issued in a container bash, the container is then stopped. To restart it eg.
docker start MyUbuntu
List the running containers
docker ps
Listing all the docker (Running and stopped) containers
docker ps -a
Notes on running containers:
docker run -t -i → can be detached with ^P^Q and reattached with docker attach
docker run -i → cannot be detached with ^P^Q; will disrupt stdin
docker run → cannot be detached with ^P^Q; can SIGKILL client; can reattach with docker attach

TROUBLESHOOTING:
1) If you get this error message after giving a docker command:
Error response from daemon: client is newer than server (client API version: x.xx, server API version: y.yy)
Just run the following command to sync their versions:
docker-machine upgrade

Creating a new local Docker image from Public(Internet) pulled image
#### The COMMIT Method ####
Run the container based on this Image
docker run -t -i ubuntu:14.04 /bin/bash
Note: Remember the Container ID which is in the bash prompt (needed to create a new one based on this one)
eg. root@ba7cbe2cd3ce:/# (ba7cbe2cd3ce being the Container ID)
Install the desired software packages (apt-get update && apt-get install …. )
eg. apt-get update && apt-get -y upgrade ; apt-get install mc ssh fail2ban nmap ngrep iftop iotop htop
When finished then stop the container by disconnecting from bash with
exit or ^D
Now save the new Image locally:
docker commit -m "My special container template" -a "My Name" ba7cbe2cd3ce myname/ubuntu:v001
This will save locally the modified container as new Image named: myname/ubuntu:v001
To check:
docker images

#### The ‘Dockerfile’ method
Create a directory where you create a special file called Dockerfile
where the instructions on how to build the new image will be written.
mkdir ubuntu_template_001
cd ubuntu_template_001
touch Dockerfile
mcedit Dockerfile

————– Content —————-
# This is a comment
FROM ubuntu:14.04
MAINTAINER My Name <mmyname @mydomain.com>
RUN apt-get update && apt-get install -y mc ssh fail2ban nmap ngrep iftop iotop htop
RUN apt-get -y install postfix

– Save the file and then run the following command to create the image:
docker build -t myname/ubuntu_template_001 .
Note: The ‘.’ at the end of the command tells where the Dockerfile is
You will be presented with all output of the installation and the Image ID at the very end as follows:
—> 8578743d7b30

– We take a look at all the images created:
docker images

Result:

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
<none> <none> 8578743d7b30 3 minutes ago 290 MB
myname/ubuntu v001 9bc5a639a1de 20 minutes ago 318.6 MB

– We see that the Image with the ID 8578743d7b30 does not have a name or a tag.
Now we give it a name:tag
docker tag 8578743d7b30 myname/ubuntu:v002

– Check the images list again:
docker images
Result:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
myname/ubuntu v002 8578743d7b30 16 minutes ago 290 MB
myname/ubuntu v001 9bc5a639a1de 33 minutes ago 318.6 MB

– Now we can run a container based on this new image
docker run -it --name "Server2" myname/ubuntu:v002 bash -c 'service ssh start ; ifconfig eth0; bash'

Deleting an Image from the local repository
docker rmi michel/ubuntu:v002

Removing a container from the local docker system
docker rm ContainerName
or
docker rm ContainerID
To run a program inside a running but not attached container:
docker exec -t -i ContainerName bash -c 'command'