Docker Basics

From ppwiki
Jump to: navigation, search

Prerequisites

For the purpose of this tutorial, I am using a virtual machine running on EXSI 5.5 host. The VM has 2GB of memory, 1 CPU and 60GB of disk. I named the VM Docker2001.The VM is in a DHCP and DNS environment with a docker2001.dfw.ppnet as the FQDN. If you have a bare metal, you can use that also. The VM has Debian Stretch installed on it.

Installation

Login to your server, create a file called docker_install.sh. Copy and paste the script below into the file.

#!/bin/bash
sudo apt-get -y install curl
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo apt-get -y install software-properties-common
sudo apt-get -y install apt-transport-https
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
sudo apt-get update
apt-cache policy docker-ce
sudo apt-get install -y docker-ce

Save the file and make the file executable and run the script.

sudo chmod +X docker_install.sh
sudo ./docker_install.sh

Check the installation by checking the docker version

ppaul@docker2001:~$ sudo docker --version
Docker version 18.06.1-ce, build e68fc7a

Working with images

The first command we are going to us, is the command to list all our images.

ppaul@docker2001:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED  

We can see that for now we have no images.

Download images

The command to download an images is

docker pull "name_of_images 

We are going to download the basic Nginx image

sudo docker pull nginx:latest
ppaul@docker2001:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f7bb5701a33c        6 days ago          126MB

Now we can see that we have an image.

Working with images

  • List all images
ppaul@docker2001:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              ed1ffcb5eff3        6 days ago          456MB
nginx               latest              f7bb5701a33c        6 days ago          126MB
  • Delete an image
ppaul@docker2001:~$ sudo docker rmi ed1ffcb5eff3
Untagged: mysql:latest
Untagged: mysql@sha256:e1b0fd480a11e5c37425a2591b6fbd32af886bfc6d6f404bd362be5e50a2e632
Deleted: sha256:ed1ffcb5eff39aed723a66ee895854a6417485f85629de7ba87610beb6bf39ed
Deleted: sha256:a1baf24e23890cd261b3fb74574bd1ea905b0f9c6008ca304a7c1ef4e238f9fa
Deleted: sha256:13155cb001017562d45bbc41f578f8be6770163a16fd0121f03501b67e54cd86
Deleted: sha256:1cd759e6aa7d3cedd5d0c20f16bfe7e7dd91485edaf5cbf0a23bcdccc43aeea8
Deleted: sha256:e2cef815e506cd47cc6ea937f127d44d7ddf35abee7fb5ece526aaef2e48e71f
Deleted: sha256:700b12f75e2792a4111743a0aed5078c4d9b4510e68a1f10d8d2452b1d6b48de
Deleted: sha256:5f7c68324b959d2c806db18d02f153bc810f9842722415e077351bc834cc8578
Deleted: sha256:338fc0cd3fb4b87a2b83d274e8fbf475fbde19947c4ac5c5eb6e981a6fb0e8f0
Deleted: sha256:f7a4ccab931f1d1e861961eb951a7806d91ccb375e737fe1f84282f6bbafd2be
Deleted: sha256:f388e1092f8fb931a3cd07a7381bd9707d19526ff81f8b624e932f4919c27a3e
Deleted: sha256:e209b7a884b4d2e9d56bbac40ced48f2caa6a19e7ad6eb6dd20ff754f3af2c5d
Deleted: sha256:2401cf11c5455d505ef49657afcc709197ffcdfc9bd732508e9b62578a30b3a5
Deleted: sha256:814c70fdae62bc26c603bfae861f00fb1c77fc0b1ee8d565717846f4df24ae5d

Working with containers

In this section, we are going to create run a container with the Nginx image we download in the previous section. the command to create and run a container is;

docker run --name "name_you_want" -d  -p "host_port":"container_port" "image_name"
  • Example
docker run --name test_Nginx -d -p 80:80 nginx
  • Explanation

The command above will create a container with the name test_Nginx, which will run as a daemon and the port 80 of the host will be mapped to the port 80 of the container. The -d option can be removed if wanted.

Basic containers commands

  • List all running container
ppaul@docker2001:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
08dad63189e1        nginx               "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp   test_Nginx
  • List all container
ppaul@docker2001:~$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
08dad63189e1        nginx               "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp   test_Nginx
  • Delete a container
ppaul@docker2001:~$ sudo docker rm 08dad63189e1

References

https://hub.docker.com/

Conclusion