1. install Docker

 a. Install using the convenience script: There is different way to install docker. The simpliest way is to install it using script. There are two commands:

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

Note: Using these scripts is not recommended for production environments

Once it is installed, please verify docker it is installed :

 $docker version

2. Creating and starting a new container

The first thing to do it upload a docker image and run it. For this, we will look for an image in dockerhub. Docker Hub is the public registry provided by Docker Inc

For example, we will use this image Whalesay:https://hub.docker.com/r/docker/whalesay

It is a simple application which displays a message. The command run will verify if the image already exist. if it does not exist docker uploads the image and then executes it.

 

$ sudo docker run docker/whalesay cowsay boo
Unable to find image 'docker/whalesay:latest' locally
latest: Pulling from docker/whalesay
Image docker.io/docker/whalesay:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
e190868d63f8: Pull complete 
909cd34c6fd7: Pull complete 
0b9bfabab7c1: Pull complete 
a3ed95caeb02: Pull complete 
00bf65475aba: Pull complete 
c57b6bcc83e3: Pull complete 
8978f6879e2f: Pull complete 
8eed3712d2cf: Pull complete 
Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b
Status: Downloaded newer image for docker/whalesay:latest
 _____ 
< boo >
 ----- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   

 

Notes: 

1. When you launch a daemon or other program in a container that you want to run in the background, remember to use either the --detachflag or its short form, -d.

$ sudo docker run -d image-name

 

2. The docker ps command only shows running containers by default. To see all containers, use the -a (or --all) flag:

In the first command, there is no container becauser. the whalesay container is existed.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

The container is displayed with the existed status.

$sudo docker ps -a
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS                 PORTS       NAMES
ea3a7ac762a9        docker/whalesay                    "cowsay boo"             32 minutes ago  Exited (0) 32 minutes ago.             beautiful_wiles

3. To display the list of the existing images 

~ $sudo docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
docker/whalesay                    latest              6b362a9f73eb        4 years ago         247MB

Container is an instance of an image. We can create different containers from one image.

4. Create and start the container of web server NGINX

In this tutorial, you’ll install a web server called NGINX. Web servers are programs that make website files and programs accessible to web browsers over a network.

By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available from outside the container user the option -p (publish)

   $sudo docker run -d -p 8080:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
68ced04f60ab: Pull complete 
28252775b295: Pull complete 
a616aa3b0bf2: Pull complete 
Digest: sha256:2539d4344dd18e1df02be842ffc435f8e1f699cfc55516e2cf2cb16b7a9aea0b
Status: Downloaded newer image for nginx:latest
8fea43dd48d09072827517a3b6d4e963871fcb63524ed4fc5e8271038cbfa108
docker: Error response from daemon: driver failed programming external connectivity on endpoint confident_clarke (53d8eaa79ee700dc412ae395212a6ed54c05d7baddbd79cc22253dc39471cead): Error starting userland proxy: listen tcp 0.0.0.0:8080: bind: address already in use.


This will install and download the image nginx. The container will be run in the backgroud (detached mode) and the tcp

port 80 will be mapped on 8080 on the docker host.

In this case the port 8080 is already in use, we will user 8082 instead:

~ $sudo docker run -d -p 8082:80 nginx
b4d405ce8e4f13c584233e2d0bb36d11855035168d5549baf3438c72d8057e16

Every time you run docker run and create a new container, that new container will get a unique identifier (The blob of characters)

 $sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
b4d405ce8e4f        nginx               "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:8080->80/tcp   kind_chau

You can create another instance of image that is mapped with another port.

5. Delete an image:

$ sudo docker rmi image

If there is an instance of this image (container) already started. You have to first stop and delete the container

then delete the image.

$ sudo docker stop container_id
$ sudo docker rm container_id

Command EXEC

Run a command in a running container. 

Fisrt we will create and install mysql container. Then, we will run some commands in mysql container. The name of the container is mysql.

$ sudo  docker run --name mysql -e MYSQL_ROOT_PASSWORD=root  -d mysql

Execute the command mysql insider the container mysql. 

~ $sudo docker exec -it mysql mysql --password
Password:
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

The password is root.

The command uses the option : --interactive (or -i) and --tty (or -t). First, the --interactive option tells Docker to keep the standard input stream (stdin) open for the container even if no terminal is attached. Second, the --tty option tells Docker to allocate a virtual terminal for the container, which will allow you to pass signals to the container.

Command Logs:

Fetch the logs of a container

 

sudo docker logs container_id

References:

Jeffrey Nickoloff; Stephen Kuenzli. Docker in Action, Second EditionManning Publications, 2019

Mohamed, Yousfi. DOCKER Part 3. https://www.youtube.com/watch?v=srGiAlnNsDU&t=359s

Add a comment