Useful Docker Command and How to start Docker

Posted by

To pull image from docker registry

https://hub.docker.com/search?image_filter=official&q=

docker pull httpd

root@Jami:/home/jami/Docker# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
13808c22b207: Pull complete
6e9a8835eae4: Pull complete
4f4fb700ef54: Pull complete
b927d001db70: Pull complete
559cc51378ed: Pull complete
d2b091e65160: Pull complete
Digest: sha256:73b2e9e2861bcfe682b9a67d83433b6a6b36176459134aa25ec711058075fd47
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

To list image

docker image ls

root@Jami:/home/jami/Docker# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
httpd        latest    fa0099f1c09d   9 days ago   148MB

To check container running

root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

To create conatiner with specifying name and without name

docker create –name Jami httpd (specifying name )

docker create httpd (without specifying name but it create with random name)

root@Jami:/home/jami/Docker# docker create --name Jami httpd
e0ecd035aae9e1d17e5aea9cf52491897bce23120db983c0e5ef9c38018aaab7

root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS    PORTS     NAMES
e0ecd035aae9   httpd     "httpd-foreground"   36 seconds ago   Created             Jami
root@Jami:/home/jami/Docker# docker create httpd
9c5e6e8c7d269f2c72b326a239c80e54a88b30469676b3b85cb9659bde47fbfa
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS    PORTS     NAMES
9c5e6e8c7d26   httpd     "httpd-foreground"   10 seconds ago       Created             optimistic_pascal
e0ecd035aae9   httpd     "httpd-foreground"   About a minute ago   Created             Jami

To start Docker conatiner

docker start 9c5e6e8c7d26

root@Jami:/home/jami/Docker# docker start 9c5e6e8c7d26
9c5e6e8c7d26
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
9c5e6e8c7d26   httpd     "httpd-foreground"   3 minutes ago   Up 2 seconds   80/tcp    optimistic_pascal
e0ecd035aae9   httpd     "httpd-foreground"   5 minutes ago   Created                  Jami
root@Jami:/home/jami/Docker#

you can start with specifying CONTAINER ID or NAMES

root@Jami:/home/jami/Docker# docker start Jami
Jami
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS              PORTS     NAMES
9c5e6e8c7d26   httpd     "httpd-foreground"   4 minutes ago   Up About a minute   80/tcp    optimistic_pascal
e0ecd035aae9   httpd     "httpd-foreground"   6 minutes ago   Up 2 seconds        80/tcp    Jami

To stop container

docker stop 9c5e6e8c7d26

root@Jami:/home/jami/Docker# docker stop 9c5e6e8c7d26
9c5e6e8c7d26
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c5e6e8c7d26 httpd "httpd-foreground" 6 minutes ago Exited (0) 2 seconds ago optimistic_pascal
e0ecd035aae9 httpd "httpd-foreground" 7 minutes ago Up About a minute 80/tcp Jami

To restart stop container

root@Jami:/home/jami/Docker# docker restart 9c5e6e8c7d26
9c5e6e8c7d26
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c5e6e8c7d26 httpd "httpd-foreground" 7 minutes ago Up 2 seconds 80/tcp optimistic_pascal
e0ecd035aae9 httpd "httpd-foreground" 8 minutes ago Up 2 minutes 80/tcp Jami

To pause container

root@Jami:/home/jami/Docker# docker pause  9c5e6e8c7d26
9c5e6e8c7d26
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                  PORTS     NAMES
9c5e6e8c7d26   httpd     "httpd-foreground"   12 minutes ago   Up 5 minutes (Paused)   80/tcp    optimistic_pascal
e0ecd035aae9   httpd     "httpd-foreground"   14 minutes ago   Up 7 minutes            80/tcp    Jami

To kill container

root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                       PORTS     NAMES
9c5e6e8c7d26   httpd     "httpd-foreground"   13 minutes ago   Exited (137) 2 seconds ago             optimistic_pascal
e0ecd035aae9   httpd     "httpd-foreground"   14 minutes ago   Up 8 minutes                 80/tcp    Jami

To remove Container

root@Jami:/home/jami/Docker# docker rm  9c5e6e8c7d26
9c5e6e8c7d26
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS     NAMES
e0ecd035aae9   httpd     "httpd-foreground"   15 minutes ago   Up 9 minutes   80/tcp    Jami

How to go inside container and run command inside container?

go inside using exec and path /bin/bash

docker exec -it e0ecd035aae9 /bin/bash

root@Jami:/home/jami/Docker# docker exec -it e0ecd035aae9 /bin/bash
root@e0ecd035aae9:/usr/local/apache2#

we can query insider container

root@e0ecd035aae9:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  modules

exit to comeout
root@e0ecd035aae9:/usr/local/apache2# exit
exit
root@Jami:/home/jami/Docker#

note: using ‘it’ and /bin/bash we can interactively go inside and work in container (for interact)

for only run command from outside container

root@Jami:/home/jami/Docker# docker exec  e0ecd035aae9 ls
bin
build
cgi-bin
conf
error
htdocs
icons
include
logs
modules

How to access container network using IP address

using inspect command

IPAddress”: “172.17.0.3”,

root@Jami:/home/jami/Docker# docker inspect e0ecd035aae9

                   "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "MacAddress": "02:42:ac:11:00:03",
                    "NetworkID": "527c00dec56d7fe9a9f297b2ad40f6c171f5a31de15ae321205a193e822af040",
                    "EndpointID": "aaa2661e2b5649267c207eaedf1669050472c95a891718913a154ed54ebe59d7",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": null
                }
            }
        }
    }

How to connect container using ip address

curl http://172.17.0.3

root@Jami:/home/jami/Docker# curl http://172.17.0.3
<html><body><h1>It works!</h1></body></html>

Use of RUN command

pull + create + start + attach

The docker run command is one of the fundamental commands in Docker, used to create and start a new container based on a Docker image. It allows you to specify various options and configurations for the container, such as port mappings, environment variables, volumes, and more. Here’s how you can use the docker run command:

docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
  • OPTIONS: These are various flags and settings you can pass to customize the behavior of the container. Some common options include:
    • -d, --detach: Run the container in the background (detached mode).
    • -p, --publish: Publish container ports to the host. For example, -p 8080:80 maps port 8080 on the host to port 80 inside the container.
    • -e, --env: Set environment variables inside the container.
    • --name: Assign a name to the container.
    • -v, --volume: Mount volumes from the host into the container.
    • --network: Attach the container to a specific Docker network.
    • -it: Allocate a pseudo-TTY and keep STDIN open even if not attached.
    • And many more.
  • IMAGE: Specifies the Docker image to use for creating the container. If the image is not already available locally, Docker will attempt to pull it from a registry (like Docker Hub) before creating the container.
  • COMMAND (optional): Specifies the command to run inside the container. If not provided, Docker will use the default command specified in the Dockerfile used to build the image.
  • ARG (optional): Additional arguments to pass to the command specified in COMMAND.

Here’s an example of using docker run to create and start a container:

docker run -d -p 8080:80 --name my_container my_image

This command starts a new container named my_container based on the Docker image my_image, detaches it (runs in the background), and maps port 8080 on the host to port 80 inside the container.

The docker run command is central to Docker workflows, allowing you to create, configure, and run containers easily and efficiently, whether for development, testing, or production purposes.

run -d

pull + create + start + do not attach

docker attach
The docker attach command lets you connect to a running container’s console or terminal, allowing you to interact with processes running inside the container. Essentially, it “attaches” your current terminal session to the container’s standard input, output, and error streams, allowing you to see the container’s output and input commands as if you were directly interacting with it.

In simple terms, it’s like peeking into the container’s console or terminal, letting you see what’s happening inside and type commands as if you were working directly within the container. This can be useful for troubleshooting or debugging purposes when you need to directly interact with the processes running inside a container.

docker run httpd

docker run -d httpd

root@Jami:/home/jami/Docker#  docker run httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Mon Apr 15 00:27:56.734710 2024] [mpm_event:notice] [pid 1:tid 136639826352000] AH00489: Apache/2.4.59 (Unix) configured -- resuming normal operations
[Mon Apr 15 00:27:56.734880 2024] [core:notice] [pid 1:tid 136639826352000] AH00094: Command line: 'httpd -D FOREGROUND'
dkdkk
lddl^C[Mon Apr 15 00:28:12.764686 2024] [mpm_event:notice] [pid 1:tid 136639826352000] AH00491: caught SIGTERM, shutting down
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                     PORTS     NAMES
6bcdc537feda   httpd     "httpd-foreground"   19 seconds ago   Exited (0) 2 seconds ago             mystifying_visvesvaraya
e0ecd035aae9   httpd     "httpd-foreground"   52 minutes ago   Up 45 minutes              80/tcp    Jami
root@Jami:/home/jami/Docker# docker run -d httpd
d725dc38ccf8bbc3fe88e7cca3dc3ad0efee19a78c0a6c00244207c6c93011af
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                      PORTS     NAMES
d725dc38ccf8   httpd     "httpd-foreground"   4 seconds ago    Up 3 seconds                80/tcp    friendly_haslett
6bcdc537feda   httpd     "httpd-foreground"   59 seconds ago   Exited (0) 42 seconds ago             mystifying_visvesvaraya
e0ecd035aae9   httpd     "httpd-foreground"   52 minutes ago   Up 46 minutes       

How to connect container from outside or your laptop

using port forwarding

docker run -itd -p 80:80 httpd or docker run -d -p 80:80 httpd

host port : eg -80

container port: eg -80

root@Jami:/home/jami/Docker# docker run -itd -p 80:80 httpd
2821b9afb607c51c20cb2a6cb3e82d7eb9fa2656d0c1c4c0b0fe48366dba7e2b
root@Jami:/home/jami/Docker# docker run -itd -p 81:80 httpd
856775dadd5020d3050660cc65effaf6beecffc249c55a9836678e2699d0e5ff

root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS                     PORTS                               NAMES
856775dadd50   httpd     "httpd-foreground"   About a minute ago   Up About a minute          0.0.0.0:81->80/tcp, :::81->80/tcp   sweet_davinci
2821b9afb607   httpd     "httpd-foreground"   About a minute ago   Up About a minute          0.0.0.0:80->80/tcp, :::80->80/tcp   dazzling_pasteur

How to create docker image

VM have operating system but container don’t have operating system

Collection of filesystem

rootfs + userfs + appfs + appfs

Using Container

ubuntu + apache + git + java = 5mins

Create and run ubuntu container

docker run -itd ubuntu

Go inside ubuntu container


 docker exec -it 5e57256f9491 /bin/bash

install apache inside ubuntu container

apt-get install apache2

exist from ubuntu container and check if apache install or not

docker exec 5e57256f9491 which apache2

Create image of ubuntu container


 docker commit -a "jami" -m"ubuntu-apache" 5e57256f9491 ubuntu-apache

docker commit -a “author” -m “comment” conatiner ID image_name

-a “author”

-m “comment”


check image created or not

docker image ls

To check how many layer of container

docker history 5e57256f9491

to create container from image

docker run -itd ubuntu-apache

upload image to docker hub repository

login to –> https://hub.docker.com/repositories/jami123 –> create repository

tag similar name to docker image as repository

docker tag ubuntu-apache jami123/jami

login to docker hub via vm

docker login

push the image to repository

docker push jami123/jami

pull the image to create container

docker pull jami123/jami:latest

run to create container

docker run -itd jami123/jami

How to check layer of image?

docker info
.
.
.
.
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Username: jami123
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Go to path — > cd /var/lib/docker

root@Jami:/home/jami/Docker# cd /var/lib/docker
root@Jami:/var/lib/docker# ls
buildkit  containers  engine-id  image  network  overlay2  plugins  runtimes  swarm  tmp  volumes

cd overlay2

root@Jami:/var/lib/docker# cd overlay2

How to automate all above process using Dockerfile

create file Dockerfile

root@Jami:/home/jami/Docker# vi dockerfile

FROM ubuntu
ARG user=raju
MAINTAINER Rajesh kumar devops@rajehskumar.xyz
RUN touch /opt/raju.txt
RUN apt-get update
RUN apt-get install git -y
RUN apt-get install apache2 -y
RUN apt-get install java
ENV JAVA /tmp/java_home

create image using below command

root@Jami:/home/jami/Docker# docker build -t jami .
[+] Building 23.2s (8/8) FINISHED                                                                                                                        docker:default
 => [internal] load build definition from dockerfile                                                                                                               0.0s
 => => transferring dockerfile: 182B                                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                    0.0s
 => [1/4] FROM docker.io/library/ubuntu:latest                                                                                                                     0.0s
 => CACHED [2/4] RUN touch /opt/raju.txt                                                                                                                           0.0s
 => [3/4] RUN apt-get update                                                                                                                                       8.6s
 => [4/4] RUN apt-get install git -y                                                                                                                              13.6s
 => exporting to image                                                                                                                                             0.8s
 => => exporting layers                                                                                                                                            0.8s
 => => writing image sha256:bccc8e8bbfefb3c5fc5345183f37f3a8b940470a4d36b6afc762ed5a3615d85c                                                                       0.0s
 => => naming to docker.io/library/jami   

Docker run to create container

root@Jami:/home/jami/Docker# docker run -itd jami
4a0db3ea65a33df8ad0db0fee3924b42a2a1aa60e22db94edfd673cfa759890a
root@Jami:/home/jami/Docker# docker ps -a
CONTAINER ID   IMAGE           COMMAND              CREATED             STATUS                      PORTS                               NAMES
4a0db3ea65a3   jami            "/bin/bash"          3 seconds ago       Up 2 seconds                                                    great_wu

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x