Open For Ad

Open for Ad

Latest Posts

Understanding Docker commands - Build a container image, start and remove container

For Docker overview and setup docker locally refer Setup-Docker-and-build-docker-image. In this post I will spawn a container(with an image built in this post) and will run most commonly used docker commands like starting a container, stopping/removing container and publish images to remote repository.

1. List all locally stored images

n0r0082@m-c02z31rnlvdt ~ % docker images 
REPOSITORY         TAG       IMAGE ID       CREATED        SIZE
nodejsapp          latest    25b6a2a39b88   17 hours ago   993MB
zytham/nodejsapp   latest    25b6a2a39b88   17 hours ago   993MB
busybox            latest    beae173ccac6   4 weeks ago    1.24MB

2. Running the container image - run a new container

n0r0082@m-c02z31rnlvdt ~ % docker run --name nodeapp-container -p 8080:8080 -d nodejsapp
15d4656ca3571431b39080b68757f26a698cab34e78797627b4852247353ba5c

  • Above command tells Docker to run a new container called nodeapp-container from the nodejsapp image. 
  • The container will be detached from the console (-d flag), which means it will run in the background. 
  • Port 8080 on the local machine will be mapped to port 8080 inside the container (-p 8080:8080 option)

3. List of all running containers

n0r0082@m-c02z31rnlvdt ~ % docker ps 
CONTAINER ID   IMAGE       COMMAND         CREATED         
... STATUS         PORTS                                       NAMES
15d4656ca357   nodejsapp   "node app.js"   
... 3 minutes ago   Up 3 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nodeapp-container

4. List of all active and Inactive containers

n0r0082@m-c02z31rnlvdt ~ % docker ps -a
CONTAINER ID   IMAGE              COMMAND                CREATED         
STATUS                      PORTS        NAMES

15d4656ca357   nodejsapp          "node app.js"          
4 minutes ago   Up 4 minutes                0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nodeapp-container

7a14bc1d20aa   zytham/nodejsapp   "node app.js"          
12 hours ago    Exited (137) 12 hours ago                                               laughing_faraday

f88dc3ad15fb   busybox            "echo 'Hello world'"   
21 hours ago    Exited (0) 21 hours ago                                                 modest_galois
  • with "-a" option we can see all inactive containers.

5. Get additional details a running container:

n0r0082@m-c02z31rnlvdt ~ % docker inspect nodeapp-container
[
    {
        "Id": "15d4656ca3571431b39080b68757f26a698cab34e78797627b4852247353ba5c",
        "Created": "2022-01-30T12:48:20.3601401Z",
        "Path": "node",
        "Args": [
            "app.js"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2824,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-30T12:48:20.7424185Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:25b6a2a39b887274d463589aabff2428bb5fbed3be3126a6057b4561fad93178",
        ...
        ...
        "LogPath": "......-json.log",
        "Name": "/nodeapp-container",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        ...
        
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "8080/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8080"
                    }
                ]
            },
            ...
            ....
        },
        "GraphDriver": {
            "Data": {
                ...
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "15d4656ca357",
            "Domainname": "",
            "User": "",
            ...
            "ExposedPorts": {
                "8080/tcp": {}
            },
            ....
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "e51097e9d2d54fcc30d97452926dd5e79dc7df3b1c0e8434b92fe92857aaf8eb",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8080/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8080"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "8080"
                    }
                ]
            },
            ..
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "7e67c344180d1bfe713c168fc80c06284e07f39329ea140a1cdd1610be2e72a9",
                    "EndpointID": "2a8a3cf1046ecbc0f9c5b3eefaf252a2526a84a126b362237b10756055e2e9ee",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2"
                    ....
                }
            }
        }
    }
]

6. Running a shell inside an existing container

In order to explore environment details inside container, we need a bash shell in container. Running docker container internals explains in detail how to open a bash terminal in container and run commands inside it.

7. Stopping a container:

n0r0082@m-c02z31rnlvdt ~ % docker stop nodeapp-container
nodeapp-container
  • To stop running app or container, above command tells Docker to stop the nodeapp-container container.
  • It will stop the main process running in the container and consequently stop the container. At this point we have stopped container but the container itself still exists and we can see it with command "docker ps -a"
    n0r0082@m-c02z31rnlvdt ~ % docker stop nodeapp-container
    nodeapp-container
    n0r0082@m-c02z31rnlvdt ~ % docker ps -a                 
    CONTAINER ID   IMAGE              COMMAND                CREATED          
    .. STATUS                       PORTS     NAMES
    15d4656ca357   nodejsapp          "node app.js"          28 minutes ago   
    ... Exited (137) 4 minutes ago             nodeapp-container
    
    7a14bc1d20aa   zytham/nodejsapp   "node app.js"          13 hours ago     
    ... Exited (137) 12 hours ago              laughing_faraday
    
    f88dc3ad15fb   busybox            "echo 'Hello world'"   22 hours ago     
    ... Exited (0) 22 hours ago                modest_galois
    

8. Removing a container:

n0r0082@m-c02z31rnlvdt ~ % docker rm nodeapp-container 
nodeapp-container
n0r0082@m-c02z31rnlvdt ~ % docker ps -a               
CONTAINER ID   IMAGE              COMMAND                CREATED        
... STATUS                      PORTS     NAMES
7a14bc1d20aa   zytham/nodejsapp   "node app.js"          
.... 13 hours ago   Exited (137) 13 hours ago             laughing_faraday

f88dc3ad15fb   busybox            "echo 'Hello world'"   
.... 22 hours ago   Exited (0) 22 hours ago               modest_galois
n0r0082@m-c02z31rnlvdt ~ %
  • With command "rm" container is removed.
  • With "ps -a" we can see its not listing as inactive container.

9. Pushing the image to an image registry

Setup-Docker-and-build-docker-image explains in details how to publish locally created image to remote repository.

10. Build container image(docker image)

Container images are composed of layers, which can be shared and reused across multiple images.
Setup-Docker-and-build-docker-image explains in details how to create container image from scratch and use it spawn container.



Reference: Kubernetes in Action By Marko Lukša

No comments