I tried Docker Container Manipulation Part-I

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Introduction

I am Akshay Rao working in Annotation.Inc. I tried different container manipulations in this blog. I have used Go app for demonstration purposes.

How to Run a Container

The “docker run” command is used to start a container using images. I used docker run "image name" command which has generic syntax but the actual syntax is Docker "object" "command" "options"

  • Object - describes which docker object will be manipulated
  • Command - task that the docker deamon will be assigned
  • Options - parameter which will overrule the defaults behaviour of the command.
  • docker container run --publish 8080:8080 aksrao1998/first-go-project

    How to Publish a Port

    syntax:- -—publish "host port":"container port" To publish port i used —publish or -p option and binding the container port 8080 with host system 8080. The app will be accessed in localhost:8080 To stop the container close the terminal window or press ctrl+c

    How to Use Detached Mode

    The container stops when the terminal window is closed. This is because, by default the container that are ran in foreground are attached to the terminal. So in-order to run the container separately detach option is used.
    This a very famous options —detach or -d
    Example:- docker container run —detach —publish 8080:8080 aksrao1998/first-go-project

    docker container run -d -p 8080:8080 aksrao1998/first-go-project
    6360b3c15f34b8dc605079cfd1c58f9e4ea9c900d4307bab5fafe766c4623451

    The order of the options provided doesn’t matter, but make sure that the options are written before image name, anything written after image name is considered as an argument.

    How to List Containers

    docker container ls
    
    CONTAINER ID   IMAGE                           COMMAND          CREATED         STATUS           PORTS                 NAMES
    6360b3c15f34   aksrao1998/first-go-project   "/app/project"   5 minutes ago   Up 5 minutes   0.0.0.0:8080->8080/tcp   affectionate_hertz

    Container ID is provide as it is 64 character long but for display only first 12 characters are displayed.
    Name is automatically assigned by the daemon, we can also manually assign name to the container for better observerability.

    How to Name or Rename a Container

    Every container has two identifiers

  • Conatiner ID - a random 64 character-long string
  • Name:- combination of two random words, joined with underscore
  • —name option can be used while running a container.
    To rename the container use container rename Syntax- docker container rename container identifier new-name

    docker rename affectionate_hertz akshay_rao
    docker container ls
    
    CONTAINER ID   IMAGE                          COMMAND          CREATED          STATUS            PORTS                 NAMES
    6360b3c15f34   aksrao1998/first-go-project   "/app/project"   19 minutes ago   Up 6 seconds   0.0.0.0:8080->8080/tcp   akshay_rao

    How to Stop or Kill a Running Container

    The container can be stoped by closing the terminal window or ctrl+c.
    But for those container which are detach options need to use container stop command.
    Syntax:- docker container stop “identifier"
    Example- docker container stop akshay_rao
    This command sends SIGTERM signal to shutdown the container properly, if the container doesn’t stop within certain time than SIGKILL signal is sent to shutdown immediately.
    To kill the container directly kill command can be used.
    docker container kill "identifier"
    docker container kill akshay_rao

    Conclusion

    I hope that this helps in managing docker containers and stay tuned for Part-II
    Thank you